1. What is the Sequence of execution of Annotations in TestNG?

The test methods in TestNG follow the Suite->Test->Class->Method sequence combined with the
Before annotations->Test annotations->After annotations sequence.

So, the order of execution is-

                           @BeforeSuite
                           @BeforeTest
                           @BeforeClass
                           @BeforeMethod
                           @Test
                           @AfterMethod
                           @AfterClass
                           @AfterTest
                           @AfterSuite

2. What is difference between @BeforeTest and @BeforeMethod?

@BeforeTest - The method will get executed only once before any test method belonging to the classes inside the <test> tag is run.

For example: If there are 10 classes inside a <test> tag in a xml file – the @BeforeTest will get executed only once before the first @Test method of first class is run.

@BeforeMethod - The method will get executed before each test method.

For example: If there are 10 @Test methods inside a class then @BeforeMethod will get executed every time before a @Test method is run. (So totally 10 times @BeforeMethod will be executed)

3. What is the Default priority value for a Test method and What happens if the priority is not set for any Test method?

The default priority value for a Test method is 0 (Zero). And if the priority is not set, TestNG will execute the @Test method based on alphabetical order of method names irrespective of their place of implementation in the code.

4. What happens when a Negative value (Example: -1, -2) is given as a priority for a Test method?

If you are setting negative priority for a test case then it will be executed firstly compare to test case having priority positive or without priority.

For example: If there are three @Test methods with following priority values,

Method 1 priority value: 0,

Method 2 priority value: 1,

Method 3 priority value:  -1.  Order of Execution is: Method 3 -> Method 1 -> Method 2.

5. What are the Two types of dependency attributes used in TestNG?

1. Using attribute “ dependsOnMethods “ in @Test annotations :

Code example:

@Test (dependsOnMethods = { "OpenBrowser" })

public void SignIn() {

System.out.println("This will execute second (SignIn)");

}

@Test

public void OpenBrowser() {

System.out.println("This will execute first (Open Browser)");

}

2. Using attribute dependsOnGroups in @Test annotations :

Code example:

@Test(dependsOnGroups = { "SignIn" })

public void ViewAcc() {

System.out.println("SignIn Successful");

}

@Test(groups = { "SignIn" })

public void BrowserOpen() {

System.out.println("Opening the browser");

}

@Test(groups = { "SignIn" })

public void LogIn() {

System.out.println("Logging In");

}

6. How to exclude a particular test method from a test case execution in Xml file?

Generally, TestNG provides an option to include or exclude Groups, Test Methods, Classes and Packages using include and exclude tags by defining in testng.xml. Specific to the above question pls find the example below :

Code Example :

public class AddTestCase {

@Test

public void AddLocation() {

System.out.println("Adding location will get executed");

}

@Test

public void AddEmployee() {

System.out.println("Adding employee to be excluded");

}

}

To exclude a particular test method from above class, please find below sample in a Xml File :

To exclude a particular test method from above class, please find below sample in a Xml File :

<class name="PackageName.AddTestCase">

<methods>

<exclude name=" AddEmployee" />

</methods> </class> </classes>

7. What is Assertion and Generic syntax for Assertions in TestNG?

Assertions in TestNG are a way to verify that the expected result and the actual result matched or not. We can also specify a message as a parameter to get displayed if the condition is not satisified.

Generic Syntax :

Assert.Method(actual, expected).

For example :

Assert.assertEquals(String actual,String expected) : Asserts that two Strings are equal. If they are not, an AssertionError is thrown.

Assert.assertEquals(String actual,String expected, String message) : Asserts that two Strings are equal. If they are not, an AssertionError, with the given message, is thrown Assert.assertTrue(condition) : Asserts that a condition is true. If it isn’t, an AssertionError is thrown.

8. What is difference between Soft Assertion and Hard Assertion?

Soft Assertion: Soft Assert collects errors during @Test. Soft Assert does not throw an exception. when an assert fails and would continue with the next step after the assert statement.

Hard Assertion: Hard Assert throws an Assert Exception immediately when an assert statement fails and test suite continues with next @Test.

Limitation with Soft Assert: It will pass the whole test script even if any step fails. To overcome this problem, we will use assertAll method at the end of each @Test. Now If it finds any assert failure, it updates the test result and marks the script fail.

9. What are the two ways to do Parameterized testing in TestNG?

1. Using “Parameters annotation” and pass parameter value in TestNG XML file.

Code Example:

public class UseParameter {

@Parameters(“browser”)

@Test

public void RunInchrome(String browser) {

System.out.println("Run the scripts in “+browser );

}

Pass the parameter value in xml like example shown below:

<test name="chrome test">

<parameter name="browser" value="CHROME"/>

<classes>

<class name="PackageName.UseParameter"/>

</classes>

</test>

2. Using “DataProvider annotation”. In the below example, single test method will get executed for both the values given in data provider method.

public class DP {

@DataProvider (name = "data-provider")

public Object[][] dpMethod(){

return new Object[][] {{"First-Value"}, {"Second-Value"}};

}

@Test (dataProvider = "data-provider")

public void myTest (String val) {

System.out.println("Passed Parameter Is : " + val);

}

10. How to perform Cross-Browser Testing using TestNG?

Using “Parameters annotation” and pass different browser parameter values in TestNG XML file we can achieve the cross browser testing.

Code Example:

public class UseParameter {

@Parameters(“browser”)

@Test

public void RunInchrome(String browser) {

System.out.println("Run the scripts in “+browser );

}

Pass the browser parameter values in xml like example shown below:

<suite name="Cross Browser">

<test name="Chrome test">

<parameter name="browser" value="CHROME"/>

<classes>

<class name="PackageName.UseParameter"/>

</classes>

</test>

<test name="Firefox test">

<parameter name="browser" value="FIREFOX"/>

<classes>

<class name="PackageName.UseParameter"/>

</classes>

</test>

</suite>

11.How to run Test methods parallel in TestNG?

Parallel test execution in TestNG triggers with the help of keyword “parallel.” and use it in the TestNG xml file and we can assign any of the values like Methods, Tests, Classes. Below example shows how to execute a Test method in chrome and Firefox in parallel.

Code Example:

public class UseParameter {

@Parameters(“browser”)

@Test

public void RunInchrome(String browser) {

System.out.println("Run the scripts in “+browser );

}

Pass the browser parameter values in xml like example shown below:

<suite name="Cross Browser" parallel="tests" thread-count="2"  >

<test name="Chrome test">

<parameter name="browser" value="CHROME"/>

<classes>

<class name="PackageName.UseParameter"/>

</classes>

</test>

<test name="Firefox test">

<parameter name="browser" value="FIREFOX"/>

<classes>

<class name="PackageName.UseParameter"/>

</classes>

</test>

</suite>

12. How to Re-Run Random failures which is not related to Application/Product using TestNG?

To Re-run the failed test cases, we will use the IRetryAnalyzer interface.

Code Example for IRetry Analyzer :

public class RetryFailedTestCases implements IRetryAnalyzer {

private int retryCnt = 0;

private int maxRetryCnt = 2;

public boolean retry(ITestResult result) {

if ((!result.isSuccess()) {

if (retryCnt < maxRetryCnt) {

System.out.println(“Retrying ” + result.getName() + ” again and the count is ” + (retryCnt+1));

retryCnt++;

return true;

} }return false; }

Now pass the RetryFailedTestCases class in @Test method which you want to Re-Run,

@Test (retryAnalyzer = packagename.RetryFailedTestCases.class)

public void RunInchrome() {

System.out.println("Run the scripts “);

Assert.fail("Failing Test"); } }


Now the @Test method will be re-run 2 times after failing once.

13. How to execute multiple xml files in TestNG at a time ?

To execute multiple xml files with a single xml file, we can use suite-of-suites xml file.
For example: There are 3 packages named multiplesuite.regression, multiplesuite.smoke, multiplesuite.sanity and each package contains 2 class files and each class contains 1 test method.

Now we have one xml file for each package say regression.xml, smoke.xml and sanity.xml.

To execute all xml files in a single file we can use - Suite of Suites xml file . Please find sample xml below:

<suite name="MultipleSuite">

<suite-files>

<suite-file path="regression.xml"></suite-file>

<suite-file path="smoke.xml"></suite-file>

<suite-file path="sanity.xml"></suite-file>

</suite-files>

</suite>


Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}