I am using Selenium webdriver 2.44 with firefox 34 on windows 7 machine. I have a script where I hover over an 'open page' icon and click it. The click opens the a new tab manually and in chrome driver 2.15. The scenario opens a new window instead of tab which I handle in firefox-34. below is the code.
public void switchWindow(){
//try {
String winHandleBefore = driver.getWindowHandle();
System.out.println("before "+winHandleBefore);
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(winHandleBefore)) {
return;
}
}
The problem I am facing is one or two tests run for more than 10,000 seconds as they look like they hang at switching the windows from parent to child. This is issue is reproducible each and every time.Has anybody seen this issue?. Is there a workaround?.Kindly let me know if additional information needs to be provided.
I have fixed this issue by closing the first window and shifting the focus back on the newly opened window. I didn't expect the fix would be this simple.
The fix works for FF-35 as well.
Related
I have a test that always fail when running inside Jenkins.
My project includes Selenium webdriver, JAVA, Maven, TestNG, Jenkins, Allure (reports).
I have a few suites of tests with 100+ test cases, and I iterate them through 3 different browsers (the tests run in parallel using TestNG). They all run (using maven command line) and pass in my development laptop, and on the test server when using a command line.
I have 2 problems regarding Jenkins and separated them into 2 questions- one of them is described in this question, and the other (IE11 issue) is here.
The problem starts when running inside Jenkins in the test server!
The test fail in mobile emulator (Chrome browser) - in the test I click on a link to verify that a new window was opened with the correct url.
I tried 3 types of clicks (Selenium click, Actions, JS) and all returned a null handle.
The code:
Here I create the main window handle and click the link:
String mwh = driver.getWindowHandle();
WebElement poweredBy = (new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Consts.POWERED_BY_XPATH_1000))));
poweredBy.click();
And this is, part of, the method that gets the handle and verify the new window:
public boolean closePopupWindow(String mwh, String mTitle, String layoutNumber) {
// For IE11- make sure popup blocker is turned off in the options. else it will have only one window handle and fail
boolean isOpenedWindowCorrect = false;
String newWindow = null;
Set<String> handlers = driver.getWindowHandles();
for (String window : handlers) {
if (!window.equals(mwh)) {
newWindow = window;
}
}
// the focus is on the main page. need to switchTo new page and close it
driver.switchTo().window(newWindow);
System.out.println("The focus now is on the NEW window");
String newTitle = driver.getTitle();
System.out.println(newTitle);
This is the error I'm getting:
java.lang.NullPointerException: null value in entry: handle=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:34)
at com.google.common.collect.SingletonImmutableBiMap.(SingletonImmutableBiMap.java:42)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:73)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:123)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.window(RemoteWebDriver.java:995)
at il.carambola.pages.Page.closePopupWindow(Page.java:786)
Do you think there is a security issue that Jenkins wont open new windows in the browser? Is it VERY slow to open the window?
The same tests PASS when not using mobile emulator. (I have the same test in Chrome and Firefox and it succeed to click and pass the verification).
JDK 1.8.0_162
Jenkins V 2.121.1
Server- AWS t2.large - 8GB RAM, Windows server 2016 Data center, 64bit
It's clear that when your program gets to this line
driver.switchTo().window(newWindow);
that newWindow is still null. The way this is written, it could be a timing issue, and it could be another issue causing the popup to not show up. To make sure it's not a timing issue, I would suggest adding some kind of wait for there to be multiple window handles before you try to switch windows. Something like
(new WebDriverWait(driver,10)).until(d -> d.getWindowHandles().size() == 2);
If that wait fails, then you know the popup is being blocked and can go from there.
I am working with selenium automation. I have coded to login a page and it works fine. The login passed and new child window was opened as a result and parent window was closed.
Due to that my web driver stops and result in exceptions.
Exception in thread main org.openqa.selenium.NoSuchWindowException:
No window found (WARNING: The server did not provide any stacktrace information)
Please help
Selenium keeps a list of windows (handles). The webdriver needs to point to the right handle. When a window is closed, its handle is deleted, and your driver will now point to something that doesn't exist anymore.
Likely, you have to explicitly switch window to point your driver to the right window handle. Maybe this could help:
Switch between two browser windows using selenium webdriver
Selenium python bindings: Moving between windows and frames
"new child window was opened as a result and parent window was closed"
The code below from (1) shows you how to retrieve the list of window handles, and retrieve the right handle based on its title.
private void handleMultipleWindows(String windowTitle) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(windowTitle)) {
return;
}
}
}
You can use that function (or something similar) to retrieve the new window. From the code you provided, this would give:
// Entering the credentials in the login window.
driver.findElement(By.id(txtUserId)).clear();
driver.findElement(By.id(txtUserId)).sendKeys(poovan);
driver.findElement(By.id(txtPassword)).clear();
driver.findElement(By.id(txtPassword)).sendKeys(welcome1);
driver.findElement(By.id(btnSubmit)).click();
// Here the login window gets closed, handler to that window disappears, and driver becomes stale.
// So we need update the driver to point to the new window
handleMultipleWindows("The title of my new window");
driver.findElement(By.name(bono)).sendKeys(080);
Please use below code. It will work for sure.
String parentWindow = driver.getWindowHandle();
Set<String> handles2 = driver.getWindowHandles();
for (String windowHandle : handles2) {
if (!windowHandle.equals(parentWindow)) {
driver.switchTo().window(windowHandle);
}
}
I am trying to automate initial configuration(of my server) through webpage. After hitting my server ip https:/localhost:4443 and entering my credentials ,i get a window to change password(overlay/popup window).
Problem:- if i browse the same ip from another m/c or from another browser , i get a window over window i.e one more window over change password window(Please click the link to see the screenshot).
What i tried is to get the handle of the window but its not working, its providing one handle only.
**Its not frame also.
HTML code -- https://dl.dropboxusercontent.com/u/91420517/Html_Code.JPG
Here's my code
WebDriver driver=new FirefoxDriver();
driver.get("https://localhost:4443/ControlPoint/");
driver.findElement(By.xpath("//*[#id='name']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='pass']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='loginForm123']/div[6]/div[1]/div")).click();
Set<String> winIds = driver.getWindowHandles();
System.out.println("Total Windows --- " + winIds.size()); // its resulting the size as 1 which is not correct.
Iterator<String> it = winIds.iterator();
String mainWin=it.next();
String changeWin=it.next();
String shareWin =it.next();
driver.switchTo().window(shareWin);
String warning = driver.findElement(By.xpath("html/body/div[4234]/div[1]/span")).getText(); // to get the text on 3 window
System.out.println(warning);
How to resolve this issue .Please help. Any other way to click on buttons on window 3.
If the additional window is opened asynchronously, then possibly you are checking for it (with getWindowHandles()) too early, before it has been created - this is a common issue with Selenium tests and asynchronous page updates.
If this is the issue, it can be solved by trying a few times with a wait in between, checking each time whether a new window has appeared.
I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.
Please suggest if there any other method for doing the same in firefox.
Store the window handle first in a variable, and then use it to go back to the window later on.
//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();
//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);
Additionally, you can try to maximise the window after switching to it, which should also activate it.
this.webDriver.manage().window().maximize();
Try switching using the window name:
driver.switchTo().window("windowName");
Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp
As described in other topics, you can use
driver.manage().window().setPosition(new Point(-2000, 0));
too.
# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd() # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")
Only thing that worked for me on mac: self.driver.fullscreen_window().
I am testing a web application that creates a new window long after a button is clicked. The sequence is the following
window 1: (parent window) click button to create window 2
window 2: progress window appears until background process on server returns data
window 3: progress window turns into 3rd window (with different handle)
I want to properly wait for the 3rd window to appear. I know what the 'title' of all 3 windows will be however in order to get the titles from WebDriver I have to use the following code:
while(timeout has not occured...){
for (String handle : _driver.getWindowHandles()) {
String myTitle = driver.switchTo().window(handle).getTitle();
if(3rdWindowTitle.equalsIgnoreCase(myTitle)){
return true;
}
}
}
This will effectively switch the active window back and forth every time it loops because of the 'switchTo'. This causes the firefox windows to cycle back and forth really quickly and is obnoxious. What I need is a way to get the title's of the windows that are available without having to 'switchTo' each window in a loop waiting for the 3rd window. Any ideas?
I basically want a method (waitForWindowByTitle(titleIWant)) which will block until the window with the title I want appears.
Well, Better you can wait for your window to appear by checking the number of windows. Like:
for(int i=0; i<noOfTrials;i++){
noOfWindows = driver.getWindowHandles().size();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if(noOfWindows>currentNoOfWindows){
break;
}
}
}
then for the first and last time you can browse through the windows (using switchTo) and navigate to the window you want.