TestNG

1) What are the advantages of using TestNG?

– It 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

– 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

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

<test>

<suite>

<class>

<methods>

<classes>

<suite><test><classes><class><methods>

3) What is the importance of testng.xml file?

TestNG.xml file is an XML file which contains all the Test configuration and this XML file can be used to run and organize our test.

4) What is TestNG Assert and list out common TestNG Assertions?

Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. Some of the common assertions are:

– assertEqual(String actual,String expected)

– assertTrue(condition)

– assertFalse(condition)

5) What is Soft Assert in TestNG?

SoftAssert don’t throw an exception when an assert fails. The test execution will continue with the next step after the assert statement.

6) What is Hard Assert in TestNG?

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

7) How to create Group of Groups in TestNG?

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd“>

<suite name=”TestNG Sample Test Suite”>

<test name=”TestNG Sample Test”>

<groups>

<define name=”include-groups”>

<include name=”sanity” />

<include name=”integration” />

</define>

<define name=”exclude-groups”>

<include name=”pass” />

<include name=”fail” />

</define>

<run>

<include name=”include-groups” />

<exclude name=”exclude-groups” />

</run>

</groups>

<classes>

<class name=”com.groups.TestNGGroupsExample” />

</classes>

</test>

</suite>

8) How to exclude a particular test method from a test case execution using TestNG?

<suite name=”Sample Test Suite” verbose=”1″ >

<test name=”Sample Tests” >

<classes>

<class name=”com.test.sample”>

<methods>

<exclude name=”TestA” />

</methods>

</class>

</classes>

</test>

</suite>

9) How to exclude a particular group from a test case execution using TestNG?

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd“>

<suite name=”Test Suite”>

<groups>

<run>

<exclude name=”integration”></exclude>

</run>

</groups>

<test name=”Test”>

<classes>

<class name=”TestNGGroupsExample” />

</classes>

</test>

</suite>

10) How to disable a test case in TestNG?

@Test(enabled = false)

public void test() {

System.out.println(“This test is disabled”);

}

11) How to ignore a test case in TestNG?

@Test(enabled = false)

public void test() {

System.out.println(“This test is ignored”);

}

12) What are the different ways to produce reports for TestNG results?

There are two ways to generate a report with TestNG −

Listeners − For implementing a listener class, the class has to implement the org.testng.ITestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes.

Reporters − For implementing a reporting class, the class has to implement an org.testng.IReporter interface. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called.

13) How to write regular expressions in testng.xml file to search @Test methods containing “smoke” keyword?

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd“>

<suite name=”testSuite”>

<test name=”test”> <classes>

<class name=”sample”>

<methods>

<exclude name=”smoke.*”/>

</methods>

</class>

</classes>

</test>

</suite>

14) What is the time unit we specify in test suites and test cases?

Time unit is Milliseconds

15) List out various ways in which TestNG can be invoked?

– Command Line

– Maven

– IDE

16) How to run TestNG using command prompt?

java -cp C:\Selenium\SampleTest\lib\*;C:\Selenium\SampleTest\bin org.testng.TestNG testng.xml

17) What is the use of @Test(invocationCount=x)?

The invocationcount attribute tells how many times TestNG should run a test method. In this example, the method testCase will be invoked ten times:

@Test(invocationCount = 10)

public void testCase(){

System.out.println(“Invocation method”);

}

18) What is the use of @Test(threadPoolSize=x)?

The threadPoolSize attribute tells TestNG to create a thread pool to run the test method via multiple threads. With thread pool, it will greatly decrease the running time of the test method.

Example: Start a thread pool, which contains 3 threads, and run the test method 3 times

@Test(invocationCount = 3, threadPoolSize = 3)

public void testThreadPools() {

System.out.printf(“Thread Id : %s%n”, Thread.currentThread().getId());

}

19) What does the test timeout mean in TestNG?

While running test methods there can be cases where certain test methods get struck or may take longer time than to complete the execution than the expected. We need to handle these type of cases by specifying Timeout and proceed to execute further test cases / methods

@Test(timeOut=5000)

public void executeTimeOut() throws InterruptedException{

Thread.sleep(3000);

}

20) What is JUnit?

JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run repeatable tests.

21) What are JUnit annotations?

– @Test

– @ParameterizedTest

– @RepeatedTest

– @TestFactory

– @TestTemplate

– @Disabled

– @Tag

– @AfterAll

– @BeforeAll

– @BeforeEach

– @AfterEach

– @TestInstance

22) What is TestNG and what is its use?

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. It is used for:

– Annotations

– Run your tests in arbitrarily big thread pools with various policies available

– Test that your code is multithread safe

– Flexible test configuration

– Support for data-driven testing

– Support for parameters

– Powerful execution model

– Supported by a variety of tools and plug-ins

– Embeds BeanShell for further flexibility

– Default JDK functions for runtime and logging

– Dependent methods for application server testing

23) How is TestNG better than JUnit?

– TestNG supports more annotations than Junit

– TestNG supports ordering of tests but Junit doesn’t

– TestNG supports various types of listeners using annotations but Junit doesn’t

– TestNG reports are better than Junit

24) How to set test case priority in TestNG?

@Test (priority=1)

public void openBrowser() {

driver = new FirefoxDriver();

}

@Test (priority=2)

public void launchGoogle() {

driver.get(“http://www.google.co.in“);

}

25) How to pass parameters through testng.xml to a test case?

public class ParameterizedTest {

@Test

@Parameters(“name”)

public void parameterTest(String name) {

System.out.println(“Parameterized value is : ” + name);

}

}

<?xml version = “1.0” encoding = “UTF-8”?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >

<suite name = “Suite1”>

<test name = “test1”>

<parameter name = “name” value=”bijan”/>

<classes>

<class name = “ParameterizedTest” />

</classes>

</test>

</suite>

1) What is the difference between Manual and Automation Testing?

– Manual Testing shows lower accuracy due to the higher possibilities of human errors. Automation Testing depicts a higher accuracy due to computer-based testing eliminating the chances of errors.

– Manual Testing needs time when testing is needed at a large scale. Automation Testing easily performs testing at a large scale with the utmost efficiency.

– Manual Testing takes more time to complete a cycle of testing, and thus the turnaround time is higher. Automation Testing completes a cycle of testing within record time and thus the turnaround time is much lower.

– Manual Testing should be used to perform Exploratory Testing, Usability Testing and Ad-hoc Testing to exhibit the best results. Automation Testing should be used to perform Regression Testing, Load Testing, Performance Testing and Repeated Execution for best results.

– Exploratory testing is possible in Manual Testing. Automation does not allow random testing.

– The initial investment and ROI in the Manual testing is lower compared to Automation testing in the long run. The initial investment in the automated testing is higher and the ROI is better in the long run.

2) What are the benefits of Automation Testing?

– ROI: Even though the initial investment is high, return on investment over time is much better

– Running tests: Tests can be run automatically or can be scheduled at a particular time

– Fewer human resources: Manual testers are not required

– Reusability: Scripts are reusable

– Bugs: Automation helps you find bugs in the early stages of software development

– Reliability: Automated testing is more reliable and way quicker

– Parallel testing: Automated tests can be run parallelly on multiple devices

3) Which Test cases needs to be automated?

– Tests that need to be run against every build/release of the application, such as smoke test, sanity test and regression test.

– Tests that need to run against multiple configurations — different OS & Browser combinations.

– Tests that execute the same workflow but use different data for its inputs for each test run e.g. data-driven.

– Tests that involve inputting large volumes of data, such as filling up very long forms.

– Tests that take a long time to perform and may need to be run during breaks or overnight.

4) What are the popular test automation tools for functional testing?

– Selenium

– Unified Functional Testing

– Test Complete

– Ranorex

– Tosca

5) What is the main purpose of Automation Testing?

Automation testing is the best way to increase the effectiveness, efficiency and coverage of your software testing.

6) What is the goal of Automation Testing?

– To reduce Testing Cost and Time.

– To reduce Redundancy.

– To speed up the Testing Process.

– To help improve Quality.

– To improve Test coverage.

– To reduce Manual Intervention.

7) Why Selenium should be selected as a Test tool?

– Language & Framework support: Selenium supports all major languages like Java, Python, JavaScript, C#, Ruby, and Perl programming languages for software test automation. Also, every Selenium supported language has dedicated frameworks which help in writing test script for Selenium test automation.

– Open source: Being an open source tool, Selenium is a publicly accessible automation framework and is free, with no upfront costs.

– Multi browser support: Selenium script is compatible with all browsers like Chrome, Safari, IE, etc …

– Support across various OS: Selenium is yet a highly portable tool that supports and can work across different operating systems like Windows, Linux, Mac OS, UNIX, etc.

– Ease of implementation: Selenium automation framework is very easy-to-use tool. Selenium provides a user-friendly interface that helps create and execute test scripts easily and effectively.

– Reusability and Integrations: Selenium needs third-party frameworks and add-ons to broaden the scope of testing.For example, it can integrate with TestNG and JUnit for managing test cases and generating reports.

– Parallel Test Execution: With the help of Selenium Grid, we can execute multiple tests in parallel, hence reducing the test execution time.

8) What are the testing types that can be supported by Selenium?

– Functional Testing

– Regression Testing

– Sanity Testing

– Smoke Testing

– Responsive Testing

– Cross Browser Testing

– UI testing (black box)

– Integration Testing

9) What are the limitations of Selenium?

– Selenium does not support automation testing for desktop applications.

– Since Selenium is open source software, you have to rely on community forums to get your technical issues resolved.

– It does not have built-in Object Repository like UTF/QTP to maintain objects/elements in centralized location.

– Selenium does not have any inbuilt reporting capability and you have to rely on plug-ins like JUnit and TestNG for test reports.

10) What is the difference between Selenium IDE, Selenium RC and Selenium WebDriver?

Selenium IDE:

– It only works in Mozilla browser.

– It supports Record and playback

– Doesn’t required to start server before executing the test script.

– It is a GUI Plug-in

– Core engine is Javascript based

– Very simple to use as it is record & playback.

– It is not object oriented

– It does not supports listeners

– It does not support to test iphone/Android applications

Selenium RC:

– It supports with all browsers like Firefox, IE, Chrome, Safari, Opera etc.

– It doesn’t supports Record and playback

– Required to start server before executing the test script.

– It is standalone java program which allow you to run Html test suites.

– Core engine is Javascript based

– It is easy and small API

– API’s are less Object oriented

– It does not supports listeners

– It does not support to test iphone/Android applications

Selenium Webdriver:

– It supports with all browsers like Firefox, IE, Chrome, Safari, Opera etc.

– It doesn’t supports Record and playback

– Doesn’t required to start server before executing the test script.

– It is a core API which has binding in a range of languages.

– Interacts natively with browser application

– As compared to RC, it is bit complex and large API.

– API’s are entirely Object oriented

– It supports the implementation of listeners

– It support to test iphone/Android applications

11) When should I use Selenium IDE?

Because of its simplicity, Selenium IDE should only be used as a prototyping tool, not an overall solution for developing and maintaining complex test suites.

12) What is Selenese?

Selenese is the set of selenium commands which are used to test your web application. Tester can test the broken links, existence of some object on the UI, Ajax functionality, Alerts, window, list options and lot more using selenese.

13) What is the difference between Assert and Verify commands?

In case of the “Assert” command, as soon as the validation fails the execution of that particular test method is stopped and the test method is marked as failed. Whereas, in case of “Verify”, the test method continues execution even after the failure of an assertion statement.

14) What is Same Origin Policy and how it can be handled? How to overcome same origin policy through web driver?

Selenium uses java script to drives tests on a browser. Selenium injects its own js to the response which is returned from aut. But there is a java script security restriction (same origin policy) which lets you modify html of page using js only if js also originates from the same domain as html.

15) How do you use findElement() and findElements()?

WebElement login= driver.findElement(By.linkText(“Login”));

List<WebElement> listOfElements = driver.findElements(By.xpath(“//div”));

16) Can Selenium handle window based pop up?

No. Selenium cannot handle window based pop-up on its own but can use third party tools.

17) How can we handle window based pop up using Selenium?

We can handle window based popups using some third party tools such as AutoIT, Robot class.

18) How can we handle web-based pop up using Selenium?

Step 1: After opening the website, we need to get the main window handle by using driver.getWindowHandle();

Step 2: We now need to get all the window handles by using driver.getWindowHandles();

Step 3: We will compare all the window handles with the main Window handles and perform the operation the window which we need.

19) How to assert title of the web page?

String actualTitle = driver.getTitle();

String expectedTitle = “Title of Page”;

assertEquals(expectedTitle,actualTitle);

20) How to mouse hover on a web element using WebDriver?

Actions builder = new Actions(driver);

builder.moveToElement(hoverElement).perform();

21) How to retrieve CSS Properties of an element?

driver.findElement(By.id(“by-id”)).getCssValue(“font-size”);

22) What is JUnit?

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks.

23) What are JUnit annotations?

Annotation is a special form of syntactic meta-data that can be added to Java source code for better code readability and structure.

Some of them are:

– @Before

– @After

– @BeforeClass

– @AfterClass

– @Ignore

– @RunWith

– @Test

24) What is TestNG and what is its use?

TestNG is an automation testing framework in which NG stands for “Next Generation”. TestNG is inspired from JUnit which uses the annotations (@). Using TestNG you can generate a proper report, and you can easily come to know how many test cases are passed, failed and skipped.

25) How is TestNG better than JUnit?

– In TestNG Annotations are easy to understand over JUnit.

– TestNG enable you to grouping of test cases easily which is not possible in JUnit.

– TestNG allows us to define the dependent test cases each test case is independent to other test case.

– Parallel execution of Selenium test cases is possible in TestNG.

1) Explain the usage of different annotations available in TestNG?

@BeforeSuite – Method will run before all tests run in the suite

@AfterSuite – Method will run after all tests run in the suite

@BeforeTest – Method will run before any test method within the class is run

@AfterTest – Method will run after all test methods within the class have run

@BeforeClass – Method will run before first test method in the class is invoked

@AfterClass – Method will run after all test methods in the class have run

@BeforeMethod – Method will run before each test method

@AfterMethod – Method will run after each test method

2) How do you prioritize your test cases in Selenium?

We can use priority method to set the priority of test method to run.

@Test(priority=1)

public void ClickA(){

System.out.println(“Test will execute first”);

}

@Test(priority=2)

public void ClickB(){

System.out.println(“Test will execute second”);

}

3) Suppose you want to skip one test method from execution, how do you skip it from execution?

We can use enabled method to enable or disable test in TestNG.

@Test(enabled=false)

public void login(){

}

4) What is the hierarchy of testNG.xml tags?

Below is the hierarchy of TestNG.xml tags:

<suite>

<test>

<classes>

<class>

<methods>

5) Explain the structure of testng.xml file?

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd“>

<suite name=”Test Suite” verbose=”1″>

<test name=”Regression Tests”>

<classes>

<class name=”com.example.TestRunner”>

</class>

</classes>

</test>

</suite>

6) What are the different methods of Assert?

– assertEqual(): Compares two strings are equal and fails if both are not equal

– assertTrue(): Checks boolean condition is true and fails if it is false

– assertFalse(): Checks boolean condition is false and falis if it is true

7) How do you store your TestNG reports?

TestNG default reports are stored in target output folder of the project.

8) How do you use Parameters in TestNG?

@Parameters({“username”,”password”})

@Test

public void login(String username, String password){

System.out.println(username);

System.out.println(password);

}

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd“>

<suite name=”Test Suite” verbose=”1″>

<test name=”Regression Tests”>

<parameter name=”username” value=”qascript”/>

<parameter name=”password” value=”qascript123″/>

<classes>

<class name=”com.example.Tests”>

</class>

</classes>

</test>

</suite>

9) What is the use of grouping in TestNG?

TestNG groups are used to include/exclude different tests from the execution

10) What is the difference between JUnit and TestNG?

– More annotations are present in TestNG compared to JUnit

– HTML Reporting is present in TestNG but it is not present in JUnit

– TestNG supports dependent tests but Junit does not

11) What is the difference between include and exclude in TestNG?

– All test methods which are defined in the group under include tags will be included in the execution

– All test methods which are defined in the group under exclude tags will be excluded from execution

12) How to execute the single selected method in TestNG?

We can define a group for the method and put the group within the include tags

13) How the packages and classes are structured in TestNG.xml?

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd“>

<suite name=”Test Suite” verbose=”1″>

<test name=”Regression Tests”>

<packages>

<package name=”com.example”/>

</packages>

</test>

</suite>

14) What are assertions and why we go for them in TestNG?

Assertions are used in TestNG for validating the result or outcome of a test.

It can be used to compare the expected test result with actual test result.

Test Execution status will pass or fail based on the assertion result.

15) Tell me a login page script in TestNG?

@Test

public void login(){

driver.findElement(By.id(“username”)).sendKeys(“qascript”);

driver.findElement(By.id(“password”)).sendKeys(“qascript123”);

driver.findElement(By.id(“submit”)).click();

}

16) What is the difference between @Parameters and @DataProviders in TestNG?

– @DataProvider can handle complex parameters like objects read from a property file or a database.

– @Parameters can be used to handle simple parameter values.

17) What problems you have faced while working with TestNG?

Working with DataProviders, Parallel execution and Listeners was challenging in TestNG.

18) How to create Suites in TestNG?

TestNG suite tags can be added in the testng.xml file.

19) How to prioritize the tests in TestNG at Class level and Suite level?

Priority can be set for test methods and then we can use @BeforeSuite, @BeforeClass methods to prioritize the tests at class and suite level.

20) Suppose I want to check a particular exception in TestNG. How will you check?

@Test(expectedExceptions = ArithmeticException.class)

public void exceptionTest() {

System.out.println(“Exception occurred”);

}

21) What is the difference between Maven and TestNG?

– Maven is a build automation tool used to build and manage Java projects.

– TestNG is a testing framework for Java to manage test execution.

22) Do you know any third party reporting other than testng?

There are many third-party reporting like Extent Reports, Maven Cucumber Reporting plugin.

23) What is the difference between TestNG and Grid?

– TestNG is a testing framework for Java to manage test execution.

– Grid is a selenium component which is used to execute tests on remote machines.

24) How will you mark as method as a data provider using TestNG annotation?

@Test(dataProvider=”TestData”)

25) Can you tell me usage of TestNG Soft Assertion?

Soft Assertion doesn’t throw any exception when an assert fails and the test execution doesn’t stop.

1) How do you fail test cases in TestNg?

We can use Assert.fail() method to the fail the test case

2) Sequence of execution of below annotations: @Test @BeforeGroups @AfterGroups @BeforeSuite @AfterSuite @BeforeMethod @AfterMethod @BeforeClass @AfterClass?

Below is the sequence of annotations:

@BeforeSuite

@BeforeTest

@BeforeClass

@BeforeGroups

@BeforeMethod

@Test

@AfterMethod

@AfterGroups

@AfterClass

@AfterTest

3) What is batch and group execution in TestNG?

TestNG allows to execute test methods belonging to a particular group.

4) What is the execution order??? @test1 (priority=1) @test2 (priority=2) @test3 test4(priority=3)?

Below is the execution order:

@test3

@test1(priority=1)

@test2(priority=2)

@test4(priority=3)

5) Can we start from 0 ie.prority=0; can we give priority= -12 ie. –ve no?

Yes. We can start from priority=0. But we cannot use negative priority.

6) Tell me any 5 assertions of TestNG which we can use In selenium webdriver.?

@Test, @BeforeSuite, @BeforeTest, @AfterTest, @AfterSuite

7) Tell me syntax to skip @Test method from execution.?

We can skip the test method in the following way:

@Test(enabled=false)

  1. I have a test case with two @Test methods. I want to exclude one @Test method from execution. Can I do It? How?

We can define two test methods under different groups and include only one group in TestNg.xml

9) Can you describe major features of TestNG?

– Provides different annotations to control flow of test execution

– Provides default reporting

– Parallel Execution and Multithreading can be performed

10) Can we run testNG class code without using any TestNg annotation?

No we cannot run code without any TestNG annotation.

11) How to accept exceptions in testNG?

@Test(expectedExceptions=ArithmeticException.class)

public void exceptionsExample(){

int i = 1/0;

}

12) Explain about testNG listeners?

Listeners are interfaces which allow us to modify TestNG behavior. It is mostly used to customize logs or reports.

13) Do you run test cases in parallel with TestNG? If yes how many threads and does it cause any problem?

Yes we can run test cases in parallel with multiple threads in TestNG. We can use unlimited threads but it may cause memory issues.

14) What is difference between @AfterMethod and @AfterTest?

@AfterMethod – The method will run after each test method

@AfterTest – The method will run after all the test methods belonging to class has run

15) What is the use of xml file in testng?

XML file contains all the configuration and also drives the test execution.

16) Given a scenario that 5 test cases are there I need to execute first and last 3 (means 2 one should not be executed) ? How u make a changes in testng xml file?

We can use enabled=false option for the second test method. Also we can put all the test cases

in one group except the 2nd test and then run the group from the xml file.

17) How do u configure only Required Testcases for running the TestNG suit in XML?

We can define groups for the required testcase:

@Test(group=”required”)

Customize the xml file to pick the mentioned group:

<groups>

<run>

<include name=”required” />

</run>

</groups>

18) How will you get the browser values from testng?

We can define the browser parameter value in testng.xml:

<suite name=”Test Suite”>

<test name=”Cross Browser Tests”>

<parameter name=”browser” value=”Chrome”/>

<classes>

<class name=”com.qascript.FirstTest”/>

</classes>

</test>

</suite>

We can use the parameter value in our test:

@Parameters({ “browser” })

@Test

public void browserTest(String browser) {

if(browser==”Chrome”){

driver = new ChromeDriver();

}

}

19) How to Install TestNG In Eclipse? How do you verify that TestNg Is Installed properly In Eclipse?

We need to install the TestNG plugin from the Eclipse marketplace. To verify the installation,

go to Window -> Show View -> Other. Open Java folder and check if TestNG is included.

20) How to skip a @Test method or a code block in TestNG?

Test can be skipped by using Enabled=false with @Test annotation.

21) Describe the similarities and difference between JUnit and TestNG unit testing frameworks

Similarities:

– Provides different annotations

– Ability to ignore tests

– Ability to accept exceptions

– Ability to run tests in parallel

Differences:

– Better reporting in TestNG compared to Junit

– Dependency testing is not possible in Junit

22) What is the command line we have to write inside a .bat file to execute a selenium

project when we are using testng ?

set projectLocation=C:\Projects\Selenium-TestNG

cd %projectLocation%

set classpath=%projectLocation%\bin;%projectLocation%\lib\*

java org.testng.TestNG %projectLocation%\testng.xml

pause

23) What is Error Collector in TestNG? What is its use?

The error collector rule allows test execution to continue after the first failure is

found and report them all at once.

24) How to prepare Customized html Report using TestNG in hybrid framework.?

TestNG provides ability to implement an interface IReporter to generate customized HTML reports.

25) Detail about TestNG Test Output folder.?

By default the report files are stored in a folder test-output under the project workspace.

1) What is the difference between WebDriver Listeners and TestNG Listeners?

TestNG work on test related events while WebDriver Listeners work on automation related events.

2) TestNG- Write sample code to select browser depending on parameter given in testing.xml?

We can define the browser parameter value in testing.xml:

<suite name=”Test Suite”>

<test name=”Cross Browser Tests”>

<parameter name=”browser” value=”Chrome”/>

<classes>

<class name=”com.qascript.FirstTest”/>

</classes>

</test>

</suite>

We can use the parameter value in our test:

@Parameters({ “browser” })

@Test

public void browserTest(String browser) {

if(browser==”Chrome”){

driver = new ChromeDriver();

}

}

3) How will you analyze the failures after giving a run in the TestNG framework?

We need to add logging into our TestNG framework and open the HTML Report after test run to

analyze the logs in failed tests.

4) Can we run a test without testng?

Yes we can run test using other test frameworks like Junit or Cucumber

5) Why to use TestNG with Selenium WebDriver?

TestNG is used in Selenium WebDriver due to the following:

– To control the flow of test execution with the help of annotations

– To perform cross browser testing

– To generate HTML reports

– To perform parallel testing and multithreading

6) Explain DataProviders in TestNG using an example

DataProviders in TestNG is used to provide different sets of data to the tests.

Ex-

public class dataProviderExample{

@DataProvider(name=”test-data”)

public Object[][] dataMethod(){

return new Object[][]{{“x”},{“y”}};

}

@Test(dataProvider=”test-data”)

public void test1(String value){

System.out.println(value);

}

}

7) Explain How does TestNG allow you to state dependencies with an example?

TestNG allows us to state dependencies using annotation @dependsOnMethods.

@Test(dependsOnMethods={“test2”})

public void test1(){

}

@Test

public void test2(){

}

8) List two different annotations present in TestNG but not in JUnit?

@BeforeSuite and @BeforeGroups are not present in Junit.

9) Have you conducted cross browser testing in a parallel way using TestNG?

Yes. We can perform cross browser testing in parallel with TestNG using parallel and thread-count attributes.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd“>

<suite name=”Regression” parallel=”methods” thread-count=”2″>

<test name=”ParallelTest”>

<classes>

<class name=”com.qascript.example”/>

</classes>

</test>

</suite>

10) What is parameterization in TestNG?

Parameterization can be done in 2 ways in TestNG:

– By using Parameters annotation and testng xml file

– By using DataProvider annotation

11) What are the types of assertion and what are assertion in junit?

Assertion is used to validate the test cases and Pass/Fail the test based on the result. Types of assertions are assertEquals, assertFalse, assertTrue.

12) Explain the different JUnit annotations mostly used while writing the Selenium scripts?

– @BeforeClass

– @Before

– @Test

– @After

– @AfterClass

– @Ignore

13) There are 300 test cases, I want to execute test cases in some custom order, how to change the order of execution without doing changes in testng.XML and in code(.class files). If we can do, tell the logic and if we cannot do, justify with reason?

We can execute the tests in custom order by setting priority of different tests.

Ex –

@Test(priority=0)

public void test1(){

}

@Test(priority=1)

public void test2(){

}

14) Is it possible to pass test data through testng.xml file, if yes how?

Yes. We can pass parameter and their values from testng.xml file.

15) How to run specific kind of Test cases using TestNG?

We can use groups in TestNG to run specific kind of test cases.

16) Can we execute test cases in order without using TestNG?

No. It is not possible to execute test cases in specific order without using TestNG or other test frameworks.

17) What is the testng.xml file used for?

It is the configuration file which can be used to organize tests, control flow of execution and group different tests.

18) How the testng class’s execution happen?

TestNG class execution happens as follows:

– Firstly BeforeSuite() method is executed only once

– Lastly AfterSuite() method executes only once

– BeforeTest(), BeforeClass(), AfterClass(), and AfterTest() methods are executed only once

– BeforeMethod() and AfterMethod() method executes for each test case but before/after executing the test case

– Each @Test method is executed between BeforeMethod() and AfterMethod()

19) How to parameterized your junit?

The custom runner Parameterized implements parameterized tests.

@RunWith(Parameterized.class)

20) What is an assertion? What is its drawback? How to overcome it?

Assertion is a way of validating the expected outcome of any test case.

Drawback of assertion is whenever any test case fails it will throw an exception and stop the execution.

We can use soft assertion if we want to continue execution even if there are test failures.

21) How can you prepare customized HTML report using TestNG in hybrid framework?

We can prepare customized HTML report with the helpd of different listeners:

– org.testng.ITestListener

– org.testng.IReporter

22) How do you manage re-running only failed test cases?

We can rerun failed test cases in TestNG by implementing the org.testng.IRetryAnalyzer interface.

23) How will you install ReportNG in your project?

– Include ReportNG maven dependencies in the pom.xml

<dependency>

<groupId>org.uncommons</groupId>

<artifactId>reportng</artifactId>

<version>1.1.4</version>

<scope>test</scope>

</dependency>

– Add listeners to testng.xml

<listeners>

<listener class-name=”org.uncommons.reportng.HTMLReporter”/>

<listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>

</listeners>

24) What is the difference between @BeforeMethod and @BeforeTest method?

@BeforeMethod – Annotated method is run after each every test method

@BeforeTest – Annotated method is run before any test method belonging to the class

25) What is the difference between @BeforeMethod and @BeforeClass ?

@BeforeMethod – Annotated method is run after each every test method

@BeforeClass – Annotated method is run before the first test method in the current class.