TestNG Questions

 1. How to Take screen shots automatically?

Ans: a. Create a function to take screen shots in base class

         b. Implement ITestListener with onTestFailure(ITestResult result) and call the above function

         c. Create a Test Assert.assertEquals(true,false)

         d. After Suite, call the listener class (from b)

            <listeners>

<listener class-name="ScreenShots.listenerclass" />

    </listeners>

    2. How to execute failed test cases automatically in testNG?

     Ans: a. Implement IRetryAnalyzer class

              b. create Method retry(ITestResult result)

                   if(retrycnt<Maxretrycnt) then return true

                    return false

               c. Create RetryListener class implements IAnnotationTransformter

                    public void transform(ITestAnnotation testannotation, Class testClass, Constructor testConstructor, Method testMethod) {

testannotation.setRetryAnalyzer(Retry.class);

    }

     3. How to Use Optional Parameters?

    Ans: @Test

            @Parameters("Value")

            public void Op_Param(@Optional("Optional Parameters called") String Value)

            {

                System.out.println("Optional Param test")

            }

4. How to use Inherited Data Provider?

Ans: When Data provider and test methods are in two diff classes

@Test (dataProvider = "data-provider", dataProviderClass = DP.class)

    public void myTest (String val) {

      System.out.println("Current Status : " + val);

    }


5. Data Provider with    method as parameter

Ans: @DataProvider (name = "data-provider")

public Object[][] dpMethod (Method m){

switch (m.getName()) {

case "Sum": 

return new Object[][] {{2, 3 , 5}, {5, 7, 9}};

case "Diff": 

return new Object[][] {{2, 3, -1}, {5, 7, -2}};

}

return null;

}

@Test (dataProvider = "data-provider")

public void Sum (int a, int b, int result) {

      int sum = a + b;

      Assert.assertEquals(result, sum);

}

  

@Test (dataProvider = "data-provider")

public void Diff (int a, int b, int result) {

      int diff = a - b;

      Assert.assertEquals(result, diff);

}

6. Test NG Priorities

Ans: When no priority is assigned, default priority is 0

When test cases have same priorities, they will run in the Alphabetical order of Test Names

Example:  Test Name = Account() 

                    Test Name= Browser() priority = -1

                    Test Name= Close() Priority =0

 Runs: Below is the order of test methods run

Browser()

Account()

Close()



Comments

Popular Posts