Академический Документы
Профессиональный Документы
Культура Документы
**TestNg annotations:
Annotations are used to specify sequence of methods execution without using main
method
1. @Test: it is used to specify that the method under it is a test case(i.e.
validation part)
In one class we can maintain multiple @Test methods
By default those will execute in alphabetical order
We can set prioritize @Test methods using �priority�
Syntax:
@Test(priority=0)
2. @BeforeTest: method under this annotation will be executed before all test cases
in the TestNG file
It will execute only once
3. @AfterTest: method under this annotation will be executed after all test cases
in the TestNG file are executed
6. @BeforeSuite: The annotation method will be run before all tests in this suite
have run
7. @AfterSuite: The annotated method will be run after all tests in this suite have
run
sequence of annotations
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
Ex: create script to perform execution by taking each scenario into individual
methods
Scenarios:
This program to launch browser
This program to refresh the page after each transaction validation
This program is to validate user Registration operation
This program is written to validate login functionality
This script to set home page of application before each transaction validation
To close browser
Note:
One suite can have multiple tests
One test can have multiple classes
One class can have multiple methods
Ex: Create TestNg Xml file to execute tests from different classes in different
packages
Package-1: sampleOne
package sampleone;
import org.testng.annotations.Test;
@Test
public void secondTestCase()
{
System.out.println("it is in second test case from demoOne Class");
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package sampleone;
import org.testng.annotations.Test;
@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoTwo Class");
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package2:sampletwo
package sampletwo;
import org.testng.annotations.Test;
========================================================
Create TestNg xml file
select "Java project"
right click mouse
"New"
select "File"
enter file name with ".xml" extension
write following code:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Ex: create script to execute on multiple browsers to enter value google search
using TestNG
Procedure:
Step 1: create class with required methods:
public class Google {
WebDriver driver;
@BeforeMethod
@Parameters({"browser"})
public void setUp(String myBrowser){
if(myBrowser.equalsIgnoreCase("firefox")){
driver=new FirefoxDriver();
}
else if(myBrowser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver","E:\\SeleniumSoftware\\chromedriver.ex
e");
driver=new ChromeDriver();
}
else if(myBrowser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver","E:\\SeleniumSoftware\\IEDriverServer.exe"
);
driver=new InternetExplorerDriver();
}
}
@Test
public void f() throws InterruptedException {
Thread.sleep(4000);
driver.get("http://google.com/");
driver.findElement(By.name("q")).sendKeys("selenium");
driver.findElement(By.name("btnG")).click();
}
}
-----------------------------------------------
Create TestNg xml file with following code
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="tests">
<test name="firefoxTest">
<parameter name="browser" value="firefox"/>
<classes>
<class name="demo1.Google"/>
</classes>
</test>
<test name="chromeTest">
<parameter name="browser" value="chrome"/>
<classes>
<class name="demo1.Google"/>
</classes>
</test>
<test name="ieTest">
<parameter name="browser" value="ie"/>
<classes>
<class name="demo1.Google"/>
</classes>
</test>
</suite>