Sunday, 11 September 2016

Parameterization in TestNG


Assertions in TestNG

Most Commonly used Assertions in TestNg for automation testing are :

•       assertEquals(boolean actual, boolean expected)
     Asserts that two booleans are equal. If not, assertions fails.
•       assertEquals(boolean actual, boolean expected, String message)
                 Asserts that two booleans are equal. If not, assertions fails, & message is displayed.
•       assertEquals(String actual, String expected)
               Asserts that two Strings are equal. If not, assertions fails.
•       assertEquals(String actual, String expected)
               Asserts that two Strings are equal. If not, assertions fails.
•       assertEquals(String actual, String expected, String message)
               Asserts that two Strings are equal. If not, assertions fails, & message is displayed.
•       assertFalse(boolean condition)
              Asserts that a condition is false. If not, assertions fails.
•       assertFalse(boolean condition, String message)
              Asserts that a condition is false. If not, assertions fails, & message is displayed.

•       assertNotNull(Object object)
        Asserts that an object isn't null. If not, assertion fails
•       assertNull(Object object)
        Asserts that an object is null.
•       assertNull(Object object, String message)
        Asserts that an object is null. . If not, assertion fails, & message is displayed.
•       assertTrue(boolean condition)
        Asserts that a condition is true. If not, assertion fails.
•       assertTrue(boolean condition, String message)

        Asserts that a condition is true. If not, assertion fails, & message is displayed

Next

Skipping a test case in TestNG

package testng.examples;
  import org.testng.Assert;
 import org.testng.annotations.Test;

  public class Ignore {
       @Test(enabled = false)
         public void test() {
              Assert.assertEquals(true, true);
      }
}

Creating Suites in TestNG

In TestNG suites are created using xml file. 

Below is an example.


<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  
 <!-- @BeforeSuite -->
<suite name="TestAll">
   <!-- @BeforeTest -->
              <test name="case1">
                     <classes>
                                <class name="com.testng.examples.configuration.Test1" />
                                <class name="com.testng.examples.configuration.Test2" />
                                <class name="com.testng.examples.configuration.Test3" />
                       </classes>
               </test>
<!-- @AfterTest -->
•
  <!-- @BeforeTest -->
           <test name="case2">
                  <classes>
                         <class name="com.testng.examples..Test1" />
                         <class name="com.testng.examples.Test2" /> 
                   </classes>
            </test>
<!-- @AfterTest -->
 </suite>

<!-- @AfterSuite -->

Next

Parameters passed to @Test annotation

•       dataProvider : The name of the data provider for this test method.

•       dependsOnMethods : The list of methods this method depends on.

•       enabled : Whether methods on this method are enabled.

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

•       expectedExceptions : The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.


•       timeOut : The maximum number of milliseconds this test should take.

Next

Why do we need TestNG in Selenium?

  1. TestNG can generate reports based on our Selenium test results.
  2. TestNG simplifies the way the tests are coded.
  3. Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.

Next

TestNG Annotations

@Test: The annotated method is a part of a test case

@BeforeMethod: The annotated method will be run before each test method.

@AfterMethod: The annotated method will be run after each test method.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeSuite: The annotated method will be run before all tests in this suite have run.

@AfterSuite: The annotated method will be run after all tests in this suite have run.


@DataProvider : Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation. 

Next