true

Find the best tutors and institutes for Selenium

Find Best Selenium Training

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

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


Black in Python
When you are upturn in your career from beginner to experienced in programming world, your team will start looking at ‘how you are writing?’ Here the responsibility piling up. Okay,...

Logeswaran | 31 Jan

0 0
0

How to Read Data From Property File
1. Create a property file using New > File > Given name - like config.properties (it must be saved with .properties). 2. Add data in config.properties like: browser = chrome environment = qa 3....

Sarthak Chadha | 19/12/2019

0 0
0

Facebook Analytics
Assume how the Facebook application will store the millions of customer's record in real-time: facebook = { 'jose': { 'name': 'jose', 'age': 33, 'hobby': , # cricket,football 'mobile': 1111111111, 'email':...

Logeswaran | 03/09/2019

0 0
0

What is Safety-Critical Software?
A software whose failure may cause death or injuries to the users called Safety-Critical Software.The software present as part of Aeroplanes, or present in Rail as railway signalling software or Automotive...

Vihanga EmbedSoft | 13/07/2019

0 0
0
X

Looking for Selenium Classes?

Find best tutors for Selenium Classes by posting a requirement.

  • Post a learning requirement
  • Get customized responses
  • Compare and select the best

Looking for Selenium Classes?

Find best Selenium Classes in your locality on UrbanPro

Post your learning requirement

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 25 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 6.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