1
public static void main(String[] args) throws InterruptedException 
{
    WebDriver driver=new FirefoxDriver();
    String Parentwindow=driver.getWindowHandle();
    Set <String>allWindows=driver.getWindowHandles();
    driver.get("http://toolsqa.com/automation-practice-switch-windows/");
    driver.findElement(By.id("button1")).click();
    Thread.sleep(3000);
    for(String str:allWindows)
    {
        System.out.println("all windows"+str);
        driver.switchTo().window(str);
        System.out.println("window title is"+driver.getTitle());
        Thread.sleep(3000);
        if(driver.getTitle().contains("Tutorial"))
        {
            System.out.println("scenario is passed");
        }                   
    }
    System.out.println("Failed");
}
2

You can use an iterator for switching from one window to other in Selenium script:

Set<?>windowHandles= driver.getWindowHandles();
Iterator<?> it = windowHandles.iterator();
String parentBrowser = (String) it.next();
String childBrowser = (String) it.next();

You can iterate or switch browser by this code:

driver.switchTo().window(parentBrowser);

Using the above code will switch the present window to another.

0

Your code is not appropriate in the way to switch into a window and get the time and compare with expected.

You have to follow below steps to achieve what you want to do

  1. Navigate to your site
  2. Store the current window
  3. Click on your button which opens new window
  4. Now get all windows currently available using Set<String> allWindows=driver.getWindowHandles();
  5. Loop through all window and compare weather it is expected

Use below code for the same :

public static void main(String[] args) throws InterruptedException 
{
    WebDriver driver=new FirefoxDriver(); 
    driver.manage().window().maximize();
    driver.get("http://toolsqa.com/automation-practice-switch-windows/"); 
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    String parentwindow=driver.getWindowHandle(); 
    driver.findElement(By.id("button1")).click();
    System.out.println(parentwindow);

    Set<String> allWindows=driver.getWindowHandles(); 

    System.out.println("Total Windows :"+ allWindows.size());


        for (String winHandle : driver.getWindowHandles()) 
        {
            if (!parentwindow.equalsIgnoreCase(winHandle))
            {
                driver.switchTo().window(winHandle); 
                if(driver.getTitle().contains("Tutorial"))
                {
                    System.out.println("scenario is passed");
                    break;
                }
                else
                {
                    System.out.println("scenario Failed");
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.