3

我希望我关于 SO 的第二个问题编写得很好。我正在尝试在 Java 中使用 Gherkin、TestNG 和 Selenium 自动化测试用例。使用带有 Intellij 的 Maven 项目。

当我在 .feature 文件中启动测试用例时,我能够启动它们,但是当我使用 testng.xml 文件或 Test Runner 类时,它会以某种方式忽略测试用例。

我已经检查了似乎配置正确的项目设置。还检查了我的 pom.xml 中是否有正确的依赖项(我希望有)

我的 test.feature

Feature: Xray Jira

  @TEST_01 @STC
  Scenario: Xray Jira Testing
    Given The user login to the application
    When the credentials are entered
    Then the homepage is viewed

我的测试赛跑者

package Runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;

@CucumberOptions(
        features = "src/test/resources/",
        glue = {"src/test/Steps/"},
        plugin = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json"
        })
public class UITest extends AbstractTestNGCucumberTests {

}

我的步骤定义 - 当我从功能文件启动测试用例时,步骤定义中的以下代码正在工作

package Steps;

import Pages.BasePage;
import Pages.HomePage;
import Pages.LoginPage;
import Helper.ConfigFileReader;
import io.cucumber.java.en.*;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

public class MyStepdefs extends BasePage {
    private WebDriver driver = null;
    private Hooks lHooks;

    public MyStepdefs(Hooks lHooks) {
        this.driver = lHooks.driver;
    }

    @Given("The user login to the application")
    public void the_user_login_to_the_application() {
        LoginPage loginObject = new LoginPage(driver);
        resultValue = loginObject.VerifyUrl();
        Assert.assertTrue(resultValue);
    }

    @When("the credentials are entered")
    public void the_credentials_are_entered() {
        ConfigFileReader config = new ConfigFileReader();
        String userID = config.getUserID();
        String userPassword = config.getUserPassword();
        LoginPage loginObject = new LoginPage(driver);
        loginObject.enterName(userID);
        loginObject.enterPassword(userPassword);
        loginObject.clickLoginButton();
        HomePage lHome = new HomePage(driver);
        resultValue=lHome.verifyMenuIsDisplayed();
        Assert.assertTrue(resultValue);
    }

    @Then("the homepage is viewed")
    public void the_homepage_is_viewed() {

        HomePage homeObject = new HomePage(driver);
        resultValue=homeObject.verifyMenuIsDisplayed();
        Assert.assertTrue(resultValue);

    }

我的 POM.xml

//Below is my POM.xml I usually followed different tutorials online to get the dependencies. I had issues with compatibility of different version of the dependencies. I was able to correct them. Not sure if it is still the problem

<?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>execute_auto</groupId>
    <artifactId>execute_auto</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>4.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.7.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>5.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
        <dependency>    
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.6</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>4.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>4.7.1</version>
        </dependency>


        <!-- Web driver manager dependency -->

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.2</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</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>2.21.0</version>
                <configuration>
                    <suiteXmlFiles>testng.xml</suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>3.15.0</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>POC-language-testing</projectName>
                            <outputDirectory>target/cucumber-reports/advanced-reports</outputDirectory>
                            <cucumberOutput>target/cucumber-reports/CucumberTestReport.json</cucumberOutput>
                            <buildNumber>1</buildNumber>
                            <parallelTesting>false</parallelTesting>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


        </plugins>


    </build>

</project>

我的预期结果应该是测试通过了。但是当通过 testng.xml 或 runner 类启动时,它总是被忽略。如果有人能帮上忙,我会非常高兴

PS:我的最终目标是使用页面对象模型在 Intellij 上使用 Cucumber 和 Java 自动化和启动测试用例。这应该会更新 Jira 中 Xray 上的测试用例。如果有人对此类功能有任何有用的链接,将不胜感激。

4

1 回答 1

1

glue元素应该是包名,而不是目录。因此,如果您的步骤在包装中,Steps那么胶水应该是Steps.

此外,TestNG 会在SkipExceptionCucumber 引发的异常中吞下消息,因此您应该添加summary插件以查看 Cucumber 跳过测试的原因(很可能是由于未正确配置胶水而导致的未定义步骤)。

@CucumberOptions(
        features = "src/test/resources/",
        glue = {"Steps"},
        plugin = {
                "summary"
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json"
        })
public class UITest extends AbstractTestNGCucumberTests {

}

顺便说一句:你不应该在你的 pom.xml 中包含传递依赖。您可以并且应该删除cucumber-core,gherkincucumber-jvm-deps依赖项。

于 2019-08-28T18:22:26.273 回答