Verify popup window with selenium webdriver

selenium

To verify popup window we use the following code –

package com.webdriver_13;

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

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.ErrorHandler.UnknownServerException;

public class MyVerification_03 {
	
	public WebDriver driver;
	private String baseUrl;
	private String parent_window;
	private String popup_window;
	
	public static void main(String[] args) {
		MyVerification_03 mv = new MyVerification_03();
		mv.launch_browser();
	}
	public void launch_browser() {
		try{
		baseUrl = "http://demo.tanmaysarkar.com/sample_03.html";		
		driver = new FirefoxDriver();
		driver.get(baseUrl);
		Set windowids = driver.getWindowHandles();
		Iterator iter= windowids.iterator();
        parent_window= iter.next().toString();
		
        driver.findElement(By.xpath("//input[contains(@value,'Submit')]")).click();
		
        windowids = driver.getWindowHandles();
        iter= windowids.iterator();
        iter.next();
        popup_window=iter.next().toString();
        System.out.println("Parent " + parent_window);
        System.out.println("Popup " +popup_window);
         
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 		
        driver.switchTo().window(popup_window);
        System.out.println("able to switch");
      
        System.out.println("Popup window contains - "+driver.findElement(By.xpath("//p")).getText());
        driver.switchTo().window(popup_window).close();
 		driver.switchTo().window(parent_window);
        System.out.println("now switch to default"); 
        System.out.println("Parent Footer Text is : " + driver.findElement(By.xpath(".//*[@id='footer']")).getText());
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.close();
		}
		catch(NoSuchElementException ne)
		{
			System.out.println(ne.getLocalizedMessage());
		}
		catch(UnknownServerException u)
		{
			System.out.println(u.getLocalizedMessage());
		}
	}
}

and the output should be –

Parent {a3e8ddf8-0462-4f31-b92c-fb4faf8f7b90}
Popup {62fb6e63-e86c-4ad7-b3c2-41fbfa09f822}
able to switch
Popup window contains – Hello Popup world! ; )
now switch to default
Parent Footer Text is : Sample by – Tanmay Sarkar

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *