Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Annotations

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

TESTng :

The default annotation for testNG is @test

Test scenario :
Before Method
 Opening Browser
 Navigate to amazon page
 Max the window
F1 :
Title of the page
F2:
url of the page
f3:

 Capture all the dropdown Elements from


the list
 Search an element from the dropdown
F4:
 Capture the screenshot

 afterMethod
Close the browser

package DAY1;

import java.io.File;
import java.io.IOException;
import java.util.List;

import
org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;
import org.testng.annotations.Test;

public class Amazon {


WebDriver d;
@Test(priority=1)
public void Opening_Browser() {
System.setProperty("webdriver.chrome.
driver","C://chromedriver.exe");
d=new ChromeDriver();
d.get("http://www.amazon.in");
d.manage().window().maximize();
}
@Test(priority=2)
public void title()
{
System.out.println("Title of the
page is : "+d.getTitle());
}
@Test(priority=3)
public void url()
{
System.out.println("url of the
current page is :
"+d.getCurrentUrl());
}
@Test(priority=4)
public void Selecting()
{
WebElement
drop=d.findElement(By.id("searchDropd
ownBox"));
List<WebElement>drop1=drop.findElemen
ts(By.tagName("option"));
System.out.println(drop1.size());
for(int i=0;i<drop1.size();i++)
{
System.out.println(drop1.get(i).getTe
xt());
}

d.findElement(By.id("searchDropdownBo
x")).sendKeys("Books");
}
@Test(priority=5)
public void Screenshot() throws
IOException
{
File
src=((TakesScreenshot)d).getScreensho
tAs(OutputType.FILE);
FileUtils.copyFile(src, new
File("D://o11.png"));
}
@Test(priority=6)
public void Close()
{
d.close();
}
}

@beforemethod and @aftermethod


Login
Customer details
Product details
Payment details
Logout

For every functionality in the class, before


method will execute first then the
functionality will be tested later on
aftermethod will be executed if the
operations are performed in a single class

Ex:
Login -----customer details ----logout
Login product details ---logout
Login payment details ---logout

Some of the annotations in testNG are :


@beforeMethod
@afterMethod

Example :

package DAY1;

import java.io.File;
import java.io.IOException;
import java.util.List;

import
org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;
import
org.testng.annotations.AfterMethod;
import
org.testng.annotations.AfterTest;
import
org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Amazon {


WebDriver d;
@BeforeMethod
public void Opening_Browser() {
System.setProperty("webdriver.chrome.
driver","C://chromedriver.exe");
d=new ChromeDriver();
d.get("http://www.amazon.in");
d.manage().window().maximize();
}
@Test(priority=2)
public void title()
{
System.out.println("Title of the
page is : "+d.getTitle());
}
@Test(priority=3)
public void url()
{
System.out.println("url of the
current page is :
"+d.getCurrentUrl());
}
@Test(priority=4)
public void Selecting()
{
WebElement
drop=d.findElement(By.id("searchDropd
ownBox"));
List<WebElement>drop1=drop.findElemen
ts(By.tagName("option"));
System.out.println(drop1.size());
for(int i=0;i<drop1.size();i++)
{
System.out.println(drop1.get(i).getTe
xt());
}

d.findElement(By.id("searchDropdownBo
x")).sendKeys("Books");
}
@Test(priority=5)
public void Screenshot() throws
IOException
{
File
src=((TakesScreenshot)d).getScreensho
tAs(OutputType.FILE);
FileUtils.copyFile(src, new
File("D://o11.png"));
}
@AfterMethod
public void Close()
{
d.close();
}
}

@beforeclass and @afterclass


Openining ---------------------
performing-------------closing
Or
Priorities
Beforeclass and afterclass will executes atleast
once the entire functionality

Either we can use Beforeclass ,afterclass or


priorities based on the functionality

Example:
package DAY1;

import java.io.File;
import java.io.IOException;
import java.util.List;

import
org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;
import
org.testng.annotations.AfterClass;
import
org.testng.annotations.AfterMethod;
import
org.testng.annotations.AfterTest;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Amazon {


WebDriver d;
@BeforeClass
public void Opening_Browser() {
System.setProperty("webdriver.chrome.
driver","C://chromedriver.exe");
d=new ChromeDriver();
d.get("http://www.amazon.in");
d.manage().window().maximize();
}
@Test(priority=2)
public void title()
{
System.out.println("Title of the
page is : "+d.getTitle());
}
@Test(enabled=false)
public void url()
{
System.out.println("url of the
current page is :
"+d.getCurrentUrl());
}
@Test(priority=4)
public void Selecting()
{
WebElement
drop=d.findElement(By.id("searchDropd
ownBox"));
List<WebElement>drop1=drop.findElemen
ts(By.tagName("option"));
System.out.println(drop1.size());
for(int i=0;i<drop1.size();i++)
{
System.out.println(drop1.get(i).getTe
xt());
}

d.findElement(By.id("searchDropdownBo
x")).sendKeys("Books");
}
@Test(priority=5)
public void Screenshot() throws
IOException
{
File
src=((TakesScreenshot)d).getScreensho
tAs(OutputType.FILE);
FileUtils.copyFile(src, new
File("D://o11.png"));
}
@AfterClass
public void Close()
{
d.close();
}
}

Instead of using beforeclass , afterclass we


can use Priorities :

package DAY1;

import java.io.File;
import java.io.IOException;
import java.util.List;

import
org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriv
er;
import
org.testng.annotations.AfterClass;
import
org.testng.annotations.AfterMethod;
import
org.testng.annotations.AfterTest;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Amazon {


WebDriver d;
@Test(priority=1)
public void Opening_Browser() {
System.setProperty("webdriver.chrome.
driver","C://chromedriver.exe");
d=new ChromeDriver();
d.get("http://www.amazon.in");
d.manage().window().maximize();
}
@Test(priority=2)
public void title()
{
System.out.println("Title of the
page is : "+d.getTitle());
}
@Test(enabled=false)
public void url()
{
System.out.println("url of the
current page is :
"+d.getCurrentUrl());
}
@Test(priority=4)
public void Selecting()
{
WebElement
drop=d.findElement(By.id("searchDropd
ownBox"));
List<WebElement>drop1=drop.findElemen
ts(By.tagName("option"));
System.out.println(drop1.size());
for(int i=0;i<drop1.size();i++)
{
System.out.println(drop1.get(i).getTe
xt());
}

d.findElement(By.id("searchDropdownBo
x")).sendKeys("Books");
}
@Test(priority=5)
public void Screenshot() throws
IOException
{
File
src=((TakesScreenshot)d).getScreensho
tAs(OutputType.FILE);
FileUtils.copyFile(src, new
File("D://o11.png"));
}
@Test(priority=6)
public void Close()
{
d.close();
}
}
beforetest
@aftertest

Test: multiple classes


Class 1
@before test
Class 2
P1, p2,p3
Class 3
P4,p5,p6
Class 4
P7,p8
@aftertest

Ex:
Class 1
Login ( beforetest)
Inbox

Class 2;

Compose
Chat

Class 3

Draft

Class 4
New folder
Logout (aftertest)
@beforesuite
@aftersuite

@dataProvider

You might also like