QaMantra: How to Switch window in Selenium

How to Switch window in Selenium

Few Web application may have multiple windows,Tab & frames. While Navigating to Multiple window’s in selenium webdriver assigns an alphanumeric id to each window as soon as
the WebDriver object is instantiated ,id called as a window’s handler means each unique window has a unique ID, so that Selenium can able to differentiate while switching onee window to another .

                       Once the user switch to new(child) window driver control will also switch to new(child) window and he would not able to perform any operation on parent window until unless switch to parent window.


GetWindowHandle 

To Handle the current one window only

Syntax 


//Return a string of alphanumeric window handle                                           String  handle= driver.getWindowHandle();

Switch Current Window

From Above Mentioned Syntax we will get Current one Window Handler and based on that one Handler Script will switch window .


Syntax 

 driver.switchTo().window(handle);

GetWindowHandles 

To Handle the current All windows

Syntax 


//Return a set of All window handle                                                           Set<String>= driver.getWindowHandles();
Switch Current Window

From Above Mentioned Syntax we will get Current Multiple Window Handlers and based on that Handlers Script will switch to required  window . we have multiple window handler so we are going to use iterator ,through iterator we will iterate to each window and get handler id that window & which ever window want to switch use that handler id .

Syntax 

for (String Child_window: driver.getWindowHandles()) {
 
    driver.switchTo().window(Child_window);}
Program

Program is written for Multiple window handling ,In this program Scenario is user land on a web page ,there is a multiple Link and if user will click to any one of link will navigate to new window .



package PackageOne;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class irctcPopUp extends BaseClass {

 @Test

 public void MultipleWindowHandle() throws InterruptedException {

  System.setProperty("webdriver.chrome.driver", ".\\Exe\\chromedriver.exe");
  WebDriver driver = new ChromeDriver();

  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  // Launch QaMantra Web Page
  driver.get("https://www.Qamantra.com/");

  Thread.sleep(5000);
// Application Specific , not required for Every Application 
  EnterSubscriberEmail();
  // Navigate to Contact Web Page
  driver.findElement(By.xpath("//a[contains(text(),'Contact')]")).click();
// Application Specific , not required for Every Application
  EnterSubscriberEmail();
  // Click to QaMantraFaebookLink
  driver.findElement(By.xpath("//a[contains(text(),'FacebookQaMantraPage')]")).click();

 // get current window ID called as a parent window
  String Parent_Window = driver.getWindowHandle();

  // Switching from parent window to child window
  for (String Child_Window : driver.getWindowHandles()) {
   
  // switch to new window which is opened after click on //FacebookQaMantraPage Link
 // Now user can perform any operation on new window   
   driver.switchTo().window(Child_Window);
   
   // if user is Already Login to Facebook //Page ,Will Navigate to QaMnatra Page
   // and will hit like button 
   driver.findElement(By.xpath("//a[@data-testid='page_profile_liked_button_test_id']")).click();
   
   //Navigate back to Parent window
   driver.switchTo().window(Parent_Window);
  }

 }
 // to Handle Subsriber Email POPUp on qamantra Page
 // Its Application Specific 
 
 public void EnterSubscriberEmail(){
  driver.findElement(By.xpath("//input[@placeholder='Email address']")).sendKeys("yorEmailID");
  driver.findElement(By.xpath("//span[contains(text(),'Subscribe')]")).click();
 }
}

No comments: