Вы находитесь на странице: 1из 5

TestNG Framework

TestNG stands for Test New Generation.


It is also same as Junit but it has more functionalities than Junit
Advantages of TestNg:
-*It generate logs
-*Annotations are easier than JUnit
-You can group test cases
-*You can set test cases prioritize
-You can do data Parameterization
-You can do parallel testing using Grid2

How to check TestNG installed or not ?


Navigation:
"Windows" menu
select "Preferences"
check TestNG available or not

Downloading & Configuration of TestNG in Eclipse:


Navigation:
"Help" menu
"Eclipse Marketplace"
Enter TestNg in search edit box
click on Go
click on install

**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

4. @BeforeMethod: methods under this annotation will be executed prior to each


method in each test case (i.e. @Test)
5. @AfterMethod: methods under this annotation will be executed after each method
in each test case (i.e. @Test)

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

8. @Parameter: to get data from TestNg xml file


9. @DataProvider: to get data from xls files

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

TestNg Xml file:


Using TestNg xml file we can execute multiple classes from different packages, and
also we can
include/exclude some methods from class

Creating TestNg xml file:


We use following tags in TestNG xml file:
<Suite name=" ">
<Test name=" ">
<Classes>
<Class name="packagename.classname"/>
<Methods>
<Include name="method name"/>
<Exclude name="method name"/>
</Methods>
</Classes>
</Test>
</Suite>

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;

public class DemoOne {


@Test
public void firstTestCase()
{
System.out.println("it is in first test case from demoOne Class");
}

@Test
public void secondTestCase()
{
System.out.println("it is in second test case from demoOne Class");
}

}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package sampleone;

import org.testng.annotations.Test;

public class DemoTwo {


@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoTwo Class");
}

@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoTwo Class");
}

}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package2:sampletwo
package sampletwo;

import org.testng.annotations.Test;

public class DemoThree {


@Test
public void firstTestCase()
{
System.out.println("im in first test case from demoThree Class");
}
@Test
public void secondTestCase()
{
System.out.println("im in second test case from demoThree Class");
}
}

========================================================
Create TestNg xml file
select "Java project"
right click mouse
"New"
select "File"
enter file name with ".xml" extension
write following code:

<suite name="example suite 1" >


<test name="Regression suite 1" >
<classes>
<class name="sampleone.DemoOne"/>
<class name="sampleone.DemoTwo"/>
<class name="sampletwo.DemoThree"/>
</classes>
</test>
</suite>

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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>

Вам также может понравиться