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");
Related
My scenario which produces the question goes something like below:
I enter a webpage via normal means, next I press on a button, to start a HTML5 application on this webpage, this application is inside an iFrame. On application start I'm being prompted to either turn the sound on or off. At this point there are two possible outcomes:
1. When I answer this prompt manually, new buttons appear in the application window, as expected.
2. When I answer this prompt through automation via Appium, new buttons do not appear.
Now to the question:
To answer the prompt I use the click() method from Selenium. Is it possible that this click() is not considered to be executed by a human and therefore doesn't trigger necessary things? And since I don't have access to the source of the application can I force the Selenium click() to look exactly like a human click?
Here is the code I use to execute the mentioned click:
//Application loading up, hence the sleep
Thread.sleep(5000);
AppiumTestBase.getDriver().switchTo().frame("e_iframe");
Thread.sleep(5000);
WebElement soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
AppiumTestBase.getStandardWaitTime().until(elementToBeClickable(soundOff));
soundOff.click();
The program is able to find and switch in to the iFrame, there are no cross-origin issues either. The AppiumTestBase is just there for initializing the driver, setup capabilities etc. I also tried clicking the element via Actions and JavaScript, but there was no change in behavior.
In C# a workaround I've found to actually take control of the mouse and move it/click with it is to use "Microsoft.VisualStudio.TestTools.UITesting" for the Keyboard/Mouse libraries. From there, you can tell it "Mouse.Click(new Point(X, Y));"and it will move your mouse to that location and click.
Sample Code:
using Microsoft.VisualStudio.TestTools.UITesting;
var soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
Mouse.Click(new Point(soundOff.Bounds.X, soundOff.Bounds.Y));
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();
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
My application has an admin account and the testing has been within that.
This account then has hyperlinks for 'regular' users that they use for their login, for example:
One http://dmplanning-stage.herokuapp.com/p/7Fimn1FRs1WZe5xmFTUA
Two http://dmplanning-stage.herokuapp.com/p/FRs1WZe7Fimn15TUAxmF
Three http://dmplanning-stage.herokuapp.com/p/mFTUA7Fimn1FRs1WZe5x
These links are generated each time I run the test suite and the id's are different.
I've created a test to locate and click on the hyperlink on a page that lists these users and their login hyperlinks. The test runs and selenium makes the browser bring up the new window but how do I then switch to it, so I can login and continue?
To make it more challenging the other window has an empty title, i.e.
I can get the programmer to add a title but it would take time. Is there any way with/without that to identify and switch to the other window?
I'm assuming you are using Selenium IDE. So from the Selenium Reference
selectPopUp ( windowID )
Simplifies the process of selecting a popup
window (and does not offer functionality beyond what selectWindow()
already provides).
If windowID is either not specified, or specified
as "null", the first non-top window is selected. The top window is the
one that would be selected by selectWindow() without providing a
windowID . This should not be used when more than one popup window is
in play.
Otherwise, the window will be looked up considering windowID
as the following in order: 1) the "name" of the window, as specified
to window.open(); 2) a javascript variable which is a reference to a
window; and 3) the title of the window. This is the same ordered
lookup performed by selectWindow .
selectWindow ( windowID )
Selects a popup window using a window
locator; once a popup window has been selected, all commands go to
that window. To select the main window again, use null as the target.
Window locators provide different ways of specifying the window
object: by title, by internal JavaScript "name," or by JavaScript
variable.
title=My Special Window: Finds the window using the text that appears
in the title bar. Be careful; two windows can share the same title. If
that happens, this locator will just pick one.
name=myWindow: Finds
the window using its internal JavaScript "name" property. This is the
second parameter "windowName" passed to the JavaScript method
window.open(url, windowName, windowFeatures, replaceFlag) (which
Selenium intercepts).
var=variableName: Some pop-up windows are
unnamed (anonymous), but are associated with a JavaScript variable
name in the current application window, e.g. "window.foo =
window.open(url);". In those cases, you can open the window using
"var=foo".
selectWindow would be ideal if you can retrieve the name of the new window that is opened.
If you're having trouble figuring out the name of a window that you
want to manipulate, look at the Selenium log messages which identify
the names of windows created via window.open (and therefore
intercepted by Selenium). You will see messages like the following for
each window as it is opened:
debug: window.open call intercepted; window ID (which you can use with
selectWindow()) is "myNewWindow"
In some cases, Selenium will be unable to intercept a call to
window.open (if the call occurs during or before the "onLoad" event,
for example). (This is bug SEL-339.) In those cases, you can force
Selenium to notice the open window's name by using the Selenium
openWindow command, using an empty (blank) url, like this:
openWindow("", "myFunnyWindow").
You can use the windowhandle to switch to the new window.
Something sort of..
Webdriver driver = new FirefoxDriver();
driver.get // Go to ur login page
driver.click //Click on link which launches new window
Set<String> s = driver.getwindowhandles() //this will return all open windows
driver.switchTo.window(s[1]); //will switch to second window
Hope it helps..
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;
}