Open new private window for cross browser testing using Selenium Webdriver - selenium

In my script, I am opening private window of the browser to verify some contents in website. Below is the sample code:
if(osName.contains("Mac")){
new Actions(tester.getInternalDriver()).keyDown(Keys.COMMAND).keyDown(Keys.SHIFT).sendKeys("P").keyUp(Keys.COMMAND).keyUp(Keys.SHIFT).build().perform();
}
else if (osName.contains("Win") || osName.contains("nux")){
String pvtWin = Keys.chord(Keys.LEFT_CONTROL,Keys.SHIFT,"p");
tester.getInternalDriver().findElement(By.cssSelector("body")).sendKeys(pvtWin);
}
The code used to work fine when I was executing these test cases on my local (mac) machine or when Jenkins execute it locally on the Windows server.
But now the challenge I am facing when I am running my script on browserstack virtual machine. My local machine is Mac, and when I try to run something on the Windows(virtual) machine, it try to look for COMMAND button on the keyboard and test case fails. Is there any better way (javascript?) to open new private window which can work on any platform and for any browser?
I am looking for an option to open new tab, new window & new private window. I know javascript has limitations to override browser behavior.

You can execute the following JavaScript, to open a new window in your Selenium tests.
JavascriptExecutor js = null;
if (driver instanceof JavascriptExecutor)
js = (JavascriptExecutor)driver;
String scr = "window.open('https://www.google.com','_blank');";
js.executeScript(scr);
That said, any specific reason you wish to open a new window in your tests?

Related

how to not open chromedriver.exe when I run my selenium executable

I made a program that alerts me if there is any seats available
using python, selenium, chromedriver.
Then I made it excutable using pyinstaller with --onefile, -w options since I do not need and I do not want to see any console windows.
But then when I execute my program, chromedriver's console, not sure to call this as a console, shows up.
Is there any way to not see this?
If I understood your question properly, it would mean that you do not wanna see any browser being open up when you run your script.
If that is the case, I would advise headless mode.
options = webdriver.ChromeOptions()
options.add_argument('--window-size=1920,1080')
options.add_argument("--headless")
and then pass the options to driver object like this:
driver = webdriver.Chrome(executable_path = driver_path, options = options)
Using headless mode, you would not see any browser window or console.Your script will run in background.

Selenium navigate().to() stuck without error

I am running tests using TestNG, chromium and Selenium, in Java, on two machines:
my own laptop
a Mac Mini I do not have physical access to, that I connect via SSH, that has the same identical project structure as the project on my laptop.
Both machines operate behind the same corporate proxy.
The issue I am having is the following:
while the tests run smoothly on my machine, when I execute the command to start them on the Mac Mini, the execution stops when telling the Selenium WebDriver to navigate to any specified url using driver.navigate().to(url) or driver.get(url); causing the program to freeze without throwing any exception, essentially remaining on hold.
The code resembles the following:
in the #BeforeClass beforeClass() driver is initialized with an instance of ChromeDriver using ChromeOptions. Among other settings, I add the following arguments to the ChromeOptions like such chromeOptions.addArguments(options): where options is things like --disable-web-security, --allow-running-insecure-content, --allow-insecure-localhost and most importantly: --proxy-server=address:port (e.g. --proxy-server=172.26.44.146:3128).
in the #BeforeMethod beforeMethod() after checking that the driver is not null and that the URL url is a valid one, I execute something similar to the stylized following code:
try {
log.debug("Navigating to: " + url);
driver.navigate().to(url);
log.debug("Navigated to url: " + driver.getCurrentUrl());
} catch (Exception e) {
log.error("Unable to navigate at page: " + url.toString());
throw e;
}
As I said, when running the test on the Mac Mini via SSH using the command line in a similar fashion to: java -cp WORKSPACE org.testng.TestNG testng.xml the code stops executing and freezes at log.debug("Navigating to: " + url); essentially not producing other output and forcing me to manually stop the execution of the test.
As additional information, tests are run using chromium version 72.0.3609.0.
The ChromeOptions also receive the following proxy setting (like such: chromeOptions.setProxy(p);) where p is a Proxy with
p.setProxyType(ProxyType.MANUAL);
p.setAutodetect(false);
I suspect an issue with the proxy (and am doubtful I have correctly preconfigured the program), but, quite frankly, I am puzzled by the fact that the same script runs locally on my machine and is unable to run locally on the Mac Mini when the tests are launched via SSH.

How to run sahi script on a new browser after opening a new browser through Sahi script

I am able to open firefox browser from a browser using sahi script like:
var $path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
//var $name = "firefox";
var $browseroption = " -profile D:\\sahi_pro_V4.0\\userdata\\browser\\ff\\profiles\\sahi1 -no-remote";
_execute($path + $browseroption);
But, when i give _navigateTo after this, it is executed in the first browser itself. Is it possible to run it in the newly opened firefox browser?
This may be late, but one of the following two APIs could be useful depending on your test requirement: _launchNewBrowser and _openBrowser.

Jenkins + Selenium WebDriver + MSTest issue

I have created a test method in Visual Studio 2010 Ultimate that checks for the existance of two text boxes. I instantiate "InternetExplorerDriver" in the AssemblyInitialize() method and have setup Jenkins (on Windows 7) to run the MSTest method using the MSTestRunner plugin. The test seem to pass but I'm not seeing the DOS command window that I see when the "InternetExplorerDriver" instantiates and also I'm not seeing the Internet Explorer browser from loading the web page at all even though the test has passed. I also call the Quit() method on the webdriver in AssemblyCleanup. I'm running Jenkins service as my own NT account on my local box as running the service as "Local System Account" has issues. Here's my code snippet:
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
webDriver = new InternetExplorerDriver(ieDriverDirectory, ieOptions);
The ieDriverDirectory has the InternetExplorerServer.exe running in 32bit mode. When I run the same test method from Visual Studio 2010 IDE or from the mstest.exe /testcontainer:, I'm able to see the DOS command window loading with the port number, the browser loading, the test method passing and the browser closing at the end when Quit() is called.

Is there any way to find out if selenium is running using code?

I want to find out if selenium is running and if it isn't then call a bat file to start it. Is it possible to find out if selenium standalone is running?
If you are using Java, you can invoke the server programmatically -
RemoteControlConfiguration settings = new RemoteControlConfiguration();
File f = new File("C:\\selenium-profile");
settings.setFirefoxProfileTemplate(f);
settings.setReuseBrowserSessions(true);
SeleniumServer seleniumserver=new SeleniumServer(settings);
seleniumserver.boot();
setUp("http://www.google.com", "*firefox");