0
votes

I am new to Selenium,Cucumber Tool and am learning to build a BDD Framework using Selenium,cucumber,maven and TestNG.

I have basically Three Java files- Test-Runner(src/test/java) Step-Definition(src/test/java) TestBase(src/main/java)

I have before and after hooks defined inside TestBase Class My Test-Runner class has a Plugin defined for Extent-report and also it has @AfterClass annotation which is loading the extent-config.xml-

Now when am running the Feature files, it doesn't execute the @AfterClass annotations, hence skips generating extent Report. But if I run directly Test-runner file using TestNG, it skips executing Hooks defined in testBase Class-

Code of Test-Runner-

@CucumberOptions(
        features={"src/test/resources/Features/login"},
         glue={"stepDefinition","src/main/java/Utils/TestBase.java"},
         monochrome=true,
         plugin={"pretty","html:target/Reports","com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"}
         )
public class Login_Runner  extends AbstractTestNGCucumberTests{

    @AfterClass
    public static void writeExtentReport() {
        System.out.println("I am in After Class");
        Reporter.loadXMLConfig(new File("src/test/resources/extent-config.xml"));
}

Can anyone help to solve this issue???

1
I believe running the Feature will naturally not run @Afterclass because it does nothing with Test-Runner. I'm not entirely sure but: you might try to put Test-Base under src/test/java. (Is there any reason not to? )Chai
Maybe it would be worth to provide a bit more details about your classes and project configuration. Otherwise it's hard to guess why it's not working. At least based on the only snippet you provided, the method annotated with AfterClass should be executed.SubOptimal
Can u add a screenshot of the expanded folder structure?Grasshopper
I'm not sure TestNG @AfterClass is supported. Could you try an After Hook instead? You can find more on After Hooks hereMarit
Here is Folder Structure- src/main/java has two classesuser2868792

1 Answers

1
votes

Assume the following structure

src/test/java/stepdef/LoginSteps.java
src/test/java/runner/Login_Runner.java
src/test/resources/features/login.feature
pom.xml

LoginSteps.java

implements all steps for `login.feature`

Login_Runner.java

package runner;
import org.testng.annotations.AfterClass;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(features = "src/test/resources/features", glue = "stepdef")
public class Login_Runner extends AbstractTestNGCucumberTests {
    @AfterClass
    public static void theAfterClassMethod() {
        System.out.println("execute @AfterClass annotated method");
    }
}

login.feature

Feature: test
    Scenario: login
        Given open browser
        And start app
        When enter details
        Then login happens

pom.xml*

<?xml version="1.0" encoding="UTF-8"?>
<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.suboptimal</groupId>
    <artifactId>cuke-testng2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>3.0.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <surefire.version>2.22.0</surefire.version>
    </properties>
</project>

running the test

mvn clean test -Dtest=Login_Runner

produces the output

...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running runner.Login_Runner
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@5ae63ade
execute @Before annotated method
execute @After annotated method
execute @AfterClass annotated method

1 Scenarios (1 passed)
4 Steps (4 passed)
0m0.023s

when you rename the class Login_Runner to e.g. LoginRunnerTest you can run the test with mvn clean test.

Maybe the runner class name is your original problem and not the missed execution of the @AfterTest method.