UrbanPro
true

Learn Selenium from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

TestNG

Kiran Ayyagari
24/07/2019 0 0

TestNG Concepts

 

TestNG:

TestNG is an automation framework to run the test cases, test suites and do parallel testing. It is an advanced version of Junit. It consists of many features like annotations, groups, parameterization and listeners. It generates html reports as well which is an advanced feature.

 

Annotations of TestNg:

There are different types of annotations present in TestNG which are used for organized and reusable tests.

  1. @BeforeSuite: The method which has this annotation will run before all the tests in the suite runs
  2. @AfterSuite: The method which has this annotation will run after all the tests in the suite runs
  3. @BeforeTest: The method which has this annotation will run before the tests present inside the class tag of testing.xml is run
  4. @AfterTest: The method which has this annotation will run after the tests present inside the class tag of testing.xml is run
  5. @BeforeClass: The method which has this annotation will run before the first test in the class is run
  6. @AfterClass: The method which has this annotation will run after all tests in the class are run
  7. @BeforeMethod: The method which has this annotation will run before each test method runs
  8. @AfterMethod: The method which has this annotation will run after each test method runs
  9. @Test: This is the main part of the test case
  10. @DataProvider: This annotation is used to extract the data from excel and send it to all test methods.
  11. @Parameters : To take the inputs from TestNg xml

 

Example:

 

<suite name="SampleTestNGXml">

<test name="TestNGAnnotationsExample">

<parameter name="browser" value="Firefox"/>

<parameter> name=”Url” value=” https://www.google.com”

<classes>

<class name=" TestNgExample" />classes>test>suite>

 

public class TestNgExample()

{

@Parameters({“browser”})

@BeforeSuite

public void  setup(String browser){

if(browser.equlas(“Firefox”)){

WebDriver driver = new FirefoxDriver();

}

else if(browser.equals(“chrome”)){

System.setProperty(“webDriver.chrome.driver”,path);

WebDriver driver = new ChromeDriver();

 

}

else if(browser.equals(“IE”)){

System.setProperty(“webDriver.ie.driver”,path);

WebDriver driver = new InternetExplorerDriver();

}

 

}

@BeforeClass

Public void browserSetup(){

DesiredCapabilities dc = new DesiredCapabilities.firefox();

driver = new FirefoxDriver(dc);

driver.manage().timeouts().implicitlywait(60,TimeUnit.SECONDS);

driver.manage().window().setSize(new Dimension(1920,1080));

 

}

 

 

@Parameters({“Url”})

@BeforeTest

public void beforeTest(String url){

 

driver.get(url);

driver.manage().window().maximize();

}

 

@DataProvider(name = “sample”)

public object[] getData(){

return new Object[]{

{“TestNg”}}}

 

@Test(dataProvider=”sample”)

Public void minTest(String searchText){

 

WebElement searchBox = driver.findElement(By.Id(“fakebox-input”));

searchBox.sendKeys(searchText);

}

 

@AfterTest

Public void afterTest(){

System.out.println(“any reporting activities using extent Reports”);

}

 

 

@AfterClass

Public void afterClass(){

driver.close();

}

 

 

 

@AfterSuite

 

Public void teardown(){

driver.quit();

}

 

 

}

 

List of Attributes which we use in @Test method:

  • alwaysRun: If we want to run a @Test method always, we can set this attribute to true

eg: @Test(alwaysRun= true)

  • enabled: If we don’t want a @Testmethod run, we can set the enabled attribute as false

eg: @Test(enabled = false)

  • Priority: If we want to run our test cases based on priority ,we can use this attribute.

eg: @Test(Priority = 1)

  • groups: If we want to group together @Test methods, we can use this attribute

eg: @Test(groups = “smoketesting”)

  • dependsOnGroups : If we want to run a particular @Test method after a particular group of @Test method runs, we can use this attribute

eg: @Test(dependsOnGroups = “smoketesting”)

  • dependsOnMethods : If we want to run a particular @Test method after a particular @Test method runs, we can use this attribute

eg: @Test(dependsOnMethods=”testMethodName”)

Example:

 

Public class attributeExample(){

 

@Test(groups=”smoketesting”)

public void test1(){

 

System.out.println(“This method belongs to smoketesting group”);

}

 

@Test(groups=”smoketesting”)

public void test2(){

 

System.out.println(“This method belongs to smoketesting group”);

 

}

@Test(dependsOnGroups=”smoketesting”)

public void test3(){

System.out.println(“This method runs  after smoketesting group methods run”);

 

}

@Test(dependsOnMethods=”test1”)

public void test4(){

System.out.println(“This method runs after test1 runs”);

 

}

 

@Test(enabled = false)

Public void test5(){

System.out.println(“This method will not run till test case is enabled”);

 

}

 

@Test(alwaysRun = true)

Public void test6(){

System.out.println(“This method always runs”);

 

}

 

@Test(priority=1)

Public void test7(){

System.out.println(“This method runs before any other test method runs as it has first priority”);

 

}

depends tag in testing.xml:

Till now we have seen dependsOnMethods and dependsOnGroups used for @Test methods to depend on groups and methods. But , we can use the depends on tag in testing.xml to perform the same operation without using the attribute.

 

 

Example:

 

Include&Exclude Tags in TestNg.Xml:

Include and Exclude Tags are used in testing.xml to include or exclude any @Test method to run

 

Example:

 

TestNG Listeners:

TestNG listeners are used to execute before or after @test method or suite runs. There are different types of TestNG listeners. Please find them below.

IsuiteListener: This is an interface which  has two methods called onStart() and onFinish(). If a class implements this listener, these methods will run before and after the suite runs.

ITestListener: This interface is also similar to IsuiteListener but the difference is that it will run before and after Test  runs. It consists of below methods.

onStart(): This method will run before @Test method runs

onFinish():This method will run after @Test method runs

onTestFailure(ITestResult result): runs whenever a test fails

onTestSkipped(ITestResult result) : runs whenever a test is skipped

onTestSuccess(ItestResult result): runs whenever a test is succeded

 

IInvokedMethodListener: This listener will run before and after every method is run. It consists of two methods.

beforeInvocation(): Invokes before each method runs

afterInvoction(): Invokes after each method runs

 

Example:

 

public class sampleListener implements ItestListener,IsuiteListener,IInvokeMethodListener{

 

public void onStart(ISuite suite){

System.out.println(“runs before suite is started”);

}

public void onFinish(ISuite suite){

System.out.println(“runs after suite is started”);

 

}

public void onStart(ITestContext testContext){

System.out.println(“runs before test is started running”);

 

}

public void onFinish(ITestContext testContext){

System.out.println(“runs after test is started running”);

}

 

public void onTestSuccess(ITestContext testContext){

System.out.println(testContext .getName()+“ is successful”);

}

 

public void onTestFailure(ITestContext testContext){

 

System.out.println(testContext .getName()+“ is failed”);

//Taking failed Test cases screenshots

File src = ((TakeScreensot)driver).getScreenshotAs(OutputType.FILE)

FileUtils.copyFile(src, new File(path));

}

 

public void beforeInvocation(IInvokedMethod method,ItestResult result){

System.out.println(“This method is invoked before every method runs”);

}

 

public void afterInvocation(IInvokedMethod method,ItestResult result){

System.out.println(“This method is invoked after every method runs”);

}

}

Including listener in Testngclass:

 

@Listeners(sampleListener)

public class testngSampleTest(){

 

@Test

public void  test1(){

 

Including listener tag in TestNg.xml:

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Xpath Cheatsheet
Xpath Descendant selectors h1 //h1 ? div p //div//p ? ul > li //ul/li ? ul > li > a //ul/li/a div > * //div/* :root

Puzzle: A Milkman has 2 Empty Jugs and milk
Puzzle: A Milkman has 2 Empty Jugs and milkSize of- Jug A is 3 Ltr and Jug B is 5 LtrHow can he measure exactly one liter without wasting any milk?Solution:Step 1: Fill Jug A with...

The Importance Of Software Testing
Software testing is an important part of the systems development life cycle. It has its own phase in the process and has its own specialised IT professionals. What is the importance of software testing?...

Difference between driver.close() and driver.quit () method
close () -By using this command, we can close the current browser or page. quit() - By using this command, we can shut down the web driver instance or destroy the web driver instance(Close all the windows).

Software Development Life Cycle Methodologies.
In this, I'm going to explain types of Software Development Life Cycle methods In earlier days many organisations follows these model: 1. Waterfall Model 2. Prototype Model 3. Incremental Model 4....
X

Looking for Selenium Classes?

The best tutors for Selenium Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Selenium with the Best Tutors

The best Tutors for Selenium Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more