Selenium Tips: Working with Multiple Windows
Sauce AI for Test Authoring: Move from intent to execution in mo.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in mo.
|
x
One common Selenium issue is dealing with popup windows. You might likewise want to make your own new windows to do something in a divided session. For example: To make a new account, the pop rearward to the original page to see that the javascript has updated to ‘ see ’ this new ‘ friend ’ appear on a friends list.
For multiple window tasks, Selenium IDE provides the undermentioned commands:
openWindow (url, windowId): Will open your new window
waitForPopup (windowId, timeout): Will expect for the window to be open and loaded
selectwindow (windowId): Will focus on this window and any future actions will act on it
We could, then make the above exam usingSelenium IDE to run on http: //google.com:
open | / | |
type | q | sauce laboratory |
click | btnG | |
waitForTextPresent | Sauce Labs | |
openWindow SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses. | /search? q=santiago | santiago |
waitForPopUp | santiago | 30000 |
selectWindow | santiago | |
assertTitle | santiago - Google Search | |
selectWindow | null | |
assertTitle | sauce labs - Google Search |
However, the IDE won & # x27; t be able to play back that test as compose. Let ’ s looking at some codification that does the same thing with webdriver3, create in Java and subject of lead onSauce OnDemand.
System.setProperty (& quot; webdriver.chrome.driver & quot;, & quot; ./chromedriver & quot;);
WebDriver driver = new org.openqa.selenium.chrome.ChromeDriver ();
// And now use this to visit Google
driver.get (& quot; https: //www.google.com/ & quot;);
driver.findElement (By.id (& quot; gb & quot;)) .click ();
driver.findElement (By.name (& quot; q & quot;)) .click ();
driver.findElement (By.name (& quot; q & quot;)) .clear ();
driver.findElement (By.name (& quot; q & quot;)) .sendKeys (& quot; sauce labs & quot;);
driver.findElement (By.name (& quot; q & quot;)) .sendKeys (Keys.ENTER);
((JavascriptExecutor) driver) .executeScript (& quot; window.open (& # x27; https: //www.google.com/search? q=Santiago & # x27;) & quot;);
Set & lt; String & gt; plow = driver.getWindowHandles ();
String firstWindowHandle = driver.getWindowHandle ();
handles.remove (firstWindowHandle);
driver.switchTo () .window (firstWindowHandle);
System.out.println (& quot; Page title is: & quot; + driver.getTitle ());
String secondWindowHandle = handles.iterator () .next ();
driver.switchTo () .window (secondWindowHandle);
System.out.println (& quot; Page rubric is: & quot; + driver.getTitle ());
driver.quit ();
}
This slightly different code, express in java, opens the window and does the parallel work that you expected it to do. Good of all, the tip isavailable in git, along with all of the dependencies and the chassis and run scripts for linux and macintosh. Windows users may require to add a .bat extension or change the argument somewhat as expressed in the README.md, which also contains links in case the dependencies become out of escort.
Of course, you can think of webdriver as an API and write this in any language webdriver supports, like Ruby, C #, or node.js. The function names will be essentially the same, using the variable eccentric and capitalization/underscore appropriate for the lyric. You can also use the selectWindowHandle () syntax to grab popup windows.
Did you chance this tip useful? Need some help adapting this to your needs? Let us know in the comments! Until next time, happy testing!
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free