Skip to main content

TestNG Annotation- Dataprovider and Test Reporting via Index.html

 




TestNG Annotation is a piece of code which is inserted inside a program or business logic used to control the flow of execution of test methods

Lets go through  few TestNG annotation

Some Basic TestNG annotation

@Before suite

@BeforeClass

@BeforeMethod

@BeforeTest

@Test

@AfterTest

@AfterMethod

@AfterClass

@AfterSuite

@BeforeSuite


You can validate them by writing the below code


public void setup(){

System.out.println("Before suite");

}


@BeforeClass


public void LaunchBrowser(){

System.out.println("Before class");

}


@BeforeMethod


public void EnterUrl(){

System.out.println("Before Method");

}


@BeforeTest

public void Login(){

System.out.println("Before Test");



}


 Priority and Groups

//priority and groups are keyword give to @test to prioritise which test annotation should be run first

priority 1 will run first then second and third so on

Keyword group will create title in index.html and the test case will open with the names of Homepage, booking checkout and login. 

@Test(priority=3,groups="Home page")

public void checkbox(){

System.out.println("Test3");


}

@Test (priority=1, groups="Booking")

public void Title(){

System.out.println("Test1");

}


@Test(priority=4,groups="checkout")

public void dropdown(){

System.out.println("Test4");

}

@Test(priority=2, groups="Login")

public void Textbox(){

System.out.println("Test2");

}


@AfterTest

public void cookies(){

System.out.println("After test");


}


@AfterMethod

public void logout(){

System.out.println("After Method");

}

@AfterClass


public void classBrowser(){

System.out.println("After class");

}

@AfterSuite


public void testreport(){

System.out.println("After suite");

}


}


Check out the result


Check out the test result in Index.html

You can check out the exceution of program  here
 

Invocation count
if you want to execute same test case 10 times then you must mention @Test(invocationCount=10)

public class TestNGbasics3 {

@Test(invocationCount=10)

public void method1(){

System.out.println("PRINTING 10 IMES");


Invocation time out and expectedExceptions

If you want a loop or infinite loop to break after 2 second of execution then you must mention

@Test(invocationTimeOut=2)


public class TestNgbasic4{

@Test(invocationTimeOut=2)

                           public void infinteloop1(){

                           int i=1;

                           while(i==1)

                                     {

                                       System.out.println("i");

                                      }


                                              }

If you want to handle exception via TestNG keyword then use  "expected Exceptions"

@Test(expectedExceptions=NumberFormatException.class)

public void test()

{

String x ="100A";

System.out.println(x);

}

                         }



Dataprovider in TestNG that allows us to pass multiple parameters to a single test in a single execution .using Dataprovider, we can easily pass multiple values to a test in just one execution cycle using DataProvider we can easily inject multiple values into the same test case. It comes inbuilt in TestNG and is popularly used in data-driven frameworks


here is the code for Data provider

public class Dataprovider

{

@Test(dataProvider = "LoginDataProvider")

     public void loginfb(String Uid,String Pwd)

        {

System.setProperty("Webdriver.chrome.driver", "chromedriver");

    WebDriver driver = new ChromeDriver();

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

    driver.manage().deleteAllCookies();

    driver.get("https://www.facebook.com");

    driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);

driver.findElement(By.xpath("//input[@id='email']")).sendKeys(Uid);

driver.findElement(By.xpath("//input[@id='pass']")).sendKeys(Pwd);

driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

driver.findElement(By.xpath("//button[@type='submit']")).click();

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

          System.out.println("Username is " + Uid);

          System.out.println("Password is " + Pwd);


        }

//the data provider’s name will automatically be set to the method’s name. returns array of objects

//“Logindataprovider” is the name of the function which is passing the test data. 


     @DataProvider(name="LoginDataProvider")


     public Object[][] getData()

          {

        

         Object[][] datanew Object[3][2];


        

               data[0][0] ="user1";

               data[0][1] ="password1";


               data[1][0] ="user2";

               data[1][1] ="password1";


               data[2][0] ="user3";

               data[2][1] ="Password3";

               return data;


You can check the execution here



Comments

Popular posts from this blog

Cucumber - Execution of test cases and reporting

Before going through this blog please checkout blog on   Cucumber Fundamentals Cucumber is testing tool which implements BDD(behaviour driven development).It offers a way to write tests that  anybody can understand, regardless of there technical knowledge. It users Gherkin (business readable language) which helps to  describe behaviour without going into details of implementation It's helpful for  business stakeholders who can't easily read code ( Why cucumber tool,  is  called  cucumber , I have no idea if you ask me I could have named it "Potato"(goes well with everything and easy to understand 😂) Well, According to its founder..... My wife suggested I call it  Cucumber  (for no particular reason), so that's how it got its  name . I also decided to give the Given-When-Then syntax a  name , to separate it from the  tool . That's why it's  called  Gherkin ( small variety of a cucumber that's been pickled. I...

Jmeter 5.4.1- Config Elements - Part-03

  Part-01- Installation of Jmeter and HTTP's Recorder click  here Part 02--Previous blog on Assertion Config elements in Jmeter are used to configure or modify the samplers requests made to the server. These elements are added at the same or higher level of the samplers that we want to configure  Let' start with  CSV data config As the name suggest it used to read data from CSV first we need to put data in variables and then use the variables in sampler request. create a new test plan add CSV data set config Add a Thread Group and then add Sampler "Java Request"  Create a CSV file  with some data (Name and Data) and save it  Now go to Jmeter CSs data set config browse and upload the css file create Make few more changes in place of  variable name - Name and Dept Ignore first line - True Delimeter - \t (as suggested) Now move on the Sampler-" Java Request" and rename it with header elements of CSV As we have Name and d...

Beginners tutorial -:working with JMeter in Mac and windows - Part-01

  Prequisite   you should have Java downloaded in your system with Home path set under environment variables.(as of today Java version 8 and higher are required fro jmeter ) for help check out this link Note Always run the jmeter on your secondary browser,  if you give the primary browser for proxy settings then your internet connection will be disrupted for the browser as well as system For ex if you have chrome and firefox and your primary or default browser is chrome then do all the proxy setting in firefox so it won't hamper the system Internet connection  if you have safari as your default browser in your mac os then set proxy in chrome/firefox  MAC Download jmeter from the link  here click on the hypelink under section Binaries  "Apache JMeter( 5.3 ). tgz" file  for Mac   Tar file will get downloaded Double click on the tar file to unzip  once you open the folder  got to bin and search for jmeter.sh file this is a executa...