I have a web application that launches the Save Window (which is Modal) when I click on the Save Button. This window takes uptil 10 seconds to load completely (I can counter this with a wait).
I need to carry out some actions in this window, before I complete the save.
The problem I face is
- The moment the modal window is launched, there is no way I can use a driver.SwitchTo() or driver.Manage().GetAllWindowHandles() etc. I confirmed this with the following lines of Code.
driver.findElement(By.xpath("//*[#id='toolbar']/a[1]")).click();
// After the above line is executed, the Popup gets launched
Set<String> sWindowHandles = driver.getWindowHandles();
System.out.println("Popup");
System.out.println(driver.getWindowHandles().size()); // This always prints "1"
The 3 lines above are not executed at all (or at least not for a long time) until I explicitly close the Popup Window.
How do I work on some control that exists within the Save Window (Modal), when there is no way I can find the Window's handler?
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
doesn't work, because immediately after the modal window is opened (makes sense to GetWindowHandles only after it is launched), the subsequent lines aren't executed at all.
I'm caught in a deadlock. Please help me out.
If Windows handles is not working on an Application.
Java robot class function can be used.
Robot robot = new Robot();
//Doing a mouse over for the X and Y coordinates of button/link which opens modal window
robot.mouseMove(210,350);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
//Clicking tab til the cursor is on specific position (textbox/button)
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(100);
//Doing a mouse over for the X and Y coordinates of button/link
robot.mouseMove(300,150);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
For more information refer the below link.
http://alvinalexander.com/java/java-robot-class-example-mouse-keystroke
I sometimes find the windowhandles can take a while to update with the correct value even though the popup is visible. To counter this I use a loop which breaks when the windowhandles reaches the expected size.
Related
I am trying to automate an app in which when I go to a screen and click a button, it closes the current window, and opens new one, which closes again and new one opens. This happens 4 times and then finally I am at the window where the app to be tested it. Selenium WD is not able to recognize the final window when it is opened. Any one has faced this issue in the past?
Not sure but a possible solution would be to tell Selenium to focus on the new open window when it has stopped changing 4 times.
So you wait for the time for your application to open/close all these windows and at the end you do :
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
This way Selenium should focus on the last open window
I am automating using selenium 2.0, my application launches the login page by default in a new window, hence my application has by default two windows. These two windows will remain open always. In this case I could switch between the windows without any problem. The below code is executed without any errors.
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
The problem starts while clicking the menu options a pop up window launches to search the records. Here, I need to switch between these three windows. I tried the below piece of code. It returns only the first two window handles.
Set availableWindows = driver.getWindowHandles();
This popup window is coded in such a way that, "In a .jsp file it is parameterised as window.open()".
Please let me know, if some one could help me on this?
If you're only seeing 2 window in getWindowHandles(), then the popup is probably a iframe. In this event, use driver.switchTo().frame() to switch focus to that frame instead of looking for an entirely new window.
Here's the documentation on the switch method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#switchTo()
One probable solution is to use JavascriptExecutor.executeScript() method to run a javascript code and handle the pop up window without switching into pop up window.
For example, from parent window of pop up window, run a javascript code which is something like this.
JavascriptExecutor exec = (JavascriptExecutor)driver;exec.executeScript("var popup = <<popupopener function>>; //operate on popup object to manipulate the markup of pop up window");
For some of the web links on our page, there are external links which direct the user to say Facebook and Twitter. The links use the HMTL tag target="_blank" so that a new browser tab is opened for our Twitter page.
I would like to verify 1. the new browser tab is open, 2. set focus to it and 3. validate page elements in the new browser tab. The #3 part would be easy enough, once I get focus on it. Tasks #1 and #2 though, I can't figure out, nor can I find anyone talking about this.
Using Selenium2Library (WebDriver)
Selenium does not support tabs (as of June 2013, Selenium 2.33.0) and always opens new windows instead. If your test opens a new tab, good luck to you.
That said, if it correctly opens a new window, use Select Window.
Select Window | url=https://twitter.com/expectedPage
My working WebDriver (2.33.0) code in Java, hopefully it will help a little. The problems you are describing are where my Robot knowledge begins to fall off.
#Test
public void targetBlankLinkTest() {
// load the website and make sure only one window is opened
driver.get(file("TargetBlankLinkTest.html"));
assertEquals(1, driver.getWindowHandles().size());
// click the link and assert that a new window has been opened
driver.findElement(By.linkText("Follow us on Twitter!")).click();
Set<String> windowHandles = driver.getWindowHandles();
assertEquals(2, windowHandles.size());
// switch to the new window and do whatever you like
// (Java doesn't have Switch Window functionality that can select by URL.
// I could write it, but have been using this trick instead)
for (String handle : windowHandles) {
if (handle != driver.getWindowHandle()) {
driver.switchTo().window(handle);
}
}
assertThat(driver.getCurrentUrl(), containsString("twitter.com"));
}
if you want to verify if a new tab has opened or not then you can just compare the window handles before and after clicking a link/element which opens a new tab. Just follow this code might help you.
Here ${LINKDIN} is the element which opens in new tab. so before clicking on it save the window handles in variable and after clicking the element again save the window handle in a variable. now if a new is tab is opened then both the variable value will be different and if no new tab is opened then both variable value is same.in this way you can verify the ne wtab opening.
Go Back
${Current_window} List Windows
Click Element ${LINKDIN}
${New_Windows_list} List Windows
Should Not Be Equal ${Current_window} ${New_Windows_list}
Using Robot Selenium2Library keywords :
#{windows} = List Windows
${numWindows} = Get Length ${windows}
${indexLast} = Evaluate ${numWindows}-1
Should Be True ${numWindows} > 1
Select Window #{windows}[${indexLast}]
Location Should Contain /my/url/whatever
Title Should Be myTitle
Page Should Contain ...
You get the idea.
I recommend:
1. First, acquire new window handle by using the switchTo() method.
2. Then bring that window into focus using a JavaScriptExecutor:
((JavascriptExecutor) myTestDriver).executeScript("window.focus();");
Using switchTo() without a javascript executor to get focus , is not
very reliable in my opinion.
3. Next, do what you need to do in that window.
4. Use driver.close() to close that window.
5. Verify you are back at the parent window with focus.
U can use below code to set focus on new window:
Click element ${a_link_which_opensin_newtab}
Select window New
now u can perform your actions on new tab. If u want to switch back the main tab then use
Select window Main
Scenario is that I need to fill one form and click on a button, as soon as I clicked on the button, one new pop-up should display.
Now my problem is that when I click on a button; 3 or 4 blank windows are opening.
Script just before the error is as follows:
//To click on "Check Availability" Button
waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("ctl00_ContentPlaceHolder1_btnCheckAvailbility")));
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnCheckAvailbility")).click();
sleep(4000);:
Notes:
When I try to execute manually it works fine
When I run the same script by using Firefox it works fine but unfortunately I have to test this application only on IE.
I properly declared the "IEDriverServer.exe", so that is not an issue.
So ...friends any help or thought on this issue ???
hmmm...
seems a lil bit strange because you press on the button once.
I would recommend you this one approach of handling new windows after button press:
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
//Close the new window, if that window no more required
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
After much brainstorming I found that if we upgrade our Operating system to Windows 7 then this issue got resolved.
what is this command for?
You might think that Selenium.selectWindow() would be all you need. But that simply tells Selenium which window you want all the Selenium commands to go to. One of the commands you can send to it is "give this (currently selected) window focus".
It's a bit confusing, because Windows (and other systems) sometimes refer to the "selected window" - the one that's on top of the others, or the "active" window. Here, we call it the window that "has focus". It's the window where keyboard events will be directed. Inside a window, individual widgets (text fields, scroll bars, buttons) can have focus too.
So windowFocus() is like clicking on the title bar of the window that Selenium is currently working with.
From the Selenium Documentation
windowFocus()
Gives focus to the currently selected
window
Unless or until, you are using windowHandles to switch between multiple windows, ur focus will be default on first windows launched by selenium. widnowFocus does the same thing
In my experience, getting window focus using the Selenium windowFocus() method is sometimes not effective. I find myself sometimes using a JavascriptExecutor, then use the Selenium switchTo() method to switch to the handle that needs focus and then execute :
public static void getWindowFocus( String windowHandle ) {
driver.switchTo( windowHandle );
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript( "window.focus();" );
js = null;
}