banner



How To Switch To New Window In Selenium

Software testing plays a vital role in creating fault-gratuitous products that offer seamless user experiences. Therefore, information technology is essential to examination the software before putting it into product.

Every bit engineering science advances, it is necessary to adopt automation testing into the evolution pipeline. Selenium is the most widely used framework for automated software testing of a website. Every website must exist tested by putting it through multiple real-globe user scenarios. One such scenario is the handling of multiple windows. This article will discuss how to automate this action with Selenium and so that the website's behavior tin can exist monitored by testers.

Handling Multiple Windows in Selenium

The user scenario being automated here is: Open a new tab and then switch back to the final window to consummate the other pending activities. In such scenarios, Selenium helps to handle multiple windows through window handlers and javascript executors.

What is a window handle?

It is a unique identifier that holds the address of all the windows. Think of information technology every bit a pointer to a window, which returns the cord value. It is assumed that each browser volition take a unique window handle. This window handle part helps to call back the handles of all windows.

Syntax

  1. become.windowhandle(): This method helps to get the window handle of the current window
  2. get.windowhandles(): This method helps to get the handles of all the windows opened
  3. set up: This method helps to set the window handles in the course of a cord. gear up<string> set up= driver.get.windowhandles()
  4. switch to: This method helps to switch betwixt the windows
  5. activity: This method helps to perform certain deportment on the windows

These are some of the methods that will be used to handle multiple windows in Selenium.


People besides read: How to handle Alerts and Popups in Selenium?


Example of treatment multiple windows

Scenario: Navigate to the Browserstack domicile page. This is the parent window. From the parent window, let's encounter how to handle the child windows and then again navigate back to the parent windows.

Steps to execute:

  1. Go the handle of the parent window using the command: String parentWindowHandle = driver.getWindowHandle();
  2. Print the window handle of the parent window.
  3. Find the chemical element on the web page using an ID which is an element locator.
  4. Open up multiple kid windows.
  5. Iterate through child windows.
  6. Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver.getWindowHandles(); which returns the set of handles.
  7. Use the SwitchTo command to switch to the desired window and likewise pass the URL of the web folio.

Refer to the complete programme beneath.

Before running the code, ane should exercise a quick bank check on 6 things to avoid while running selenium scrips. Check information technology out.

import java.util.Iterator;  import java.util.Prepare;  import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;  public class WindowHandle_Demo {  public static void chief(String[] args) throws Exception {   Organization.setProperty("webdriver.chrome.commuter","Path to the driver");  WebDriver commuter = new ChromeDriver();  driver.manage().window().maximize();  // Load the website driver.go("http://www.naukri.com/");  // It will render the parent window name as a String String parent=driver.getWindowHandle();  Set<String>southward=commuter.getWindowHandles();  // Now iterate using Iterator Iterator<String> I1= s.iterator();  while(I1.hasNext()) {  String child_window=I1.next();   if(!parent.equals(child_window)) { commuter.switchTo().window(child_window);  Organisation.out.println(driver.switchTo().window(child_window).getTitle());  driver.close(); }  } //switch to the parent window commuter.switchTo().window(parent);  } }

Output:

Handling multiple windows in Selenium

On executing the parent window handle, it will open multiple kid windows and then navigate dorsum to the final window handle.

Run Selenium Tests on Real Device Cloud for Gratuitous

Now let'south perform some actions on the Browserstack website.

  • Use the javascriptexecutor to roll down through a page.
  • Find the element using XPath and send keys (which is of the form string) to that particular chemical element location.
  • Declare the web chemical element Link to click on a particular link on the folio. In this case, the link must open in a new window.
  • Get the window handles of all the windows and impress them in a sequential manner.
  • Switch to the parent window and bank check if the title matches. If it does, scroll down the folio using the javascriptexecutor.
  • Notice another element on the web page using the element locator and specify the position of the new window.
  • Switch back to the parent window and ringlet down through the page.

Pro Tip: Want to dive deeper into Selenium implementation on BrowserStack with free interactive courses and lab exercises? Visit Test University


Code Snippet

import java.util.Prepare;  import org.openqa.selenium.Past; import org.openqa.selenium.JavascriptExecutor;  import org.openqa.selenium.Keys;  import org.openqa.selenium.Point;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.WebElement;  import org.openqa.selenium.chrome.ChromeDriver;  import org.openqa.selenium.interactions.Actions;   public grade selenium { public static void master(String[] args) throws Exception  {  System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");  WebDriver driver = new ChromeDriver();  driver.get("https://www.browserstack.com/");  String championship = driver.getTitle(); Organisation.out.println(title);  JavascriptExecutor js = (JavascriptExecutor) commuter;  driver.findElement(By.xpath("//span[contains(text(),'Solutions')]")).click(); driver.findElement(By.xpath("//a[contains(text(),'Geolocation Testing')]")).click(); js.executeScript("window.scrollBy(0,40)");   WebElement link = commuter.findElement(By.xpath("//a[@id='product-carte-toggle']//span[@class='account-down-caret']//*[local-name()='svg']"));  Actions newwin = new Actions(driver);  newwin.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();  //Thread.sleep(2000);  //js.executeScript("window.scrollBy(0,400)");  Thread.slumber(3000);  Set<String> windows = driver.getWindowHandles();  Arrangement.out.println(windows);  Organization.out.println("a1");  for (String window : windows)  {  driver.switchTo().window(window);  if (driver.getTitle().contains("About Reliable App & Cantankerous Browser Testing Platform | Browserstack"))  {  Organisation.out.println("a2");  js.executeScript("window.scrollBy(0,g)");  Organization.out.println("b1"); driver.findElement(Past.xpath("//a[@id='logo']//*[local-name()='svg']")).click();  driver.findElement(By.xpath("//a[@id='signupModalButton']")).click();  driver.manage().window().setPosition(new Point(2000, 0));  }  }  Thread.sleep(3000);  Set<String> windows1 = driver.getWindowHandles();  System.out.println(windows1);  System.out.println("a3");  for (String window : windows1)  {  driver.switchTo().window(window);  Organisation.out.println("a4");  js.executeScript("window.scrollBy(0,400)");  }  }  }

On executing the code above, it will launch multiple windows and the window handle will exist retrieved. Run the lawmaking, automate user navigation through multiple windows, and ensure the website works perfectly in existent user weather. With Selenium WebDriver, ensure that websites offer an optimal user experience in all possible circumstances.

Source: https://www.browserstack.com/guide/handle-multiple-windows-in-selenium

Posted by: pickenselly1966.blogspot.com

0 Response to "How To Switch To New Window In Selenium"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel