Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97792

Can I have @BeforeTest and @AfterTest run for only a group of Tests?

$
0
0

I am building a test framework for a project with multiple modules. Currently, I have two tests in my testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <listeners>
    <listener class-name = "listener-class" />
  </listeners>
  <test thread-count="5" name="frontEnd">
  <parameter name="URL" value="front-end-url" />
    <classes>
      <class name="frontendTestRunner"/>
    </classes>
  </test>
  <test thread-count="5" name="Backend">
  <parameter name="URL" value="back-end-url" /> 
    <classes>
      <class name="backendtestrunner"/>
    </classes>
  </test>  <!-- Test -->
</suite> <!-- Suite -->

For both of these tests, the before and after conditions are different.

Is there is way I can have @BeforeTest and @AfterTest methods run for only one test say "frontEnd" and different @BeforeTest and @AfterTest methods for the second one - "Backend".

Currently I am using two different Cucumber testrunners to accomplish this. But I want to do it using only one testrunner.

Here are my two TestRunners:

@CucumberOptions(
            features = "src/test/java/com/frontEnd",
            glue = "com/stepDefinitions/frontEnd",
            tags = {"~@ignore"}
        )
public class FrontEndTestRunner extends AbstractTestNGCucumberTests {

    @Parameters( {"URL"})
    @BeforeTest
    public static void setup(String url) throws IOException {
        TestConfiguration.initialise(true);
        Pages.initialise();
        TestConfiguration.getHomePageUrl(url);
    }

    /**
     * This class runs after the completion of the final test. It populates the reports and closes the browser driver. 
     */
    @AfterTest
    public static void after() {
        Browser.close();        
    }

}

And

@CucumberOptions(
        features = "src/test/java/com/backEnd",
        glue = "com/stepDefinitions/backEnd"
    )
public class BackEndTestRunner extends AbstractTestNGCucumberTests {

    @BeforeTest
    public static void setup() throws IOException {
        TestConfiguration.initialise(true);
        Pages.initialise();
    }

    /**
     * This class runs after the completion of the final test. It logs the user out and closes the browser driver. 
     */
    @AfterTest
    public static void after() {
        Browser.close();        
    }
}

Is there a way that I can only one TestRunner and somehow still be able to run correct Begin and After conditions for the two set of features?

Basically I need to be able to group two sets of Features and call two different @BeforeTest and @AfterTest methods depending on the which group the Feature belongs to.


Viewing all articles
Browse latest Browse all 97792

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>