Wednesday 2 September 2015

How to perform switching between 3 windows

package com.multiplewindows;

import java.util.ArrayList;
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.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MultiWindows22 {

    WebDriver d;
    public String mainWindow;
    public String childWindow;
    public String child1;
    public String child2;
    @Test
    public void testscript() throws InterruptedException {
        d.get("http://www.bing.com/");
        //d.findElement(By.xpath(".//*[@id='msn']/a")).click(); 
        d.findElement(By.xpath(".//*[text()='MSN']")).click();
        //Thread.sleep(3000);
         //d.findElement(By.xpath(".//*[@id='office']/a")).click();
        //d.findElement(By.xpath(".//*[text()='Office Online']")).click();
        Thread.sleep(3000);
        mainWindow=d.getWindowHandle();
        Set<String> wids=d.getWindowHandles();
        Iterator i=wids.iterator();
        while (i.hasNext()) {
           
            String windows=(String)i.next();
            System.out.println("windows are"+windows);
             if (!windows.equalsIgnoreCase(mainWindow)) {
                 d.switchTo().window(windows);
                 Thread.sleep(2000);
                 childWindow=d.getWindowHandle();
                    Thread.sleep(2000);
                    d.findElement(By.id("q")).sendKeys("selenium");   
                    d.findElement(By.id("sb_form_go")).click();
                    Thread.sleep(6000);
                    break;   
            }
        }   
        Thread.sleep(5000);
        Set<String> wids1=d.getWindowHandles();
        for (String string : wids1) {

            if (!mainWindow.equalsIgnoreCase(string)&&!childWindow.equalsIgnoreCase(string)) {
                d.switchTo().window(string);
                Thread.sleep(6000);
                child1=d.getWindowHandle();
                d.findElement(By.xpath(".//*[@id='id_sc']")).click();
                Thread.sleep(6000);
                break;
            }
        }
        Thread.sleep(5000);
       
       
    }
   
    @BeforeClass
    public void beforeClass() {
        d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
    @AfterClass
    public void afterClass() {
        d.quit();
    }
   
}

No comments:

Post a Comment