Showing posts with label How to handle Multiple windows ?. Show all posts
Showing posts with label How to handle Multiple windows ?. Show all posts

Saturday, June 21, 2014

How to handle multiples windows if they open at different places not all together.

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Naukari {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.naukri.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //Click on Employer's Zone
        driver.findElement(By.xpath(".//*[@id='empNavM']/ul/li[1]/a")).click();
       
        switchControlToLatestWindow(driver); //switch control to Employer's Zone window
   
        driver.findElement(By.xpath("//div[@class='headbg']//a[text()='Report a Problem']")).click(); //click on 'Report a Problem'
        //this will again open a new window
        switchControlToLatestWindow(driver); //switch control to 'Report a Problem' window
       
        driver.findElement(By.xpath("//input[@name='strName']")).sendKeys("jdhjhgdjhgj1222"); //pass some value to Your name field to confirm our control is there
}
    //this is a method to switch the control to the latest opened window
    public static void switchControlToLatestWindow(WebDriver driver){
        Iterator<String> browsers = driver.getWindowHandles().iterator();
        while(browsers.hasNext()){
            driver.switchTo().window(browsers.next());
        }
    }

}

Tuesday, May 27, 2014

How to handle Multiple windows ?

package SeleniumMakeItEasy;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Naukari {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.naukri.com/");
        Iterator<String> in = driver.getWindowHandles().iterator();
        String mainPage = in.next();
        String child1 = in.next();
        String child2 = in.next();
        driver.switchTo().window(child1); //switch control to 1st child window
        //here you can perform any operation on child1 window if u want
        driver.close(); //close 1st child window
        driver.switchTo().window(child2); //switch control to 2nd child window
        //here you can perform any operation on child2 window if u want
        driver.close(); //close 2nd child window
        driver.switchTo().window(mainPage); //switch control to main page of naukari.com
    }
}