BeforeSuite/AfterTest and Before/After Method class for multiple Test Classes

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

BeforeSuite/AfterTest and Before/After Method class for multiple Test Classes

jrfpi
I would like to have a class that performs a BeforeSuite and AfterTest method to set up and tear down the environment for the rest of my test classes, and have those test classes share a Before/After Method method, in the same class as the Before and After Suite methods if possible. How would I go about doing this?

As an example, using selenium I have the aforementioned class:

public class SetUpTearDown
{
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();
}
}

Then I have two test classes like so: 

public class NewTest1
{
@Test
public void f()
{
driver.findElement(By.id("sb_form_q")).sendKeys("apple");
driver.findElement(By.id("sb_form_go")).click();
}
}

public class NewTest2
{
@Test
public void g()
{
driver.findElement(By.id("sb_form_q")).sendKeys("microsoft");
driver.findElement(By.id("sb_form_go")).click();
}
}

And the testng.xml looks something like this: 

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
<test verbose="2" name="Test1" enabled="true">
<classes>
<class name="NewTest1">
<methods>
<include name="f"/>
</methods> 
 </class>
</classes>
</test>
<test verbose="2" name="Test2" enabled="true">
<classes>
<class name="NewTest2">
<methods>
<include name="g"/>
</methods> 
 </class>
</classes>
</test>
</suite>

This is a very simple example, my needs would expand on this structure. I haven't included the SetUpTearDown class in the testng.xml because I first tried extending it to NewTest1 and 2, but that would result in NewTest1 failing with a NullPointerException for driver in beforeMethod, but I mainly believe it should be independent of the xml suite, although I am open to any and all suggestions. Thanks in advance.

--
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/-/dr3B49KdANEJ.
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.