How to run BrowserStackLocal for website behind firewall - browserstack

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();
}

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 implement headless browser for secure browser (HTTPS) or validate certificate by utilizing PhantomJs in 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

Cross browser test

I have to do login to an application with two different browsers (IE & FF) and hence I tried to do cross browser test. When the URL gets passed to IE, I am getting link as "Continue to this website not recommended", whereas the same link will not be displayed in FF. In the below if I use this statement driver.findElement(By.id("overridelink")).click(); then it works fine in IE but failing in firefox since there wont be any link in FF.
please let me know the possible where one script should run in both the browsers. Below is the script which am trying
#Test
#Parameters("browser")
public void verifypagetitle(String browsername) {
if(browsername.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver",
"D:\\2.53.1\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if (browsername.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.firefox.bin",
"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
}
driver.get("URL");
driver.manage().window().maximize();
driver.findElement(By.id("overridelink")).click();
driver.findElement(By.id("j_username")).sendKeys("userid");
driver.findElement(By.id("j_password")).sendKeys("pwd");
driver.findElement(By.id("j_password")).submit();
This is most likely caused by the IE security level. In my experience if I change it to Low, the warning is not present. You can try and see if this will solve the issue in your case.
Another solution is to add conditional logic to check the browser being used. Something like this:
Capabilities cap = ((RemoteWebDriver) browserDriver).getCapabilities();
String browsername = cap.getBrowserName();
// This block to find out if it is IE
if ("internet explorer".equalsIgnoreCase(browsername)) {
driver.findElement(By.id("overridelink")).click();
}

selenium grid, creating node programmatically

I have to create a java application that will start a node and connect it to the hub. So far I have been able to do so when the hub and node are on the same computer, but as soon as I try to connect on another machine hub, the registering process hang forever.
I tried different approach. To just call my bat file function in code.
String command = "java -jar selenium-server-standalone-2.26.0.jar -role node -hub http://192.168.0.11:4444/grid/register -port 4449 -Dwebdriver.chrome.driver=data\\driver\\chromedriver.exe -Dwebdriver.ie.driver=data\\driver\\IEDriverServer.exe -nodeConfig data\\configurations.json";
try
{
pr = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{
e.printStackTrace();
}
The command work when called from a bat file, but in code it only works if the node and hub are on the same computer.
I also tried to use the RegistrationRequest.
RegistrationRequest req = new RegistrationRequest();
req.setRole(GridRole.NODE);
Map<String, Object> nodeConfiguration = new HashMap<String,
Object>();
nodeConfiguration.put(RegistrationRequest.AUTO_REGISTER, true);
nodeConfiguration.put(RegistrationRequest.HUB_HOST, "192.168.100.66");
nodeConfiguration.put(RegistrationRequest.HUB_PORT, 4444);
nodeConfiguration.put(RegistrationRequest.PORT, 5555);
URL remoteURL = new URL("http://" + "192.168.100.66" + ":" + 5555);
nodeConfiguration.put(RegistrationRequest.PROXY_CLASS, "org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
nodeConfiguration.put(RegistrationRequest.MAX_SESSION, 1);
nodeConfiguration.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
nodeConfiguration.put(RegistrationRequest.REMOTE_HOST, remoteURL);
nodeConfiguration.put(RegistrationRequest.MAX_INSTANCES, 1);
req.setConfiguration(nodeConfiguration);
remote = new SelfRegisteringRemote(req);
remote.startRemoteServer();
remote.startRegistrationProcess();
Same result, when I try to run on another computer hub, it hand at the registering process.
INFO - Registering the node to hub :http://192.168.100.66:4444/grid/register
any idea why? or how to do it.
I figured out my problem, which is really simple to fix. In my code I had
URL remoteURL = new URL("http://" + "192.168.100.66" + ":" + 5555);
I just needed to replace the ip address by my local ip address, not the hub ip address, and it worked. Which is weird cause I am pretty sure I took this code from somewhere online and he had a variable for the ip, and it was the same for remoteURL and the HUB_HOST
well. Not sure whether is suitable for you but I'd like to share approach I use on my project.
I've got remote machine with 192.168.4.52 IP and selenium stanadlone server running on it.
All selenium test suites I got locally.
So to run my selenium test suite on remote machine I simply use these settings in BaseSeleniumTest.java on my local machine:
....
#BeforeClass
public static void firefoxSetUp() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920, 1080));
}
#Before
public void homePageRefresh() throws IOException {
driver.manage().deleteAllCookies();
driver.get(propertyKeysLoader("login.base.url"));
}
#AfterClass
public static void closeFirefox(){
driver.quit();
}
where string
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
indicates IP of machine which I want to run my selenium test siute on.
I'm starting server on remote machine with this command:
java -jar selenium-server-standalone-2.26.0.jar in cmd before I run my test suite.
Hope it be helpful for you.