Different Types Of Pop Up in Selenium Web Driver.
How To Handle Multiple Windows Navigation Using Web Driver....
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class WinowHandle {
public static void main(String[] args) throws InterruptedException {
// login to App
WebDriver driver=new FirefoxDriver();
driver.get("http://piyush-pc/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// navigate to "time track" page
driver.findElement(By.linkText("Time-Track")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// navigate to "Add task" page
Select sel = new Select(driver.findElement(By.name("selectedUser")));
sel.selectByIndex(1);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// when action causes on "Add tasks" link , which opens new window
driver.findElement(By.linkText("Add tasks to the list")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//get all windows id using getWindowHandles() mtds
Set<String> set = driver.getWindowHandles();
//capture window id from Collection "Set" using iterator
Iterator<String> it = set.iterator();
// get parent and child window Id , & store it in string variable
String parentWindowId = it.next();
String childWindowId = it.next();
//display all window id's
System.out.println(parentWindowId);
System.out.println(childWindowId);
//pass driver control to child window
driver.switchTo().window(childWindowId);
//perform an operation on child window
driver.findElement(By.xpath("//input[@value='Show Tasks']")).click();
//close child window
driver.close();
// pass control back to parent window
driver.switchTo().window(parentWindowId);
//perform an operation on parent window
driver.findElement(By.linkText("Reports")).click();
}
}
How To Handle Alert pop up Using Web Driver....
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AlertHandling {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://piyush-pc/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//navigate add Projects & Customers page
driver.findElement(By.linkText("Projects & Customers")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//navigate to customer details page
driver.findElement(By.linkText("sk11")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// click on delete customer , which alert popUP
driver.findElement(By.xpath("//input[@value='Delete This Customer']")).click();
//pass driver control to alert
Alert alt = driver.switchTo().alert();
// perform an operation on alert
System.out.println(alt.getText());
//click on "Cancel" button
//alt.dismiss();
//click on "OK" button
alt.accept();
}
How To Handle Invisible pop up Using Web Driver....
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class InvisiblePopupExample {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();//launch browser
driver.get("http://piyush-pc/login.do");//navigate to actiTime login page
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.linkText("Projects & Customers")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@style='width: 108pt;']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("name")).sendKeys("xyz");
driver.findElement(By.linkText("Reports")).click();
boolean status=driver.findElement(By.xpath("//input[@id='RemainOnThePageButton']")).isEnabled();
if(status)
{
Thread.sleep(3000);
driver.findElement(By.xpath("//input[@id='RemainOnThePageButton']")).click();
}
else {
System.out.println("Invisible pop-up not displayed");
}
}
}
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class WinowHandle {
public static void main(String[] args) throws InterruptedException {
// login to App
WebDriver driver=new FirefoxDriver();
driver.get("http://piyush-pc/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// navigate to "time track" page
driver.findElement(By.linkText("Time-Track")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// navigate to "Add task" page
Select sel = new Select(driver.findElement(By.name("selectedUser")));
sel.selectByIndex(1);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// when action causes on "Add tasks" link , which opens new window
driver.findElement(By.linkText("Add tasks to the list")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//get all windows id using getWindowHandles() mtds
Set<String> set = driver.getWindowHandles();
//capture window id from Collection "Set" using iterator
Iterator<String> it = set.iterator();
// get parent and child window Id , & store it in string variable
String parentWindowId = it.next();
String childWindowId = it.next();
//display all window id's
System.out.println(parentWindowId);
System.out.println(childWindowId);
//pass driver control to child window
driver.switchTo().window(childWindowId);
//perform an operation on child window
driver.findElement(By.xpath("//input[@value='Show Tasks']")).click();
//close child window
driver.close();
// pass control back to parent window
driver.switchTo().window(parentWindowId);
//perform an operation on parent window
driver.findElement(By.linkText("Reports")).click();
}
}
How To Handle Alert pop up Using Web Driver....
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AlertHandling {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://piyush-pc/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//navigate add Projects & Customers page
driver.findElement(By.linkText("Projects & Customers")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//navigate to customer details page
driver.findElement(By.linkText("sk11")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// click on delete customer , which alert popUP
driver.findElement(By.xpath("//input[@value='Delete This Customer']")).click();
//pass driver control to alert
Alert alt = driver.switchTo().alert();
// perform an operation on alert
System.out.println(alt.getText());
//click on "Cancel" button
//alt.dismiss();
//click on "OK" button
alt.accept();
}
How To Handle Invisible pop up Using Web Driver....
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class InvisiblePopupExample {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();//launch browser
driver.get("http://piyush-pc/login.do");//navigate to actiTime login page
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.linkText("Projects & Customers")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@style='width: 108pt;']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("name")).sendKeys("xyz");
driver.findElement(By.linkText("Reports")).click();
boolean status=driver.findElement(By.xpath("//input[@id='RemainOnThePageButton']")).isEnabled();
if(status)
{
Thread.sleep(3000);
driver.findElement(By.xpath("//input[@id='RemainOnThePageButton']")).click();
}
else {
System.out.println("Invisible pop-up not displayed");
}
}
}