Home > database >  TestNG @Test method running at feature level, how to run at scenario level
TestNG @Test method running at feature level, how to run at scenario level

Time:12-16

I want to execute @AfterMethod at scenario level(Just after each Scenario) of the feature file. But @AfterMethod executing at feature level(after all scenarios) of the feature file. how can I achieve this. Below is the my code snippet.

  1. Runner Class.

    @SuppressWarnings("unused") @CucumberOptions(features = "Features", glue = { "com.dell.clouddam.stepdefinitions" }, dryRun = false, plugin = { "com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html", "pretty", "json:target/cucumber-reports/Cucumber.json" }, monochrome = true) public class MyTestRunner {

     private TestNGCucumberRunner testNGCucumberRunner;
    
     private DriverFactory driverFactory;    
     private WebDriver driver;   //these are made private because these are specific to this class only.
     private ConfigReader configReader;
     Properties properties;
    
    
     @BeforeSuite(alwaysRun = true)
     public void getProperty() {
         configReader = new ConfigReader();
         properties = configReader.initProp();
         System.out.println("@BeforeClass getpropeety ");
    
     }
    
     @BeforeClass(alwaysRun = true)
     public void setUpClass() throws Exception {
         testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
     }
    
     @BeforeMethod(alwaysRun = true)
     public void launchBrowser() throws FileNotFoundException, IOException, Exception {
         String browserName = properties.getProperty("environment");
         driver = driverFactory.initDriver(browserName); 
         driver.get(properties.getProperty("author_url"));
         ((RemoteWebDriver) driver).getSessionId();
    
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     }
    
    
    
     @DataProvider
     public Object[][] features() {
         return testNGCucumberRunner.provideFeatures();
     }
    
     @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
     public void feature(CucumberFeatureWrapper cucumberFeature) {
         testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
     }
    
     @AfterMethod(description = "Executes After Each Iteration")
     public void afterMethod(ITestResult res) throws Exception {
         driver.quit();
     }
    
     @AfterTest(alwaysRun = true)
     public void afterTest() throws Throwable {
    
     }
    
     @AfterClass(alwaysRun = true)
     public void tearDownClass() throws Throwable {
         testNGCucumberRunner.finish();
     }
    

    }

  2. DriverFactory.java

    public class DriverFactory {

    public static WebDriver driver; HashMap<String, Object> moonConfig = new HashMap<String, Object>();

    private ConfigReader configReader=new ConfigReader();; Properties properties = configReader.initProp();

    public WebDriver initDriver(String environment) throws FileNotFoundException, IOException, Exception { System.out.println("Browser value is - " environment);

     if (environment.equals("chrome")) {
         WebDriverManager.chromedriver().setup();
             driver = new ChromeDriver();
     }
    
     else if (environment.equals("firefox")) {
         WebDriverManager.firefoxdriver().setup();
         driver = new FirefoxDriver();
     }
    
     else if (environment.equals("safari")) {
         driver = new SafariDriver();
     } 
     else {
         System.out.println("Please select the correct browser value");
     }
     getDriver().manage().deleteAllCookies();
     getDriver().manage().window().maximize();
     return getDriver();
    

    }

    public static synchronized WebDriver getDriver() { return driver; } }

  3. Pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sample</groupId>
     <artifactId>SampleDAM</artifactId>
     <version>1.0</version>
     <dependencies>
         <dependency>
             <groupId>io.github.bonigarcia</groupId>
             <artifactId>webdrivermanager</artifactId>
             <version>4.2.2</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
         <dependency>
             <groupId>io.rest-assured</groupId>
             <artifactId>rest-assured</artifactId>
             <version>4.2.0</version>
         </dependency>
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.17</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-scratchpad</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml-schemas</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-excelant</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.xmlbeans</groupId>
             <artifactId>xmlbeans</artifactId>
             <version>2.6.0</version>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-collections4</artifactId>
             <version>4.1</version>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
             <version>3.12.0</version>
         </dependency>
         <dependency>
             <groupId>com.paulhammant</groupId>
             <artifactId>ngwebdriver</artifactId>
             <version>1.1.6</version>
         </dependency>
         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-remote-driver</artifactId>
             <version>2.48.2</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.github.jesg/phantomjsdriver -->
         <dependency>
             <groupId>com.github.jesg</groupId>
             <artifactId>phantomjsdriver</artifactId>
             <version>2.0.0</version>
         </dependency>
         <dependency>
             <groupId>com.saucelabs</groupId>
             <artifactId>sauce_bindings</artifactId>
             <version>1.0.0</version>
         </dependency>
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
             <version>6.14.3</version>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1</version>
         </dependency>
         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-java</artifactId>
             <version>2.53.0</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-jvm</artifactId>
             <version>1.2.5</version>
             <type>pom</type>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1.1</version>
         </dependency>
         <dependency>
             <groupId>org.json</groupId>
             <artifactId>json</artifactId>
             <version>20180130</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-junit</artifactId>
             <version>1.2.5</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-core</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-html</artifactId>
             <version>0.2.3</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-java</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-jvm-deps</artifactId>
             <version>1.0.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>gherkin</artifactId>
             <version>2.12.2</version>
         </dependency>
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest-all</artifactId>
             <version>1.3</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-picocontainer</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-testng</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>com.aventstack</groupId>
             <artifactId>extentreports</artifactId>
             <version>3.1.2</version>
         </dependency>
         <dependency>
             <groupId>org.freemarker</groupId>
             <artifactId>freemarker</artifactId>
             <version>2.3.26-incubating</version>
         </dependency>
         <dependency>
             <groupId>com.vimalselvam</groupId>
             <artifactId>cucumber-extentsreport</artifactId>
             <version>3.0.1</version>
         </dependency>
     </dependencies>
    
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>3.0.0-M5</version>
                 <configuration>
                     <parallel>methods</parallel>
                     <useUnlimitedThreads>true</useUnlimitedThreads>
                     <suiteXmlFiles>
                         <suiteXmlFile>CloudDAM.xml</suiteXmlFile>
                     </suiteXmlFiles>
                 </configuration>
             </plugin>
         </plugins>
     </build>
    

CodePudding user response:

For cucumber, you would need to use Cucumber-jvm's annotations eg. (io.cucumber.java.After) @After, @Before - run before and after each scenario. There is no annotation which supports feature end. There is @AfterAll - which runs after all features all scenarios. If you want more granular control, you need to implement ConcurrentEventListener.

CodePudding user response:

@niharika_neo Yeah, we can achieve this using @Before and @After annotations from cucumber, but i have some dependency in CICD integration. So i converted cucumber junit to cucumber TestNG. Is there any way to achieve using TestNG annotations?

  • Related