Automation QA Testing Course Content

How to handle WebPopups/tabs/new Windows in Selenium Webdriver?

click on something to get the another window/popup
//fetch all the window ids
Set<String>handles=driver.getWindowHandles();

//using Iterator interface
Iterator it=handles.iterator();
//fetchign main window id
String pid=(String)it.next();
//fetching the child window id
String chwinid=(String)it.next();
//switch to the child window id
driver.switchTo().window(windowref);
driver.switchTo().window(chwinid);
//perform the actions on the child window;
//close the child window
driver.close();
//switch back the cursor to parent window from childwindow
driver.switchTo().window(pid);
-----------------------------------------------------------------
2nd Way:based on the window title
//fetch the parent window id
String pid=driver.getWindowHandle();
click on something to get the another window/popup

//fetch all the window ids
Set<String>handles=driver.getWindowHandles();

for(String h:handles){
//if(!pid.equal(h){
if(driver.switchTo().window(h).getTitle().equals("windowtitle")){
//switch to that window
driver.switchTo().window(h);
//perform the actions on the window
} //end the if
 }//for loop end

 //close the child browser
 driver.close();

 //switch to parent window from child window
 driver.switchTo().window(pid);
 ============================================
 //using iterator while loop
 String pid=driver.getWindowhandle();
 //fetch all window ids
 Set<String>handles=driver.getWindowHandles();
 Iterator it=handles.iterator();
 while(it.hasNext()){

 if(!pid.equals(it.next())){
 //switch to child window
 driver.switchTo().window(it.next());
 close the child window
 driver.close();
 }
 }
 //switch to the parent window
 driver.switchTo().window(pid);

(OR)
for(String h:handles) {
if(!pid.equals(h)) {
driver.switchTo().window(h);
driver.close();
}
}

//switch back to parent window
driver.switchTo().window(pid);
============================================
There are 5 windows and I need to switch to 3rd window 
and verify the checkout button in the 3rd window and close the 3rd window
 and then come back to parent window 
 and type something in the parent window?

step1: fetch all the window ids
String pid=driver.getWindowHandle();
Set<String>handles=driver.getWindowHandles();

//convert the Set collection into List
List<String>windowsList=new ArrayList(handles);
step2-switch to 3rd window
driver.switchTo().window(windowsList.get(2));
WebElement checkoutBtn=driver.findElement(By.id("checkout"));
Assert.assertTrue(checkoutBtn.isDisplayed));
//close the child window
driver.close();
//swithc back to parent window.
driver.switchTo().window(windowsList.get(0));
driver.switchTo().defaultContent();
//now do action on prent page searcheditbox /log0 
driver.findElement(By.id("searchEditboix")).sendKeys("selenium");
================================================================
i have a login page clicked terms and condiitons link window click close button and 
licence agreement window accept it then come back to main window then type credentials
step1:fetch the parent window
String pid=driver.getWindowHandle();
click on terms & conditions link
driver.findElement(By.linkText("terms & conditions")).click();
click on license agreement link
driver.findElement(By.linkText("license agreement")).click();
Step4:fetch all window ids
Set<String>handles=driver.getWindowHandles();
for(String h:handles){
if(!pid.equals(h)){
if(driver.switchTo.window(h).getTitle().equals("terms & conditions")){
//switch to Terms & Conditions window 
driver.switchTo.window(h);
//click on close button
driver.findElement(By.xpath("//*[text()='close']")).click();
}else if(driver.switchTo.window(h).getTitle().equals("LicenseAgreement")){
//switch to LicenseAgreement window 
driver.switchTo.window(h);
//Accept the licence agreement radio button
driver.findElement(By.xpath("//*[text()='close']")).click();

}
//close the child window
driver.close();
}
}
//switch back to parent window.
driver.switchTo().window(pid);
//type the username.
driver.findElement(By.id("username")).sendkeys("rameshn3");
//type the password in password editbox
driver.findElement(By.name("password")).sendkeys(""test123);
//click on login button.
driver.findElement(By.xpath("//*[@type='submit']")).click();

================================================================
SELENIUM4 FEATURE OPENING NEW TAB AND NEW WINDOW  PROGRAM

package junitprograms;

import static org.junit.jupiter.api.Assertions.*;

import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

class Selenium4OpeninginNewTabAndNewWindow {
private static WebDriver driver = null;
private static WebDriverWait wait = null;
String pid=null;

@BeforeAll
static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome99\\chromedriver_win32\\chromedriver.exe");

// interface variablename=new classname();
driver = new ChromeDriver();

// maximize the window
driver.manage().window().maximize();

// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

// create WebDriverWaitobject
wait = new WebDriverWait(driver, Duration.ofMillis(30000));

// launch the application url
driver.get("http://the-internet.herokuapp.com/");
// wait for the page title
wait.until(ExpectedConditions.titleContains("The Internet"));
Assertions.assertEquals("The Internet", driver.getTitle());
Assertions.assertTrue(driver.getTitle().contains("The Internet"));
// verify the page heading
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1.heading")));
String headingTxt = driver.findElement(By.cssSelector("h1.heading")).getText();
Assertions.assertEquals("Welcome to the-internet", headingTxt);
// Click on Multiple Windows link
driver.findElement(By.partialLinkText("Multiple Windows")).click();

}

@AfterAll
static void tearDownAfterClass() throws Exception {
if (driver != null) {
driver.quit();
}
}

@Test
void testOpenNewTab() throws InterruptedException {
System.out.println(" testOpenNewTab()::::");
// Verify the login page Addressbar url and page heading
wait.until(ExpectedConditions.urlContains("/windows"));
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div[class='example']>h3")));
String windowheadertxt = driver.findElement(By.cssSelector("div[class='example']>h3")).getText();
Assertions.assertEquals("Opening a new window", windowheadertxt);
   pid=driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
Thread.sleep(3000);
driver.navigate().to("https://linkedin.com");
Thread.sleep(3000);
//close the tab
driver.close();
//switch back to parentwindow
driver.switchTo().window(pid);
driver.switchTo().defaultContent();
String windowheadertxt1 = driver.findElement(By.cssSelector("div[class='example']>h3")).getText();
Assertions.assertEquals("Opening a new window", windowheadertxt1);
Thread.sleep(3000);
}

@Test
void testOpenNewWindow() throws InterruptedException {
System.out.println(" testOpenNewWindow()::::");
// Verify the login page Addressbar url and page heading
wait.until(ExpectedConditions.urlContains("/windows"));
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div[class='example']>h3")));
String windowheadertxt = driver.findElement(By.cssSelector("div[class='example']>h3")).getText();
Assertions.assertEquals("Opening a new window", windowheadertxt);
pid=driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.WINDOW);
Thread.sleep(3000);
driver.navigate().to("https://facebook.com");
Thread.sleep(3000);
//close the tab
driver.close();
//switch back to parentwindow
driver.switchTo().window(pid);
driver.switchTo().defaultContent();
String windowheadertxt1 = driver.findElement(By.cssSelector("div[class='example']>h3")).getText();
Assertions.assertEquals("Opening a new window", windowheadertxt1);
Thread.sleep(3000);
}
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.