Selenium Java Interview Questions and Answers Part-20
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)
8) 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.
Next Steps:
> More interview questions and answers on Selenium Java, continue to the next post (Click on Next Post link below)
> Check complete Selenium Java interview questions and answers here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
Connect to me on Linked In (Click here)
On a mission to contribute to the Software Testing Community in all possible ways.