Where we learn technology

Author: Naveen AutomationLabs (Page 7 of 7)

TestNG Interview Questions

TestNG Interview Questions
 

1. What is TestNG?
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing to integration testing. For more information.
 

2. What are the advantages of TestNG?
·      TestNG provides parallel execution of test methods
·      It allows to define dependency of one test method over other method
·      It allows to assign priority to test methods @Test(priority=1, groups = “Login”)
·      It allows grouping of test methods into test groups
·      It has support for parameterizing test cases using @Parameters annotation
·      It allows data driven testing using @DataProvider annotation
·      It has different assertions that helps in checking the expected and actual results
·      Detailed (HTML) reports
·      Testng Listeners
 

 
3. What are the annotations available in TestNG?
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test
@DataProvider
@Parameters
 

 
4. Can you arrange the below testng.xml tags from parent to child? 

 
5. How to create and run testng.xml ? 
In TestNG framework, we need to create testng.xml file to create and handle multiple test classes. We do configure our test run, set test dependency, include or exclude any test, method, class or package and set priority etc in the xml file.
View Complete Post
 

 
 
 
6. What is the importance of testng.xml file?
In a Selenium TestNG project, we use testng.xml file to configure the complete test suite in a single file. Some of the features are as follows.
   testng.xml file allows to include or exclude the execution of test methods and test groups
   It allows to pass parameters to the test cases
   Allows to add group dependencies
   Allows to add priorities to the test cases
   Allows to configure parallel execution of test cases
   Allows to parameterize the test cases
 

 
 
7. How to pass parameter through testng.xml file to a test case?
We could define the parameters in the testng.xml file and then reference those parameters in the source files.
x

Create a java test class, say, ParameterizedTest.java and add a test method say parameterizedTest() to the test class. This method takes a string as input parameter. Add the annotation @Parameters(“browser”) to this method.
// TestNG Interview Questions

public class ParameterizedTest {
            @Test
            @Parameters(“browser”)
            public void parameterizedTest(String browser){
                        if(browser.equals(“firefox”)){
                                    System.out.println(“Open Firefox Driver”);
                        }else if(browser.equals(“chrome”)){
                                    System.out.println(“Open Chrome Driver”);
                        }
            }          
}
The parameter would be passed a value from testng.xml, which we will see in the next step.
We could set the parameter using the below syntax in the testng.xml file. 

Here, name attribute represents the parameter name and value represents the value of that parameter.
Practical Example
 

 
8. What is TestNG Assert and list out common TestNG Assertions?
TestNG Asserts help us to verify the condition of the test in the middle of the test run. Based on the TestNG Assertions, we will consider a successful test only if it is completed the test run without throwing any exception.
Some of the common assertions supported by TestNG are
 

   assertEqual(String actual,String expected)
   assertEqual(String actual,String expected, String message)
   assertEquals(boolean actual,boolean expected)
   assertTrue(condition)
   assertTrue(condition, message)
   assertFalse(condition)
   assertFalse(condition, message)
For Complete Post
 

 
9. What is Soft Assert in TestNG?
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.
If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.
Practical Example
 

 
T1(){
1 sendkeys()
2 HA –passed à 3 else à terminate test case / failed
3 SA — passed
4 SA  — failed à 5
5 SA – passed à 6
6 SA – Failed
 

softAssert.assertAll();
 

 
 
}
 

 
 
 
10. What is Hard Assert in TestNG?
Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test
 

 
11. What is exception test in TestNG?
TestNG gives an option for tracing the Exception handling of code. You can verify whether a code throws the expected exception or not. The expected exception to validate while running the test case is mentioned using the expectedExceptions attribute value along with @Test annotation.
 

@Test
t1(expectedExceptions = ElementNotFoundException){
 

}
 

 
 
 
12. How to set test case priority in TestNG?
We use priority attribute to the @Test annotations. In case priority is not set then the test scripts execute in alphabetical order.
// TestNG Interview Questions

package TestNG;
import org.testng.annotations.*;
public class PriorityTestCase{
           
@Test(priority=0)
            public void testCase1() { 
                        system.out.println(“Test Case 1”);
            }
           
@Test(priority=1)
            public void testCase2() {          
                        system.out.println(“Test Case 2”);
            }
}
Output:
Test Case 1

Test Case 2
 
 
13. What is Parameterized testing in TestNG?
Parameterized tests allow developers to run the same test over and over again using different values.
There are two ways to set these parameters:
   using testng.xml – 
   using Data Providers – 
 

 
14. How can we create data driven framework using TestNG?
By using @DataProvider annotation,  we can create a Data Driven Framework.
 

 
// TestNG Interview Questions
@DataProvider(name=”getData”)
            public Object[][] getData(){
                        //Object [][] data = new Object [rowCount][colCount];
                        Object [][] data = new Object [2][2];
                       
                        data [0][0] = “FirstUid”;
                        data [0][1] = “FirstPWD”;
                       
                        data[1][0] = “SecondUid”;
                        data[1][1] = “SecondPWD”;
                       
                        return data;
                       
            }
Practical Example
 

 
 
 
 
15. How to run a group of test cases using TestNG?
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set.  This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.
Groups are specified in your testng.xml file and can be found either under the or tag. Groups specified in the tag apply to all the tags underneath.
 

@Test (groups = { “smokeTest”, “functionalTest” })
public void loginTest(){
System.out.println(“Logged in successfully”);
}
 
 
 
16. How to create Group of Groups in TestNG?
Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group all that includes smokeTest and functionalTest. Let’s modify our testng.xml file as follows:
 

  
             
             
  
  
        
          

 
 
17. How to run test cases in parallel using TestNG?
we can use “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG
The parallel attribute of suite tag can accept four values:
 

tests – All the test cases inside tag of testng.xml file will run parallel
classes – All the test cases inside a java class will run parallel
methods – All the methods with @Test annotation will execute parallel
instances – Test cases in same instance will execute parallel but two methods of two different instances will run in different thread.

 
 
 
18. How to exclude a particular test method from a test case execution? 
By adding the exclude tag in the testng.xml

 
    
      
    
       

 
19. How to exclude a particular test group from a test case execution? 
By adding the exclude tag in the testng.xml

   
             
         

 
 
20. How to disable a test case in TestNG ?
To disable the test case we use the parameter enabled = false to the @Test annotation.
@Test(enabled = false)

 
 
21. How to skip a @Test method from execution in TestNG?
By using throw new SkipException()

 
Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.
throw new SkipException(“Skipping – This is not ready for testing “);

 
 
22. How to Ignore a test case in TestNG?
To ignore the test case we use the parameter enabled = false to the @Test annotation.
@Test(enabled = false)

 
 
 
 
23. How TestNG allows to state dependencies?
TestNG allows two ways to declare the dependencies.
Using attributes dependsOnMethods in @Test annotations –Using attributes dependsOnGroups in @Test annotations –
 

 
 
 
24. What are the different ways to produce reports for TestNG results?
TestNG offers two ways to produce a report.
Listeners implement the interface org.testng.ITestListener and are notified in real time of when a test starts, passes, fails, etc…
Reporters implement the interface org.testng.IReporter and are notified when all the suites have been run by TestNG. The IReporter instance receives a list of objects that describe the entire test run.
 

 
 
25. What is the use of @Listener annotation in TestNG?
TestNG listeners are used to configure reports and logging. One of the most widely used listeners in testNG is ITestListener interface. It has methods like onTestStart, onTestSuccess, onTestFailure, onTestSkipped etc. We should implement this interface creating a listener class of our own. Next we should add the listeners annotation (@Listeners) in the Class which was created.
 

 
 
26. How to write regular expression In testng.xml file to search @Test methods containing “smoke” keyword.
 

Regular expression to find @Test methods containing keyword “smoke” is as mentioned below.
 
27. What is the time unit we specify in test suites and test cases? 
 

We specify the time unit in test suites and test cases is in milliseconds.
 

 
 
28. List out various ways in which TestNG can be invoked?
TestNG can be invoked in the following ways
   Using Eclipse IDE
   Using maven/ant build tool
   From the command line
   Using IntelliJ’s IDEA
 

 
29. How To Run TestNG Using Command Prompt?
 

C: test
Java c://testing.jar test.java
 

 
30. What is the use of @Test(invocationCount=x)?
The invocationcount attribute tells how many times TestNG should run a test method
@Test(invocationCount = 10)

public void testCase1(){
In this example, the method testCase1 will be invoked ten times
 

 
 
31. What is the use of @Test(threadPoolSize=x)?
The threadPoolSize attribute tells to form a thread pool to run the test method through multiple threads.
Note: This attribute is ignored if invocationCount is not specified
@Test(threadPoolSize = 3, invocationCount = 10) public void testCase1(){

In this example, the method testCase1 will be invoked from three different threads
 

 
32. What does the test timeout mean in TestNG?
The maximum number of milliseconds a test case should take.
@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)

public void testCase1(){
In this example, the function testCase1 will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever.
 

 
 
33. What are @Factory and @DataProvider annotation?
@Factory: A factory will execute all the test methods present inside a test class using a separate instance of the respective class with different set of data.
@DataProvider: A test method that uses DataProvider will be executed the specific methods multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.

For any feedback, feel free to reach me. You can write to me at naveenanimation20@gmail.com

 

Advanced Selenium Webdriver Interview Questions and Answers in Java

Advanced Selenium Webdriver Interview Questions and Answers in Java

Selenium WebDriver – QA

Here is the list of Selenium Interview questions and answers.


How to read data from table? 

You can easily read the data from table using findElements method. 

Find all the table cells using TD tag and then get the text inside each cell by getText() method or innerText property in HTML DOM.


What all browsers and OS are supported by Selenium? 

Selenium supports below browsers.
  1. Internet Explorer
  2. Google Chrome
  3. Firefox
  4. Safari
  5. Opera
  6. Android browser
  7. iPhone Browser
Operating systems supported by Selenium are –
  1. All Windows OS
  2. Linux
  3. Mac (Apple OS)
  4. Android
  5. IOS
Various Synchronization methods in Selenium Webdriver – implicit wait, 
page load timeout, explicit wait?
There are 4 ways we can handle synchronization in Selenium.
  1. Implicit
  2. Explicit using wait and expected conditions
  3. Browser Navigation timeout
  4. Script time out 
How to take screenshot in Selenium? 
Selenium provides an interface called TakesScreenshot which has a method getScreenShotAs which can be used to take a screenshot of the application under test.

How to execute JavaScript in Selenium? How many arguments 
can be passed to executeScript method? 
Selenium webdriver API provides one interface called 
JavascriptExecutor with a method called executeScript. 
This method has 2 main arguments. 
First argument is the javascript code you want to execute on current page and second argument is optional but important. 
We can pass variable number of web elements as an arguments which can be manipulated in javascript code passed in the first argument.

How to handle multiple browser windows and frames?
We can easily work with multiple windows and frames by switching to them. 
We can find all open windows in selenium using getWindowhandles method. 
We can find frame in the web page using name, id or any other method like css or xpath selectors.

How to drag and drop in Selenium Webdriver? 
Selenium webdriver provides Actions class which has many methods to support complex user actions like contextClick, doubleClick, dragAndDrop. 

How to use Selenium Grid?
Selenium grid is used to execute selenium scripts on various environments (OS and browsers) simultaneously and on different machines. 
Hub acts as a central server which sends the execution requests to the other machines called as nodes. 
Hub server and node server must be up and running. 
Node machine must be registered with HUB. Depending upon the webdriver capabilities, HUB routes the request to appropriate node to execute the scripts.

Challenges, issues and limitations of Selenium tool? 

How to handle Captcha? 
We can not handle captcha in Selenium. We can not also automate 
the Adobe Flash contents on webpages. These are some of the limitations of Selenium.
Other common issues are –
  1. Certificate Errors in IE
  2. Protect mode error
  3. Browser zoom error in IE
  4. Canvas image map
  5. Unsupported command line flag – ignore certificates warning in Chrome
What is the page object model in Selenium? 
Page object model is an alternative way to handle automation project.

Write a complex xpath or css expression? Which one is better? 
xpath and css can be used to find any element on the web page.
 Most of the engineers prefer writing xpath expressions as finding r
elative elements is very easy in xpath.

How to upload files using AUTOIT Script, Robot?
We can use AUTOIT/Robot/Sikuli code to handle the window dialogs 
which are not supported by web driver.

Flash automation, silverlight Automation? 
Selenium has no support for these controls. We need to use third party tools to automate these apps.

How to format date and time in Selenium in Java? How to compare dates? 
We can format date in selenium using DateFormat object.

How to append a data to file in Selenium?
We can use File and FileWriter class to work with files in Selenium Webdriver.

Frameworks in Selenium – Data driven, Hybrid, Keyword, 

BDD – Cucumber, TestNG?
Frameworks are used to speed up the automation testing by 
developing the test data, reusable functions and reports.

Database testing in Selenium? 
We can use JDBC driver to connect to any database in Java.

How to work with Excel files in Selenium?

We can use JXL or POI library to work with excel files in Selenium.

Find total time of execution in Selenium? 
If we are using custom library, we can log the start and end time 
of the execution and then find the total execution time by 
subtracting end time from start time.

How to schedule the Test Suite Execution?
We can schedule the test suite execution using CI tools like 
hudson(Jenkins), Bamboo. Alternatively, we can use windows 
scheduler to launch the test execution.

How to send an email stating the execution status to all stakeholders in Selenium? 
We can send mail in Java using javax.mail library.

What is the difference between findElement and findElements?
findElement returns the first matching element. If element is not found, NoElementFoundException is thrown. findElements 
returns the list of matching web elements. 
If no element is found, empty list is returned. Exception is not thrown.

What is the difference between Junit and TestNG. What are the various annotations and features? 
TestNG is the new generation testing framework with lot of new 
useful features as compared to old Junit framework. 
Using TestNG, we can run the tests in parallel.

Automation of Android apps and Android Browsers using Selenium possible?

We can use  Appium and Selendroid tools to automate mobile applications.

How to handle Ajax calls in Selenium?
When working with Ajax calls, we must be very careful to handle StaleElementStateException. This exception occurs 
when we refer to the stale (Old) element on page. 
To handle this error, we need to find the element again after ajax load new elements in the page.

Difference between Selenium and QTP. Competitors to Selenium Webdriver? 
QTP is a licensed tool supporting automation of lot of applications based on .Net, Java, Web and much more. Selenium is a free tool and supports only web application automation testing.

What is desired capabilities?
Capabilities are used to set the values of the browser attributes 
before we launch any browser using selenium web driver.

Version control tools like SVN, GIT? 
We use version control tools like SVN to track the changes to 
the files in a project and work in collaboration.

Build tools – Ant, Maven? 
We use these tools to manage build activities for the Java project.

CI tools – Jenkin, Bamboo? 

These are continuous integration tools helping in quick 
deployment of applications, testing them and reporting the issues in the code before it is too late. 
It helps in getting the application into production quickly and with more quality confidence.

How to identify the frame which does not have Id as well as name? 

We can identify the frame using xpath or css selectors as well.

What is the alternative to sendkeys? 

We can use JavaScriptExecutor to work with html controls.

How to press TAB key in Selenium? 

We can press any keyboard key using Keys class.

What are the Exception that may occur in Selenium Webdriver 

Selenium may throw below exceptions.

  1. InvalidElementStateException
  2. StaleElementReferenceException
  3. ElementNotFoundException
  4. ElementNotVisibleException 

For any feedback, feel free to reach me. You can write to me at naveenanimation20@gmail.com

Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:
Newer posts »