Showing posts with label testng. Show all posts
Showing posts with label testng. Show all posts

TestNG @Test attributes - threadPoolSize, invocationCount, timeOut

Let's see invocationCount implementation

public class InvocationCnt {
 
      @Test(invocationCount=5)
      public void test1() throws InterruptedException
      {
   System.out.println("Thread ID: "+Thread.currentThread().getId());  
      }
}

OutPut:
Thread ID: 1
Thread ID: 1
Thread ID: 1
Thread ID: 1
Thread ID: 1
above method ran 5 times, a single thread will be assigned to run the method one by one..

Now let's see with threadPoolSize implementation

TestNG @Test attributes - AlwaysRun

alwaysRun is a testNG attribute which makes a test method run always even if it's dependent method is not run...

let's see the code implementation

public class AlwaysRun {
     WebDriver driver;
     @Test
     public void test1()
 {
    driver = new FirefoxDriver();
    driver.get("http:\\qavalidation.com");
    Assert.assertTrue(driver.getTitle().contains("Testing"));
 }
     @Test(dependsOnMethods = {"test1"}, alwaysRun=true)
     public void test2()
 {
     driver.findElement(By.linkText("Selenium Tutorial!")).click();
 }
}

Observe the output, even though test1 did not run, test2 executed.

TestNG @Test attributes - dependOnMethods

There are situations when we want to run our tests depends on the prior test execution like
if a particular test is run fine, then execute this test or else skip the test execution...
Basically we are creating inter dependencies in test execution.

Examples
Launch browser with URL, verify if title matches, then login
In Gmail, if login successful, then verify if Inbox link present or not
If report generated, then print the report

Let's see the code implementation

TestNG @Test attributes - enabled property

There are situations where we need to skip one or more @Test method in testNG,
for this TestNG provides enabled property to skip the execution of methods...

enabled = false (skip the method run)
enabled = true (execute method), it's by default set to true,

NOTE: if we do not mention enabled property, it's set to true

Let's see the code implementation

TestNG @Test attributes - run by priority

to prioritize test case execution order, testng provides priority keyword and thew value starts from
1 to n...

For a method less priority number, executes first

test case   priority              
   tc1            2                     Order of execution
   tc2            4                            tc3 - tc1 - tc2 - tc4
   tc3            1
   tc4            5

Let's see the code implementation:

TestNG @Test attributes - method description and grouping

description and droups

In framework and with growing number of automated test cases, it's hard to maintain what test cases need to be executed for functional, regression or sanity etc, 
To avoid this, TestNG came up with nice concept called grouping (aka groups), where we can provide the group names in each @Test method and can run the respective grouping testNG.xml to run only required test cases.

To understand this, refer to the below code output

package testng1;
import org.testng.annotations.Test;

public class TestNGAnnGroups {
      @Test(description="This is testcase1 desc", groups={"funct", "sanity"})
      public void test1()
      {
 System.out.println("This is method: test1");
      }
 
      @Test(description="This is testcase2 desc", groups={"funct"})
      public void test2()
      {
 System.out.println("This is method: test2");
      }
 
      @Test(description="This is testcase3 desc", groups={"sanity"})
      public void test3()
      {
 System.out.println("This is method: test3");
      }
}

we can assign one or more group names to each of the @Test methods, as shown above

and the testng.xml looks like


Handling multiple browser windows/popups using selenium

There are situations in which we get browser popups or multiple browser window opens, when we open an URL or click on an element on browser.

popups can be blocked in browser using some browser addons,
but for testing purpose, sometimes we need to allow the popups or multiple windows.

A single instance of selenium web-driver can handle multiple popups and browser windows.
Let's see how web-driver handles these above mentioned popups or browser windows.

Selenium provides 2 methods getWindowHandle() and getWindowHandles() to deal with the multiple browser windows.

testNG @Test attributes



TestNG framework provides some more features with the @Test to customize execution of test methods

To know basics about the @Test, see the testng annotation.





Attributes are:

description: The description for this method.
groups: The list of groups this class/method belongs to.

Refer description and grouping testcases


priority: The priority for this test method. Lower priorities will be scheduled first.

Refer Prioritize test cases


enabled: Whether methods on this class/method are enabled.

Refer @Test method enabled property


dependsOnMethods: The list of methods this method depends on.
Same, dependsOnGroups: The list of groups this method depends on.

Refer dependsOnMethods


alwaysRun: If set to true, this test method will always be run even if it depends on a method that failed.

Refer alwaysRun


invocationCount:The number of times a method should be invoked.
threadPoolSize:The size of the thread pool for a method.
timeOut: this works with threadPoolSize , timeOut says how much time a particular thread holds, after the time over, thread will be release and test will not be executed

Refer demo


Assert in Selenium

Assert is to compare the expected with actual.

Assert.assertEquals:
If fails i.e if expected != actual, then test execution stops with exception: java.lang.AssertionError
Else, Program continues normal flow after checking the Assert statement.

Assert.assertTrue:
is to evaluate a condition

Usage:
Best example is login, if logged then continue else stop executing rest of the test case.
To check if a table exist or not, if not present, then no need to loop through the content..

Let's see the implementation of Assert.

Selenium testNG Parametrization - Parameters and dataProvider

TestNG provides a way to get the data from external sources like from excel/textfile/arrays which helps to prepare data driven framework in automation.

@Parameters - pass parameters from the testNG.xml

@dataProvider - Pass the parameters as a 2D array.

Let's see the code implementation

Selenium testNG annotations

For the fundamentals of testNG framework and the testng.xml content, please refer to my prev blog post.
Let's jump into a sample code to get more understanding on the annotations-

These below 2 classes contains all the details of testNG annotations and the output shows sequence of execution of each annotation.

Below examples also shows difference between @AfterTest and @AfterMethod


Selenium testng.xml and emailable report in detail

To get started with testNG framework, refer testNG framework

If we want to execute the testng class, we can do one by one in eclipse by clicking F5 or right click run as,

what if we want to run multiple classes, then we can take use of testng.xml.

tetng.xml helps to execute one or more testNG classes at a time and also gives an option to maintain the sequence of execution of java classes and also it provides various ways to control the execution.

Let's discuss them one by one.

Selenium - All about testNG frameowrk


testNG is a testing framework helps to run our tests(methods) in a guided manner. 

testNG stands for 'test Next Generation' - as this offers more flexibility over JUnit (another testing framework to run the methods).



What testNG offers:
  • Need not to have test names in sorting order
  • User friendly annotations.
  • Support for data-driven testing(@DataProvider)
  • Support for parameters
  • Group wise test run
  • Parallel test run
  • Allow dependency of one test on another
  • Support to enable or disable the test run 
We will discuss the above one by one.