How to implement headless browser for secure browser (HTTPS) or validate certificate by utilizing PhantomJs in Selenium? - selenium

I need to implement Headless Browser for HTTPS (validate certificate). For this I need to write extra line of code.
I have written for browser HTTP and it is working fine.
public class Headless {
public static void main(String[] args)
{
File src=new File("C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path",src.getAbsolutePath());
WebDriver driver=new PhantomJSDriver();
driver.get("https://www.google.co.in/");
System.out.println(driver.getTitle());
}
}
I google it and get some info:-
phantomjs --ignore-ssl-errors=yes;

DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] {
"--ssl-protocol=any",
"--ignore-ssl-errors=true"
};
dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcap);
You can read more about the command-line options on this link: Command Line Interface | PhantomJS

Related

We are unable to detect your camera while passing fake media to experitest chrome browser

I did not get a specific chrome option to work fine when my test is running on the server where we don't have a webcam when started chrome per the java selenium web driver script.
The goal is to mock a camera on the server and pass a fake media stream to the server machine where we don't have a webcam. The script is working fine on the local machine (because the local machine has a webcam) but it's not working on the experitest cloud browser where we don't have a webcam.
How to mock a camera on devices without a camera so that I can run my script through CI pipeline on a server or cloud browser?
The below code is working locally but not on the cloud browsers where we don't have support for the webcam and currently facing the we are unable to detect your camera error when running on a cloud browser
public class Sample {
private static final String ACCESS_KEY = "XXXX";
private RemoteWebDriver driver;
private URL url;
private DesiredCapabilities dc = new DesiredCapabilities();
#Before
public void setUp() throws Exception {
String videoSource = getClass().getClassLoader().getResource("sample1.y4m").toURI().toString();
System.out.println(videoSource);
ChromeOptions config = new ChromeOptions();
config.addArguments( //
"--use-fake-ui-for-media-stream", //
"--use-fake-device-for-media-stream", //
"--use-file-for-fake-video-capture=" + videoSource);
config.setHeadless(true);
config.setAcceptInsecureCerts(true);
dc.setCapability("testName", "Quick Start Chrome Browser Demo");
dc.setCapability("accessKey", ACCESS_KEY);
dc.setCapability(ChromeOptions.CAPABILITY, config);
dc.setCapability(CapabilityType.BROWSER_NAME, "chrome");
driver = new RemoteWebDriver(new URL("https://XXXXX/wd/hub"), dc);
}
#Test
public void virtualTryOn() {
driver.get("https://mytrialpage.com");
WebElement cookies = driver.findElement(By.id("onetrust-accept-btn-handler"));
cookies.click();
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[#id='virtualTryOn']")));
WebElement vto = driver.findElement(By.xpath("//button[#id='ctaVirtual']"));
vto.click();
driver.switchTo().frame("scan-iframe");
}
#After
public void tearDown() {
System.out.println("Report URL: "+ driver.getCapabilities().getCapability("reportUrl"));
driver.quit();
}
}
**
cloud browser output:
**

Selenium, WebDriver Manager, and Electron Desktop Apps

I'm building out some ui automation tests for an electron app. I have an existing test framework built in C# using Selenium and Appium for web and mobile devices.
I figured out how to start the chrome driver and target the electron app, but to do so, I had to not use the extremely handy WebDriverManager package.
This is my set up
[SetUp]
public void TestSetUp()
{
ChromeOptions chromeOptions = new()
{
BinaryLocation = #"ElectronApp.exe",
};
_driver = new ChromeDriver(#"local driver path", chromeOptions);
}
This works to open up the electron app using Chrome driver. I did have to match the version of Chrome the electron app used and made sure to download that version of the webdriver.
What I want to know is if there's a good way to use WebDriver Manager to set up my driver, but open the electron app.
This was what I was trying:
[SetUp]
public void TestSetUp()
{
ChromeOptions chromeOptions = new()
{
BinaryLocation = #"ElectronApp.exe",
};
ChromeConfig chromeConfig = new();
new WebDriverManager.DriverManager().SetUpDriver(chromeConfig, "98.0");
_driver = new ChromeDriver(chromeOptions);
The 98 is the version of Chrome that the electron app is apparently using -- that's the same version I had to match the driver for.
This is the stack error:
Message:
System.Net.WebException : The remote server returned an error: (404) Not Found.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
HttpWebRequest.GetResponse()
WebClient.GetWebResponse(WebRequest request)
WebClient.DownloadBits(WebRequest request, Stream writeStream)
WebClient.DownloadFile(Uri address, String fileName)
BinaryService.DownloadZip(String url, String destination)
BinaryService.SetupBinary(String url, String zipPath, String binaryPath)
DriverManager.SetUpDriverImpl(String url, String binaryPath)
DriverManager.SetUpDriver(IDriverConfig config, String version, Architecture architecture)
DesktopTests.TestSetUp() line 41
--TearDown
DesktopTests.Teardown() line 54
I was so focused on the WebDriver Manager, I missed that the chrome version I was specifying wasn't valid. Instead, I used the full version, and it worked great.
[SetUp]
public void TestSetUp()
{
ChromeOptions chromeOptions = new()
{
BinaryLocation = #"ElectronApp.exe",
};
ChromeConfig chromeConfig = new();
new WebDriverManager.DriverManager().SetUpDriver(chromeConfig, "98.0.4758.102");
_driver = new ChromeDriver(chromeOptions);
}

How to scrape JavaScript webpages using PhantomJS?

I am trying to Crawl this site and get the links of each Job posting. I am using the Selenium and PhantomJS to render the webpage, but I am not able to find the links in the rendered content. This is the script I am using
public static void main(String[] args) {
// TODO Auto-generated method stub
String url="https://www.paycomonline.net/v4/ats/web.php/jobs?clientkey=D25120971391831BA4315C705AA7ABF1&jpt=";
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setPlatform(Platform.LINUX);
RemoteWebDriver driver = getRemoteDriver("http://localhost:9515", capabilities);
driver.manage().window().maximize();
driver.get(url);
new WebDriverWait(driver, 360)
.until(RemoteWebDriver -> ((JavascriptExecutor) RemoteWebDriver)
.executeScript("return document.readyState").equals("complete"));
byte[] content = driver.getPageSource().getBytes();
String html = new String(content);
System.out.println(html);
}
I am running PhantomJS in my local machine at port 9515.Can someone help?

How to run BrowserStackLocal for website behind firewall

I'm trying to run my tests against a url that is behind our firewall.
I run this command:
./BrowserStackLocal [KEY] -force &
And then I run my tests:
py.test blah blah
In BrowserStack I see this error:
I must be doing something wrong when running the BrowserStackLocal binary, but I can't figure out what.
Any ideas?
To access the local servers on BrowserStack Automate, you can follow these steps:
Setup the Local Testing connection by executing the Local Testing binaries.
Add the capability 'browserstack.local' = true in your scripts.
It seems you have done Step 1, have you added the capability as well?
Few things to consider here :
Are you setting correct Host settings .
Have you tried with multiple browser versions because I get some errors while trying for IE and chrome for Android devices.
Please try to use the below simple code and check :
public static final String USERNAME = "xyz";
public static final String AUTOMATE_KEY = "xyz";
public static final String URL = "http://" + USERNAME + ":" + AUTOMATE_KEY + "#hub.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception
{
DesiredCapabilities caps = new DesiredCapabilities();
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost","2.2.2.2");
System.setProperty("http.proxyPort","8080");
System.setProperty("http.proxyUser","xyz");
System.setProperty("http.proxyPass","xyz");
caps.setCapability("browser", "FireFox");
caps.setCapability("browser_version", "40.0");
caps.setCapability("os", "Windows");
caps.setCapability("browserstack.debug", "true");
caps.setCapability("browserstack.local", "true");
WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
System.out.println(driver.getTitle());
driver.quit();
}

WebDriver not opening URL

I'm very new to selenium so I'm having trouble spotting the problem with my code. I'm using a webDriver backed selenium object, it starts the driver but never opens the URL and the driver just closes after a few moments. The last time this happened to me it was just because I had left "http" out of the URL. So what's causing it this time?
public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login");
System.out.println(selenium.getXpathCount("//img"));
}
The setup looks like:
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(2000);
}
The teardown method just consists of driver.close().
I'm using selenium 2.14 and the testNG Eclipse plug-in.
You might need to do the following
selenium.open("www.testsite.com/login");
Check out this example from the selenium site:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
link to selenium
You would need to add driver.get(url) like below.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}