Selenium/TestNG Interview questions

By | January 21, 2019

TestNG and FrameWork


  1. What is TestNG and its features?
    TestNG(Next Generation) is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use.
    TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc…
    Features:

      Flexible test configuration.
      Support for data-driven testing (with @DataProvider).
      Support for parameters.
      Annotations.
      Default JDK functions for runtime and logging (no dependencies).
      Better reporting
  2. What is the purpose of testng.xml?
    TestNG can be invoked in several different ways, testng.xml is one among these:
    →With a testng.xml file
    →With ant
    →From the command line
    One can define new groups inside testng.xml and specify additional details in attributes.
    Give whether tests to run the tests in parallel or not.
    How many threads to use in case of parallel execution.
    One can provide package name, all test classes in that package will get executed.
  3. What are annotations in TestNG?
    List of some Annotations:

      @BeforeSuite: These methods will be run before all tests in this suite have run.
      @AfterSuite: These methods will be run after all tests in this suite have run.
      @BeforeTest: Will be run before any test method belonging to the classes inside the tag is run.
      @AfterTest : Will be run after any test method belonging to the classes inside the tag is run.
      @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.
      @BeforeMethod: The annotated method will be run before each test method.
      @AfterMethod: The annotated method will be run after each test method.
      @DataProvider: This annotated method will provide data to execute.
      @Test: Marks a class or a method as part of the test.
  4. What are hard and soft asserts in TestNG?
    Asserts comes with TestNG to keep validation points. Corresponding library is org.testng.asserts.Assertion.
    1.Hard Assert:
    Hard assert will check the condition, if condition did not meet it will mark it as failure and abort the test case execution.

               #Below case 2 not equals to 3, hence it will mark as failure and abort further execution.
                import org.testng.Assert;
                public class testcase1 {
                    @Test
                        public void tc001() {
                            Assert.assertEquals(2, 3);
                            system.out.println("Test 1 completed")
                        }
    
    

    Test 1 completed — > this will not get printed

    2.Soft Assert:
    Soft assert will check the condition, if condition did not meet it will mark it as failure and continue the test case execution.
    They don’t throw an exception when an assert fails. It collects all the assertion failures. If we want to fail the test case we can do
    that using assertall at the end of the test.

    To use soft assert we need to create an object for SoftAssert class explicitly.

            #Below case 2 not equals to 3, hence it will mark as failure and continue further execution.
             import org.testng.Assert;
             public class testcase1 {
                 @Test
                 public void tc001() {
                     SoftAssert softasrt=new SoftAssert();
                     softasrt.assertEquals(2, 3);
                     softasrt.assertEquals(2, 2);
                     softasrt.assertEquals(2, 90);
                     system.out.println("Test 1 completed")
                     softasrt.assertAll();
                            }
        
    

    Test 1 completed — > this will not get printed

  5. How to add dependencies of a test case in TestNG?
    TestNG will allow to set dependencies of one test case on another.
    For example all test cases will depend on login test case, if login is failing we will not be able to proceed further
    and verify. So all scenarios will depend on login scenario.
    Syntax:
    @Test(dependsOnMethods={“TC001_Login”})
  6. What is listener in TestNG? Mention some of those
    Listeners are inferaces which listen to webdriver events and change behaviour of TestNG accordingly.
    @Listeners annotation will apply to your entire suite file
    List of some Annotations:

      IInvokedMethodListener- A listener that gets invoked before and after a method is invoked by TestNG.
      IMethodInterceptor- This is used to alter the list of test methods that TestNG is about to run.
      Only methods that have no dependents and that don’t depend on any other test methods will be passed in parameter.
      IReporter- The IReporter instance receives a list of objects that describe the entire test run. It generates a report.
      ISuiteListener- Listener for test suites. It will listen at the starting and ending of test suite.
      ITestListener- Listnens when a test starts, passes, fails, etc.. It will provide empty methods which one can
      override in case of pass or failure to take further steps.
  7. How to pass a parameter in TestNG.xml?
    You can pass parameter in TestNG, if parameter consists of simple/few values. Mention value iin parameter tags of TestNG file like below.

            parameter name="testphase" value="uat"
    

    To capture the passed parameter there must be @parameter annotation in the script class.
    For example:

                    
            @Parameters({ "testphase" })
            @Test
            public void testphaseValidator(String testphase) {
                    System.out.println("Testing phase is " + testphase);
            }  
    
  8. How to define priority of test case in TestNG ?
    You can define priority of test case in @Test, Lesser priority values will get executed first. If both contains same priority, it will execute in
    alphabetical order.

            @Test(priority=1)
    
  9. How to run a method even the method on which its depend is failing?
    This can be achieved by setting alwaysRun to True in TestNG.

            @Test(alwaysRun=true)
    
  10. How to form test cases into groups?
    TestNG allows you to perform groupings of test methods. It can be either test or suite tags in testng.xml.
    Let us say you have IntegratedTests and UAT tests. You can divide test cases into these groups.
    Step1: Inclde group name in @Test annoatation in your test class(one test can be part of multiple groups):

           
                public class myTest {
                    @Test(groups = { "IST", "UAT" })
                    public void testMethod1() {
                    }
                   
                    @Test(groups = {"IST"} )
                    public void testMethod2() {
                    }
                   
                    @Test(groups = { "UAT" })
                    public void testMethod3() {
                    }
                  }
        
    

    Step2: Include groups in testng.xml in group tag. Running only IST group tests in this example.

    
    <test name="myTest">
        <groups>
            <run>
                <include name="IST">
                </include>
            </run>
       </groups>
      <classes>
           <class name="example1.Test1">
           </class>
      </classes>
    </test>
    


  11. How to run same method n number of times?
    This can be achieved by setting invocationCount to n in TestNG.

            @Test(invocationCount=n)
    
  12. How to perform parallel execution using TestNG?
    You can use TestNG to run your tests in separate threads in various ways.
    It can be Parallel tests, classes or methods. Add parallel attribute on the suite tag in testng.xml.

            
        Parallel execution of methods:
        <suite name="My suite" parallel="methods" thread-count="5">
        Parallel execution of tests:
        <suite name="My suite" parallel="tests" thread-count="5">
        Parallel execution of classes:
        <suite name="My suite" parallel="classes" thread-count="5">
        Parallel execution of instances:
        <suite name="My suite" parallel="instances" thread-count="5">
      
    

    Here thread-count allows you to specify how many threads should be allocated for this execution.

  13. How to disable a method in testNG?
    Disabling a test case/method can be done by setting enabled to “false ” in TestNG for @Test annotation.

            @Test(enabled=false)
    
  14. How to retry failed test cases in testNG?
    TestNG will automatically retry a test whenever it fails using retry analyzer interface.
    IRetryAnalyzer – Add this to tests which you want to go for retry whenever it fails.
    Implementation :
    Step 1: Implement IRetryAnalyzer interface.(Below example lretry implemented interface.)
    Step 2: Use implemented interface in @Test tag of respective test case.

       
            Step1:    
             
                    import org.testng.IRetryAnalyzer;
                    import org.testng.ITestResult;
                     
                    public class lretry implements IRetryAnalyzer {
                     
                      private int retryCount = 0;
                      private static final int maxRetryCount = 5;
                     
                      @Override
                      public boolean retry(ITestResult result) {
                        if (retryCount < maxRetryCount) {
                          retryCount++;
                          return true;
                        }
                        return false;
                      }
                    }
    
    Step 2:  
                    import org.testng.Assert;
                    import org.testng.annotations.Test;
                                     
                    public class TestclassSample {
                        @Test(retryAnalyzer = lretry.class)
                        public void testtoexecute{
                            }
                    }
                                    
       
    
  15. What is The Test Timeout in Testng?
    Test time out is the value which contains the maximum number of milliseconds a test should take.

           
            @Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
                public void testcase1() {
                    //Steps
                }
                
    

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.