Handling Multiple Windows in Selenium WebDriver

package multiplewindows;

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class MultipleWins 
{
	@Test
	public void TestPopUp() throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver","E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.manage().window().maximize();
		
		driver.get("http://www.naukri.com");
		
		String parent = driver.getWindowHandle();
		
		Set<String>s = driver.getWindowHandles();
		
		Iterator<String> Iter = s.iterator();
		
		while(Iter.hasNext())
		{
			String childWindow=Iter.next();
			
			if(!parent.equals(childWindow))
			{
				driver.switchTo().window(childWindow);
				
				String childWinName = driver.switchTo().window(childWindow).getTitle();
				
				System.out.println("Child Window Title: ----"+childWinName+"\n");
				
				driver.close();
			}
		}
		 
		driver.switchTo().window(parent);
		
		driver.close();
		 
	}
}

 

Handle Windows Authentication Popup using Selenium Webdriver

package com.zedo.Testing;

import java.io.IOException;

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

public class AutoITScriptHandler {

	public static void main(String[] args) throws IOException 
	{
		WebDriver driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		Runtime.getRuntime().exec("D:\\Scripts\\AutoITScript\\HandleAuthentication.exe");
		
		driver.get("https://www.engprod-charter.net/");

	}

}

AutoITScript:

WinWaitActive("Authentication Required")
Send("validusername")
Send("{TAB}")
Send("validpassword")
Send("{ENTER}")

 

Read data from CSV using Selenium Webdriver

package com.zedo.Testing;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class ReadfromCSV {

public static void main(String[] args) throws Exception {
 
CSVReader reader = new CSVReader(new FileReader("D:\\Scripts\\test.csv"));

List<String[]> li=reader.readAll();

System.out.println("Total rows which we have is "+li.size());

Iterator<String[]>i1= li.iterator();

while(i1.hasNext())
{
	String[] str=i1.next();
	
	System.out.print(" Values are: ");
	
	for(int i=0;i<str.length;i++)
	{
		System.out.print(" "+str[i]);
	}
	
	System.out.println("   ");
}

}

}

 

Downloaded Files stored in specific folder in Selenium Webdriver

package webdriver;

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

public class DownloadFile {
	
	static WebDriver driver;
	
	public static String downloadPath = "E:\\Selenium\\DownFiles";

	public static void main(String[] args) throws Exception 
	{		
	
        driver = new FirefoxDriver(FirefoxDriverProfile());
		
	driver.manage().window().maximize();
		
	driver.get("http://spreadsheetpage.com/index.php/file");
		
	driver.findElement(By.linkText("xllightbox.xls")).click();
		
	driver.quit();

	}
	public static FirefoxProfile FirefoxDriverProfile() throws Exception 
	{
        FirefoxProfile profile = new FirefoxProfile();
        
        profile.setPreference("browser.download.folderList", 2);
        
        profile.setPreference("browser.download.manager.showWhenStarting",false);
        
        profile.setPreference("browser.download.dir", downloadPath);
        
        profile.setPreference("browser.helperApps.neverAsk.openFile","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        
        profile.setPreference("browser.helperApps.alwaysAsk.force",false);
        
        profile.setPreference("browser.download.manager.alertOnEXEOpen",false);
        
        profile.setPreference("browser.download.manager.focusWhenStarting",false);
        
        profile.setPreference("browser.download.manager.useWindow",false);
        
        profile.setPreference("browser.download.manager.showAlertOnComplete",false);
        
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        
        return profile;
    }
	
}

 

Print all options of the dropdown using Selenium Webdriver

package webdriver;

import java.util.List;

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.support.ui.Select;

public class PrintAllfromDropDown {
	
	static WebDriver driver;

	public static void main(String[] args) {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://www.facebook.com");
		
		WebElement element = driver.findElement(By.id("month"));
		
        Select opt = new Select(element);        
        
        List<WebElement> allOptions = opt.getOptions();
        
        System.out.println("Total Options are --" +allOptions.size());
      
        for (int i=0; i<allOptions.size(); i++)
        {        	
            String optionValue = allOptions.get(i).getText();
            
            System.out.println(optionValue);
        }
		driver.quit();

	}

}

 

PageLoadTimeOut in Selenium

package webdriver;

import java.util.concurrent.TimeUnit;

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

public class PageLoad {
	
	static WebDriver driver;

	public static void main(String[] args) {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://facebook.com");
		
		driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
		
		driver.quit();
		
	}

}

The above code will verify the page is getting load with expected time(5 Sec) or not.  If Page will not load completely within the given time then selenium will throw the TimeoutException in the console.

1 2 3 5