Selenium - how to switch to a different (login) wi

2019-06-07 20:27发布

问题:

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?

回答1:

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



回答2:

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