Here's our routine driver instantiation code
WebDriver driver =new InternetExplorerDriver();
driver.get("http://internal.com");
Let's say that the above opens an instance (window) of IE - Window 1. In this case, we have some JS on internal.com's index.html that opens a new window, say Window 2. The problem is that when we quit the driver, we can easily close Window 1 but we seem to have no control over Window 2.
driver.quit();
Are there any clean ways to close Window 2 and any other derivative browser windows at the end of every test case?
Related
I have the next situation:
I'm running Automation in MacOS with Selenium in Safari.
I want to take control over a new tab opened (Switch to a new tab in Safari)
I'm able to do it with Chrome and Firefox, but in Safari does not work.
The next code is what I'm using for that:
//Store the parent window
String parentWindow = driver.getWindowHandle();
//Open a new Windows(Mailtrap)
String a = "window.open('https://mailtrap.io/signin','_blank');";
((JavascriptExecutor)driver).executeScript(a);
//Take control over new browser
for(String handle: driver.getWindowHandles()){
driver.switchTo().window(handle);
}
//Make the same actions over new window
driver.manage().window().maximize();
driver.switchTo().defaultContent();
After that...
//Close window and back to the parent window
driver.close();
driver.switchTo().window(parentWindow);
driver.switchTo().defaultContent();
As I told before: It works in Firefox, Chrome, IE11, but in Safari 12.0.3 this is the message when I do a manual click over a tab.
"This Safari window is remotely controlled by an automated test."
To interact with this Safari window, you need to stop the current automated test session.
You should not use this window for normal web browsing.
"Turn OFF All Automation" , "Stop Session", "Continue Session"
Could anybody help me with this?
PS: Develop --> Allow remote automation is selected.
As the automation speed is very fast on Safari, the only solution for this was the "old friend forever":
Thread.sleep
That was the only solution, because the Implicit and Explicit waits were not working.
I am trying to open chrome, close it and then open it again using the same session.
Is a thing like this even possible? I've looked through the internet/stackoverflow and tried using driver.Close(); but with no luck.
Anybody has some experience with this and mind helping me?
thanks
In Selenium Webdriver, a browser session can be closed using two webdriver commands: close() and quit(). The situations in which they are used are briefly explained below:
close() is a webdriver command which closes the browser window which is currently in focus.
During the automation process, if there are more than one browser window opened, then the close() command will close only the current browser window which is having focus at that time. The remaining browser windows will not be closed. The following code can be used to close the current browser window:
driver.close() //where, ‘driver’ is the Webdriver object.
quit() is a webdriver command which calls the driver.dispose method, which in turn closes all the browser windows and terminates the WebDriver session.
If we do not use quit() at the end of program, the WebDriver session will not be closed properly and the files will not be cleared off memory. This may result in memory leak errors.
The following code can be used to close all the browser windows:
driver.quit() //where, ‘driver’ is the Webdriver object.
If the Automation process opens only a single browser window, the close() and quit() commands work in the same way. Both will differ in their functionality when there are more than one browser window opened during Automation.
Source: Reference link
I am creating automated test cases with Selenium WebDriver 2. My code is set up, so that FireBug automatically is opened when Selenium WebDriver opens FireFox. A few seconds later I ask Selenium to close it again. For some reason, if I don't do this, sometimes FireBug won't be available to me at a later stage.
My problem:
When I use the TestNG suite to run my test suite, some of the test cases end up opening FireBug but not closing it again. My guess is, that this is due to existing browser windows already being open. The "close FireBug code" is then misused to open FireBug instead of closing it.
For this reason, I would like to make a check to see if FireBug is open, and if so, I would like to close it.
Opening code:
String firebugPath = TO_Constant.fireBug();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
profile.setPreference("extensions.firebug.showFirstRunPage", false);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
driver = new FirefoxDriver(profile);
Code to close FireBug:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();
I can find ways to do this in javascript, using window.console. Is there any way to do something similar in java?
console.log('is DevTools open?', window.devtools.open);
You can also listen to an event:
window.addEventListener('devtoolschange', function (e) {
console.log('is DevTools open?', e.detail.open);
});
It doesn't work when DevTools is undocked. However, works with the Chrome/Safari/Firefox DevTools and Firebug.
The answer is by Sindre Sorhus and has been taken from here!
Find out whether Chrome console is open
I am opening a window based application in my desktop using Sikuli. After clicking on a button in that application, a browser is opened.
Can I get focus on that browser in Selenium? Can Webdriver detect an already opened browser?
The answer to that is no. In order for Selenium Webdriver to control a browser it must be instantiated as WebDriver object creating a session ID, etc..
One possible solution might be to somehow intercept the URL that is generated by the desktop application and use it to launch a Webdriver instance.
I have written Java code using Selenium Webdriver and AutoIt that opens a Firefox driver, navigates to a page that requires authentication via selection of a soft certificate, selects the appropriate certificate when the Certificate Selection window pops up, and clicks OK, at which point the page I'm accessing should load but instead it just loads a blank "New Tab"; a new tab isn't opened per se, just the current tab remains blank with the title of "New Tab".
If I perform this process manually (without having Selenium Webdriver open a Firefox driver and I just launch it myself) using the same exact Firefox profile, the page loads after authentication just fine. I should also note that the page loads successfully using driver instances of Internet Explorer and Chrome.
I am using Firefox v33.1, IE 10, and Chrome v38. Finally, I should note that this has nothing to do with my automated certificate selection process because even if I only have Selenium Webdriver open a Firefox driver and stop there and then I take the wheel and select the certificate myself and click OK, it still does the same thing.
I think Selenium Webdriver might have some annoying guard built into the Firefox drivers it instantiates that prevents it from loading pages requiring authentication. If this is the case, does anyone know how to possibly disable that?
Java needed to be set to "Always Activate" under the Firefox Add-ons -> Plugins. Before it was set to "Ask to Activate" but it never explicitly asked to activate after I selected the certificate, thus the page wouldn't load.