RE: Second Test fails on TestNG test suite

Just curious to know that anybody else has faced the issue with TestNG xml, that the first test runs perfectly fine but second test of the test suite fails. Running test using testng xml

The issue is that the testng is trying to run the second test on the driver which was opened in the first test case. Even though the first driver was opened and closed in the first test case itself and a new driver instance is opened in the second, still it is ignoring the second driver.

Root cause is Testng xml passing the first driver to second test case internally.


<suite name="First Suite" >

<test name="InjuryBenfCal" >

<classes>

<class name="testCases.InjuryBenfCal_001" />

<class name="testCases.InjuryBenfCal_002" />

</classes>

</test>

</suite>
Lakshay Sharma Professor Asked on October 29, 2014 in Selenium WebDriver.
Add Comment
2 Answers

Hi

I would like to add something to it
From your question what i can figure is you have multiple classes and having multiple methods and driver in intialized at class level

what happens in this case


public class TestClass{

WebDriver driver = new FirefoxDriver();
public void testMethod1(){

driver.get(something);

driver.quit();

}
public void testMethod2(){

driver.get(something);

driver.quit()

}


Now if i take above example then test1 should work perfectly fine and it quits the driver gracefully , but when you come to testMethod2 then same driver object is used which doenot have any firefox window attached , so it is bound to fail

As Lakshay mentioned you can use @BeforeMethod

so sample code would be


public class TestClass{

WebDriver driver = null;
public void setupDriver(){

driver = new FireFoxDriver();

}

public void testMethod1(){

driver.get(something);

driver.quit();

}
public void testMethod2(){

driver.get(something);

driver.quit()

}

Supporter Answered on November 7, 2014.

I wish i could edit the code ,

please decorate setupDriver with @BeforeMethod

on November 7, 2014.

Thanks Kapil for your help.

All of my testcases have Before, Test & After method and in every test case I instantiate a new browser in Before method and quit the broswer in After method. And in every test case I just have one Test method. I could under theproblem if i would have used many Test methods in a single class, but here I have completely separate test with separate class files, then why testng xml providing driver of testcase 1 to testcase 2. I think its a defect for TestNg.

Regards,
Lakshay Sharma

on November 7, 2014.

Hi LakshaySorry to say , your comment is very confusing Could you share some code with me , I am really not able to understandĀ I really doubt how can testNG XML pass the test instance , and which version of TestNG you are usingĀ 

on November 7, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.