Showing posts with label TestNG. Show all posts
Showing posts with label TestNG. Show all posts

Road to parallel execution of selenium webdriver test using testng

In this post you will learn how to run selenium webdriver test scripts parallel in more two environments using testng suite, testng has a feature by which we can create multiple instance of a single testng class file.

In below example, I created a selenium webdriver test script for google search. Which will run on two browsers firefox and IE.

TestNG factory class with example

TestNG factory class is used to create instance of testNg class at runtime (dynamically). When we apply Factory annotation on method, it always return object array of Object class ( “Object[]”). This is useful annotation when you run test multiple time.

TestNG pick @Factory method from class and consider each object mentioned in factory test as a TestNg class and invoke in separate.

Example:  FactoryClassImp.java
package com.tests;

import org.testng.annotations.Factory;

public class FactoryClassImp {

 @Factory
 public Object[] createTest() {
  Object[] res = new Object[3];
  res[0] = new FactoryTestClass(2, 2);
  res[1] = new FactoryTestClass(2, 3);
  res[2] = new FactoryTestClass(2, 4);

  return res;
 }
}


TestNG - Exception Test

In this post I will show you how to handle exception in Testng. TestNg provides expectedExceptions parameter  which is used along with @Test annotation, exp @Test(expectedExceptions)

package com.test;

import org.testng.annotations.Test;

public class TestNgException {

        @Test(expectedExceptions = ArithmeticException.class)
        public void testException() {
               int a = 10;
               int b = 0;
               a = a / b;
        }

        @Test
        public void testException1() {
               int a = 10;
               int b = 0;
               a = a / b;
        }
}

In test method testException I used “@Test(expectedExceptions = ArithmeticException.class)” so airthmetic exception handle but in  test testException1 exception generated.

TestNG - Ignore Test

In this post I will show you how to ignore testng test case to execute,  TestNg provide attribute enabled which make the test to be execute or not, if test mark @Test(enabled = false) then test will not execute. By default enabled value is true.

Here is example for ignore test case:

package com.test;

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

public class IgnoreTestClass {

        @Test
        public void testMethod1() {
               Assert.assertTrue(true);
        }

        @Test(enabled = false)
        public void testMethod2() {
               Assert.assertTrue(true);
        }

        @Test(enabled = true)
        public void testMethod3() {
               Assert.assertTrue(true);
        }
}

Run above test you will see that testMethod2 will ignored and not executed:

PASSED: testMethod1
PASSED: testMethod3

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
====================================   

Road to publish Webdriver TestNg report in Jenkins

In this post I will show you how to publish TestNg report on Jenkins server.

Installation TestNG plug-in.
1. After launching Jenkins server go to "Manage Jenkins>>Manage Plugins"
2. Enter TestNg in filter field, you will see “TestNg Results Plugin” like below screen.


3. Click on check box and then click on installation button.

Road to override TestNg listener methods

In this post I will show you how to create custom testng listener  class and override its methods.
Create java class like I created below “MyListner” and inherit “TestListenerAdapter” testing class.

package com.test;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class MyListner extends TestListenerAdapter {
       
     @Override
     public void onTestFailure(ITestResult result) {
        System.out.println(result.getName() + " Test method failed\n");
     }

     @Override
     public void onTestSkipped(ITestResult result) {
        System.out.println(result.getName() + " Test method skipped\n");
     }

     @Override
     public void onTestSuccess(ITestResult result) {
        System.out.println(result.getName() + " Test method success\n");
     } 
 

TestNg Dependencies

Sometime we need to run test methods in certain order, As we need to run a test method always run after some test method. TestNg provide two ways to accomplish this either with annotation or XML suite.

Dependencies with annotations.

dependsOnMethods or dependsOnGroup attribute are used on @Test annotation to accomplish execution of dependent test.

There are two kinds of dependencies:

TestNG Parametrization: Part-2

If you need some complex parameter or parameter that need to created from java then using testng xml file parameter is not sufficient. For this need to use Data provider to pass values into test method. A data provider is a method of your class which return an array of array of objects. TestNg annotation of this method is @DataProvider.

TestNG Parametrization: Part-1

Parametrization means passing values in Unit test script using TestNG xml file, or using @DataProvider testing annotation

Parametrization using testing xml file:

For this type of parametrization need to declare “@Parameters” annotation in method which needs parameter testing, the value of parameter will be provided by using TestNG suite xml file. Using this we can Test a single unit test for multiple set of values.

How to create TestNG suite file.

TestNG suite means grouping two or more than two classes to execute with a single command.

How to create:
Create some test class as I created below two test class  “FirstTest.java” and “SecondTest.java” java files.

FirstTest.java:

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class FirstTest {
       
        @BeforeClass
        public void beforeClass() {           
           System.out.println("One time executed before first mathod");
        }
         
        @AfterClass
        public void afterClass() {
            System.out.println("One time executed after last method");
        }
                  
        @Test
        public void testFirstMethod() {       
           System.out.println("@Test - executed first test method");
        }
}


Some basic annotation of TestNG

Below are some basic annotation which we use in automation.
 
@BeforeSuite - Execute only one time before all testNg method..
@AfterSuite - Execute only one time after all testNg method.
@BeforeClass - Execute only one time before first test and after @BeforeSuite testNg method.
@AfterClass - Execute only one time after last test and before @AfterSuite testNg method.
@BeforeMethod - Execute before every @Test methods.
@AfterMethod - Execute after every @Test methods.
@Test – TestNG test method.

Road To - TestNG Installation in eclipse

1. Open eclipse.
2. Goto Help>>Install New Software and click, screen should be display as below:.

TestNG vs Junit 4

Junit 4  and TestNG are similar on surface where as Junit 4 is designed for unit test. TestNg is high label testing it specially use very large and complex test suite.TestNg has some feature which are not in Junit 4.
Below are compression of TestNG and Junit 4 feature