在控制台步骤中成功运行后,黄瓜测试步骤未显示在J-Unit中

时间:2019-05-22 13:23:21

标签: java junit cucumber

我目前正在尝试创建我的第一个Cucumber测试。在Java Eclipse中,我创建了一个具有以下内容的“功能文件”:

Feature: Login functionality DemoQA.com

Scenario: Verify if user is able to login to the DemoQA website
    Given A user is on DemoQA.com
    When User clicks MyAccount link
    Then User is taken to Login Page
    When User enters valid username and password
    Then User is able to login 

我还创建了以下testrunner文件:

@RunWith(Cucumber.class)
@CucumberOptions(   
        features = "src/test/Features/",
        glue = {"Tests"}
        )
public class CucumberRunner {
}

我还创建了Stepdefinitions:

公共类LoginStepDefinitions {

@Given("A user is on DemoQA.com")
public void a_user_is_on_DemoQA_com() {

     System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.10.0-win64\\geckodriver.exe");

         WebDriver driver = new FirefoxDriver();
         WebDriverWait wait = new WebDriverWait(driver, 50);

         String url = "https://demoqa.com";

            //Launch the Online Store Website
            driver.get(url);

try {

            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"logo-events\"]/a/img")));   
            driver.findElement(By.xpath("//*[@id=\"logo-events\"]/a/img"));
            System.out.println("User has succesfully opened DemoQA.com");

             } 

catch (Exception e) {
            System.out.println("User was not able to open DemoQA.com");

            }

}

@When("User clicks MyAccount link")
public void user_clicks_MyAccount_link() {
    // Write code here that turns the phrase above into concrete actions
    System.out.println("User clicks on the MyAccount link");
}

@Then("User is taken to Login Page")
public void user_is_taken_to_Login_Page() {
    System.out.println("User is succesfully taken to MyAccount login");
}

@When("User enters valid username and password")
public void user_enters_valid_username_and_password() {
    System.out.println("User enters valid credentials for MyAccount login");
}

@Then("User is able to login")
public void user_is_able_to_login() {
    // Write code here that turns the phrase above into concrete actions
    System.out.println("User is succesfully logged in");
}

}

当我将脚本作为Junit测试运行时,控制台将成功执行测试并显示结果:

User has succesfully opened DemoQA.com
[32m.[0mUser clicks on the MyAccount link
[32m.[0mUser is succesfully taken to MyAccount login
[32m.[0mUser enters valid credentials for MyAccount login
[32m.[0mUser is succesfully logged in
[32m.[0m
1 Scenarios ([32m1 passed[0m)
5 Steps ([32m5 passed[0m)
0m7.706s

但是当打开“ JUnit”选项卡时,会发生两件事:

1)测试步骤似乎未显示:

enter image description here

2)当我双击“功能/场景”步骤时,我收到一条消息:

在所选项目中找不到测试类

在阅读了有关该主题的其他文章之后,我首先想到的是我的功能文件不在正确的文件夹中,但是我现在几乎已将其移至任何地方,而且似乎没有任何作用。

这是我当前在Eclipse中的结构:

enter image description here

有人可以帮我吗?谢谢!

3 个答案:

答案 0 :(得分:3)

@CucumberOptions中的答案'junit =“ --step-notifications”'对我不起作用。但幸运的是,我立即找到了解决方案。

@CucumberOptions
(
    features="path to feature file",

    glue="path to step definition file",

    stepNotifications = true
)

答案 1 :(得分:0)

我自己解决了该问题,方法是在Runner文件的@cucumberoptions中添加以下行:junit =“ --step-notifications”。在运行文件中添加此行之后,我的测试步骤也将出现在Junit结果中。问题可以关闭

答案 2 :(得分:0)

@CucumberOptions中的'junit =“ --step-notifications”'对我也不起作用。

我这样做了

@CucumberOptions(
stepNotifications = true,
strict = true,
features="path to feature file",
glue="path to step definition file")

及其工作。我的测试步骤也出现在Junit结果中。

strict = true-用于压制Waring:

此默认值将更改为--strict,并且将删除--non-strict。 您可以使用--strict或@CucumberOptions(strict = true)来禁止显示此警告