Switching to empty window handle (Selenium webdriver) - selenium

I have this problem with window handles. I click on a button and expect a popup window to appear, but i'm not sure if it pops up. I print out all window handles before switching, [u'{7f8728c1-75db-4899-b4d1-63af134ee2fc}', u'']. I switch to the list[-1] that means u'' in this case. After switching I print out the current window handle and I get the answer {7f8728c1-75db-4899-b4d1-63af134ee2fc}
Two questions arises here:
1- what is an empty window handle u''?
2- Did I switch to u'' and failed and switched back to main window or selenium can't switch to empty handle and chooses to switch to main window?
Thanks in advance

Maybe its a alert?
driver.getWindowHandles() - prints all windows that are enable in this session, if there is only 1 string '{7f8728c1-75db-4899-b4d1-63af134ee2fc} then this is your current window and there are no more windows. Try driver.switchTo().alert().accept();

Related

Switching control to a Modal window (not an alert) using Selenium Webdriver

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.

Unable to switch among the windows

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");

Unable to fetch URL of window containing an alert box while using selenium webdriver

I am using selenium webdriver + java + eclipse + testng for my automation scripts.
I am trying to get the URL of window which contains an alert box.
On clicking download button on a webpage, it opens an alert box in a new window. I want to fetch the URL of this window.
I tried getCurrentURL command for this but i am getting UnhandledAlertException : Modal dialog present. If i dismiss the alert box the window containing is immediately closed so it is not possible to get the URL.
It seems the alert box (modal dialog here) is blocking webdriver in reading the URL of the window.
Please suggest me a solution for this.
Thanks
I'm not sure I understand the question as an alert box does not have any URL !
Anyhow, you can access it this way : Alert alert = webDriver.switchTo().alert();
You can then retrive the text content or interact with it as described in here : http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/Alert.html
I think you need to get the set of windowHandles first for this driver.getWindowHandles();. Then get the required Handle of the newly opened window by iterating them. After that you can switch to the opened window using driver.switchTo().window("pass the handle here");
Now your control comes to new window. Then use like driver.getCurrentUrl();
Hope this could help you. Best Regards :)

Selenium WebDriver how to close browser popup

I am writing tests for a web App using selenium webDriver and came across a scenario where when I try to close the browser I get a popup saying "Are you sure? The page is asking you to confirm that you want to leave - data entered will be lost." with 2 buttons: Leave Page and Stay on Page
How do I click on those buttons?
( ( JavascriptExecutor ) _driver )
.executeScript( "window.onbeforeunload = function(e){};" );
solved the issue for me
IAlert alert = driver.SwitchTo().Alert();
alert.Accept(); //for two buttons, choose the affirmative one
// or
alert.Dismiss(); //to cancel the affirmative decision, i.e., pop up will be dismissed and no action will take place
You can interact with alerts and the like using the Alert API. Switching to the alert and confirm it would look like this (in Java):
Alert alert = driver.switchTo().alert();
alert.accept();
This is also explained in the Selenium FAQ.
def close_all_popups(driver):
driver.window_handles
for h in driver.window_handles[1:]:
driver.switch_to_window(h)
driver.close()
driver.switch_to_window(driver.window_handles[0])
You might try using keyboard events. So once the window pops up:
Tab onto the "Ok" button.
driver.Keyboard.PressKey(Keys.Tab);
You'll have to play around to see how many tab presses are required.
Then hit space.
driver.Keyboard.PressKey(Keys.Space);
Yeah it's sort of a hack, but WebDriver is still pretty immature.
EDIT: This will work for "real" popups, but as another answerer said, maybe not for weird in-page things. Basically, if you can tab to the close button manually, this method should work.
Not sure what is the structure of the popup that you have used.
Here are a few that may work for you if you have used either of as to popup.
If its an alert. you can try:
driver.SwitchTo().Alert()
If its a window that pops up then:
driver.SwitchTo().Window(<windowname>)
For iFrame:
driver.SwitchTo().Frame("iFrmLinks")
Once you get through either of the three then you can access all its internal elements.
Regards
Tahir
I've got the same problem when i have the form of fields and "done editing" submit button, because when Selenium IDE auto-click the javascript function, that responsible to disable confirmation window (leave page or still on it), it does not take "mouseup" mouse event that mean window.confirm still works and auto-pass test was fails. My solution is override javascript function window.onbeforeunload in this case (no need to ask if we know that we do when we record test in Selenium IDE). You can run override script in the Selnium IDE before click on "Save" (or something like this) button through selenium.runScript - it should simple disable the confirmation window.
Command: runScript
Target: {window.onbeforeunload=function(e){};}
You need to handle the unexpected alerts using try catch blocks. Put your code in try block and catch the 'UnhandledAlertException'
Example:
try{
.....
.....
code here
}catch(UnhandledAlertException e ){
driver.switchto().alert().dismiss();
}
Put one of these before the click event:
selenium.chooseCancelOnNextConfirmation()
selenium.chooseOkOnNextConfirmation()

selenium.windowfocus() what is this command used for?

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