Showing posts with label Multiple windows. Show all posts
Showing posts with label 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());
        }
    }

}

Sunday, June 8, 2014

Suppose there are five Browser windows opened, how to find the third one and close it through web driver ?

1st get the address of all the browsers using

Iterator<String> windows = driver.getWndowHandles().iterator();

Now suppose you want to close the window whose title is "abc";

while(windows.next()){
         driver.switchTo().window(windows.next());
         String title = driver.getTitle();
         if(title.equals("abc")){
                driver.close();
                 break;
          }
}

or say if you want to close 3rd one by force and you don't know the expected title then

for(int i=0; i<3; i++){
         driver.switchTo().window(windows.next());
         if(i==2){
                driver.close();
                 break;
         }
}


or you can do the same -


for(int i=0; i<3; i++){
          String child = windows.next();
         if(i==2){
                 driver.switchTo().window(child);
                driver.close();
                 break;
         }
}

Tuesday, June 3, 2014

Switch to window then frame then again switch back to frame and window. (complex example)

Note- Please do the manual steps first and see how the flow going then try with the script.
Manual Steps-
1) open google
2) type in search box 'times of india'
3) click on 1st link of 'times of india'
4) click on 'click here to go to timesofindia.com'
5) click on 'Login' button (which is in the top just beside the LogIn with Facebook)
6) click on facebook
7) check the checkbox in facebook page and click on cancle
8) then close the frame (which has 3 options facebook, twitter and your email)
9) click on india in main page.
10) close the browser.

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 TimesOfIndia {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        driver.get("https://www.google.co.in/"); //open google
        driver.findElement(By.id("gbqfq")).sendKeys("times of india"); //type in search box 'times of india'
        driver.findElement(By.xpath("//span[text()='times of india']")).click(); //click on 1st link of 'times of india'
        driver.findElement(By.xpath("//div[div[div[div[cite[b[text()='indiatimes']]]]]]//a[text()='The ']")).click(); //click on 'click here to go to timesofindia.com'
        driver.findElement(By.linkText("Log In")).click(); //click on 'Login' button
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='signupsso']"))); //now switch to login frame which has 3 options facebook, twitter and your email
        driver.findElement(By.id("fb")).click(); //click on facebook which will open in new window
        Iterator<String> it = driver.getWindowHandles().iterator();// get the address of all the windows
        String times = it.next();
        String fb = it.next();
        driver.switchTo().window(fb); // switch to facebook window
        driver.findElement(By.xpath("//input[@type='checkbox']")).click(); //check the checkbox in facebook window
        driver.findElement(By.xpath("//input[@value='Cancel']")).click(); //click on cancle button in facebook window
        driver.switchTo().window(times); //switch back to main window
        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='signupsso']"))); //again switch to login frame which has 3 options facebook, twitter and your email
        Thread.sleep(2000);
        driver.findElement(By.xpath("//img[@src='/photo/11509366.cms']")).click(); // click on cross sign to close frame
        Thread.sleep(2000);
        driver.switchTo().defaultContent(); //switch back to main page
        driver.findElement(By.xpath("//div[ul[script[text()='sectionid=4719157']]]//a[@href='/india/indiaarticlelist/-2128936835.cms']")).click(); //click on india
        driver.close();
    }

}

switch to window then frame.

Note- This is to login for HDFC credit card.

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

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

public class HdfcBank {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("give your hdfc credit card userid: ");
        String userid = in.next();
        System.out.println("give your pass: ");
        String pass = in.next();
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.hdfcbank.com/");
        driver.findElement(By.xpath("//img[@src='/assets/images/login_butn.png']")).click();
        Iterator<String> browser = driver.getWindowHandles().iterator();
        String parent = browser.next();
        String child1 = browser.next();
     
        driver.switchTo().window(child1);
        driver.findElement(By.xpath("//img[@src='/assets/images/netbanking_continue_red.gif']")).click();
        driver.switchTo().frame(driver.findElement(By.xpath("//frame[@src='RSLogin.html']")));
        driver.findElement(By.xpath("//p[strong[text()='Credit Cardholders']]//a[text()='click here']")).click();
     
        Iterator<String> browser1 = driver.getWindowHandles().iterator();
        String child2=null;
        while(browser1.hasNext())
        {
             child2 = browser1.next();
        }
        driver.switchTo().window(child2);
        driver.findElement(By.xpath("//input[@name='fldLoginUserId']")).sendKeys(userid);
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys(pass);
        driver.findElement(By.xpath("//img[@alt='Log In']")).click();
    }

}

Saturday, May 31, 2014

How to handle multiple windows.

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MultipleWindows {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://selenium-makeiteasy.blogspot.in/");
       
        WebElement ele = driver.findElement(By.xpath("//a[text()='View my complete profile']"));
        Actions act = new Actions(driver);
        act.moveToElement(ele).contextClick().sendKeys("W").perform(); //this will open the link in new window
       
        Iterator<String> addWindow = driver.getWindowHandles().iterator(); //get the address of all opened windows
        String mainPage = addWindow.next(); //get the address of main page
        String childPage = addWindow.next();//get the address of child page
       
        driver.switchTo().window(childPage); //switch the control to child window
        driver.findElement(By.xpath("//a[text()='Automate the website']")).click(); //this will click in child window's interests- Automate the website link
        Thread.sleep(5000); //simply added wait for 5sec so that one can see the how it works in new window
        driver.close(); //this will close the child window
       
        driver.switchTo().window(mainPage); //switch back to main window
        List<WebElement> pageView = driver.findElements(By.xpath("//span[@id='Stats1_totalCount']/span"));//this will give the all the WebElements of the Total Pageviews numbers
        for(WebElement num: pageView){
            System.out.print(num.getText()); //this will print the number of Total Pageviews
        }
        driver.close(); //this will close main window as well
    }
}

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
    }
}