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();
		 
	}
}

 

Read data from excel file using Selenium Webdriver

package com.zedo.Testing;

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.Test;

public class TestRead {

@Test
public void TestReadData(){

File src=new File("D:\\Scripts\\myexceldata.xls");

try {

Workbook wb=Workbook.getWorkbook(src);

Sheet sh= wb.getSheet(0);

Cell c=sh.getCell(0,0);

String data=c.getContents();

System.out.println("Data contains: --- "+data);

} catch (BiffException e) {

e.printStackTrace();

} 
catch (IOException e)
{ 
e.printStackTrace();
}
}
}

 

Use Grouping in TestNG – Selenium Webdriver

package testng;

import org.testng.annotations.Test;

public class Grouping 
{
	@Test (groups={"regression"})
	public void testcase1()
	{
		System.out.println("It's Test case 1 and in regression group" +"\n");
	}
	@Test (groups={"unit"})
	public void testcase2()
	{
		System.out.println("It's Test case 2 and in unit group" +"\n");
	}
	@Test (groups={"regression"})
	public void testcase3()
	{
		System.out.println("It's Test case 3 and in regression group" +"\n");
	}

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Grouping Test Case Suite">
  <test name="grouping">
  	<groups>
  	<define name = "all">
  		<include name="regression"/>
  		<exclude name="unit"/>
  	</define>
      <run>
        <include name="all"/>
      </run>
    </groups>
    <classes>
       <class name="testng.Grouping" />
    </classes>
  </test>
</suite>

 

Using Priority Test Case in TestNG

Test Case: 1

package testng;

import org.testng.annotations.Test;

public class PriorityTestCase 
{
	@Test (priority=1)
	public void register()
	{
		System.out.println("Create an account in Gmail" +"\n");
	}
	@Test (priority=2)
	public void login()
	{
		System.out.println("Login into an account in Gmail" +"\n");
	}
	@Test (priority=3)
	public void logout()
	{
		System.out.println("Logout from Gmail account" +"\n");
	}

}

Output 1:

[TestNG] Running:  
  C:\Users\QA\AppData\Local\Temp\testng-eclipse-241244459\testng-customsuite.xml

Create an account in Gmail

Login into an account in Gmail

Logout from Gmail account

PASSED: register
PASSED: login
PASSED: logout

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

Test Case 2:

package testng;

import org.testng.annotations.Test;

public class PriorityTestCase 
{
	@Test (priority=3)
	public void register()
	{
		System.out.println("Create an account in Gmail" +"\n");
	}
	@Test (priority=2)
	public void login()
	{
		System.out.println("Login into an account in Gmail" +"\n");
	}
	@Test (priority=1)
	public void logout()
	{
		System.out.println("Logout from Gmail account" +"\n");
	}

}

Output 2:

[TestNG] Running:
  C:\Users\QA\AppData\Local\Temp\testng-eclipse--1702558414\testng-customsuite.xml

Logout from Gmail account

Login into an account in Gmail

Create an account in Gmail

PASSED: logout
PASSED: login
PASSED: register

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

 

Include and Exclude Methods in TestNG

package testng;

import org.testng.annotations.Test;

public class IncludeandExclude 
{
	@Test
	public void TestCase1()
	{
		System.out.println("This is my 1st test case."+"\n");
	}
	
	@Test
	public void TestCase2()
	{
		System.out.println("This is my 2nd test case."+"\n");
	}
	
	@Test
	public void TestCase3()
	{
		System.out.println("This is my 3rd test case."+"\n");
	}

}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Include and Exclude Test Suite" verbose="1" >
  <test name="Method Test Cases" >
    <classes>
       <class name="testng.IncludeandExclude">
        <methods>
        <include name="TestCase1" />
        <include name="TestCase2" />
        <exclude name="TestCase3" /> 
      </methods>
       </class>
    </classes>
  </test>
</suite>

Output

[TestNG] Running:
  E:\Selenium\Myscripts\testng.xml

This is my 1st test case.

This is my 2nd test case.


===============================================
Include and Exclude Test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

 

Passing Parameters through testng.xml

Test Case:

package testng;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class PassingParameters 
{
	@Parameters({"username", "password"})
	
	@Test
	public void login (String username, String password)
	{
		System.out.println("User name :- " + username +"\n");
		
		System.out.println("Password :- " + password +"\n");
	}	

}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Passing parameters Test Suite" >
  <test name="Parameterization Execution"  >
  <parameter name="username" value="selenium"/>
  <parameter name="password" value="testng123"/>
    <classes>
      <class name="testng.PassingParameters"/>      
    </classes>
  </test>
</suite>

Output:

[TestNG] Running:
  E:\Selenium\Myscripts\testng.xml

User name :- selenium

Password :- testng123


===============================================
Passing parameters Test Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

 

1 2 3 7