How to enter the credentials in a new window using Selenium? - selenium

I need to enter credentials in a new window opened using Selenium. I am using selectWindow command but the name of the window is dynamic. Please suggest how can I handle this.
Thanks

First get the main window handle (getWindowHandle()) before opening new window.
Using getWindowHandles() you can get all opened window handles.
Using above two things try to get newly opened window handle.
Refer below link for full logic .
http://santoshsarmajv.blogspot.in/2012/04/how-to-switch-control-to-pop-up-window.html

Related

Robot Framework verify a new browser tab was opened

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

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 - how to switch to a different (login) window that is brought up?

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..

Capturing a newly opened window with Selenium

I'm using the Selenium Client drivers to run tests built in C#. I'm having a problem where the test navigates to a page and clicks on a button in a form with its target set to _blank causing it to open a new window. However the new page being opened returns an XML document and not a typical webpage. Selenium seems to have trouble with this because it hangs when the button is click and the new window opens. No instructions after the click method are executed. The test eventually fails with the error Timed out running command.
Any help/guidance would be appreciated. I've scoured the net but haven't seen anyone running into this particular issue of the page being opened not being a typical webpage which I think is the core of the problem as Selenium can't really manipulate this new opened window. I would post code except that literally all I'm doing is calling the click method on a button which causes a new window to open. Thanks in advance.
To tackle the issue of opening in new window. I typically get url of the link which is to be opened and instead of using selenium.click(), I use:
selenium.open("http://yourwebsite/yourlink")
This helps me to be on the same window only(which avoids opening new window or tab) and when the testing is finished on this page and you want to switch back to original page you can again use:
selenium.open("http://whatever/")

How can we force selenium to focus on new tab?

One of my application after clicking continue button refreshes and opens in a new tab and closes the old tab that had continue button.
Is there anyway that we can select the new tab as the current window and move forward?
I used the following code which didn't work.
selenium.select("");
selenium.select(null);
Any help would be appreciated.
There is an additional option for selenium server. You can add -singlewindow parameter when you are starting selenium server. This parameter will force selenium to open all pages in the same frame. Please look here for more details: http://seleniumhq.org/docs/05_selenium_rc.html#server-options
Best Regards
Pawel