Issue running the same test sequentially with Selenium

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue running the same test sequentially with Selenium

jrfpi
I have this class: 

public class NewTest1
{
WebDriver driver;
@BeforeSuite(alwaysRun = true)
public void beforeSuite()
{
driver = new FirefoxDriver();
}
@BeforeMethod(alwaysRun = true)
public void beforeMethod()
{
driver.get("http://www.bing.com");
}
@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult tr)
{
if(tr.getStatus() == ITestResult.FAILURE)
{
driver = new FirefoxDriver();
}
}
@AfterTest(alwaysRun = true)
public void afterTest(ITestContext tc)
{
driver.close();
}
@Test
public void f()
{
driver.findElement(By.id("sb_form_q")).sendKeys("apple");
driver.findElement(By.id("sb_form_go")).click();
}
}

and this is my testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test verbose="2" name="Test1" enabled="true">
<classes>
<class name="Tests.NewTest1">
</class>
</classes>
</test>
<test verbose="2" name="Test2" enabled="true">
<classes>
<class name="Tests.NewTest1">
</class>
</classes>
</test>
</suite>

When I run this in eclipse, Test1 fails, indicating a null pointer exception for driver in beforeMethod, but Test2 completes just fine. Does anyone know why this happens?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/sdo0FPjTQOsJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Issue running the same test sequentially with Selenium

Krishnan Mahadevan
When TestNG sees @BeforeSuite it gets executed first (probably when TestNG was executing test2) so it instantiates the WebDriver which is then made available to that particular test alone. 

Now TestNG envounters test1 but doesnt execute the @BeforeSuite because it was already executed. So it skips it leaving the driver instance null for test2. 

That explains the error you are seeing. I would suggest that you move your driver instantiation to @BeforeMethod or to @BeforeClass. 

On Friday, October 26, 2012, jrfpi wrote:
I have this class: 

public class NewTest1
{
WebDriver driver;
@BeforeSuite(alwaysRun = true)
public void beforeSuite()
{
driver = new FirefoxDriver();
}
@BeforeMethod(alwaysRun = true)
public void beforeMethod()
{
driver.get("http://www.bing.com");
}
@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult tr)
{
if(tr.getStatus() == ITestResult.FAILURE)
{
driver = new FirefoxDriver();
}
}
@AfterTest(alwaysRun = true)
public void afterTest(ITestContext tc)
{
driver.close();
}
@Test
public void f()
{
driver.findElement(By.id("sb_form_q")).sendKeys("apple");
driver.findElement(By.id("sb_form_go")).click();
}
}

and this is my testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test verbose="2" name="Test1" enabled="true">
<classes>
<class name="Tests.NewTest1">
</class>
</classes>
</test>
<test verbose="2" name="Test2" enabled="true">
<classes>
<class name="Tests.NewTest1">
</class>
</classes>
</test>
</suite>

When I run this in eclipse, Test1 fails, indicating a null pointer exception for driver in beforeMethod, but Test2 completes just fine. Does anyone know why this happens?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/sdo0FPjTQOsJ.
To post to this group, send email to <a href="javascript:_e({}, &#39;cvml&#39;, &#39;testng-users@googlegroups.com&#39;);" target="_blank">testng-users@....
To unsubscribe from this group, send email to <a href="javascript:_e({}, &#39;cvml&#39;, &#39;testng-users%2Bunsubscribe@googlegroups.com&#39;);" target="_blank">testng-users+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.