【问题标题】:Java with Cucumber scenario orderJava 与 Cucumber 场景顺序
【发布时间】:2018-12-03 10:36:58
【问题描述】:

我正在从事一个自动化项目。

我的第一个场景是登录功能。我在 SaaS 上工作,我需要先登录。

所以我在考虑第一个 login.feature 场景和第二个填写表单的场景。

我有两个文件:

登录功能

fill_form.feature

我用

启动我的测试
mvn clean test -Dcucumber.options="--tags @login,@form"

所以它打开了两个窗口,但执行没有按预期工作:它同时启动了两个场景。

为了让它工作,我只需要制作一个功能文件,但这不是我想要的架构。

欢迎提出建议!

谢谢

【问题讨论】:

    标签: java selenium cucumber gherkin


    【解决方案1】:

    您的方法是正确的(您不希望代码重复),但是您应该重用 step.xml 而不是重用功能文件。您有两种选择:

    1. 背景

    您可以指定在功能文件中的所有场景之前应调用哪个步骤。例如:

    Background:
      given I logged in
    
    Scenario: Fill a form
      then I filled a form
    
    Scenario: Some other scenario
      then "here is some work for other scenario"
    
    1. 从步骤定义中调用步骤

    创建一个调用其他步骤的步骤定义。在您的情况下,这意味着创建一个填写表单步骤,该步骤将在开始时登录。

    【讨论】:

    • 我试过后台解决方案。但是在您的提议中,鉴于我已登录,“我已登录”是场景名称吗?
    • “我已登录”是步骤的名称。在这种情况下,“背景”与“场景”块的工作方式相同。因此,您可以输入背景场景的名称(如果需要),并且您可以像在“场景”块中一样输入步骤(可以是多个)。
    • 好的,成功了,两个场景正在启动。但是,在我的第二种情况下,无法识别 ui 元素..
    【解决方案2】:

    我假设您必须运行您尝试登录的测试,然后填写表格。 为此,您无需创建两个不同的功能文件

    你可以像这样创建你的功能文件

    场景:我以用户身份登录。

    鉴于我使用有效凭据登录。

    何时我应该导航到表单。

    那么我应该填写所有细节。

    所有这些步骤都将链接到步骤定义。

    【讨论】:

    • 不想重复代码,所以隔离了登录场景
    • 在这种情况下,您可以为另一个场景调用该步骤定义。这可以是一个常见的步骤@vieroli
    • 你有例子吗?
    • @vieroli 当然!!
    【解决方案3】:

    功能文件

    特点:我登录并填写所有信息

    场景:我以用户身份登录,在表单中填写有效信息

    鉴于从登录页面启动应用程序

    我使用有效的用户凭据登录

    Step-Definition 类(仅包括您的所有步骤,您可以从中调用另一个类的方法来执行任务)

    public class Some_functionality extend DriverInitializer {
    
    WebDriver webDriver; 
    
    @Given("^Start application from a Login page$")
    public void start_application_from_signin_page() throws Throwable {
    
        webDriver = driverInitilizer();
    }
    
    @When("^I Login with valid user credentials")
    public void login() throws Throwable {
        LoginSteps loginsteps = new LoginSteps();
        loginsteps.signinAsUser(webDriver);
    }
    

    }

    现在您必须创建另一个类来定义您的步骤方法

    public class Loginsteps{
    
    public void signinAsUser(Webdriver webDriver){
     //your code here
    }
    

    }

    你可以在任何你想使用的地方调用“signinAsUser”方法

    LoginSteps loginsteps = new LoginSteps();
    
    loginsteps.signinAsUser(webDriver);
    

    您可以在单独的类中启动驱动程序

    public class DriverInitializer{
    
    
    public WebDriver driverInitilizer(){
    //your code to initialize driver
    SetProperty...
    return webdriver;
    }
    

    【讨论】:

      【解决方案4】:

      在 cucumber 中,所有场景都按按字母顺序运行(所以@form 在@login 之前)。您可以将您的场景重命名为 @001-login、@002form、@003...

      如果你使用 Maven,你可以用 org.apache.maven.plugins:maven-antrun-plugincom.google.code.maven-replacer-plugin:replacer 添加复制/替换

      示例:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.7</version>
          <executions>
              <execution>
                  <id>copy-order-scenarios</id>
                  <phase>compile</phase>
                  <goals>
                      <goal>run</goal>
                  </goals>
                  <configuration>
                      <target name="copy">
                          <copy
                              file="src/test/resources/steps/scenarios/login.feature"
                              tofile="src/test/resources/steps/run/001-login.feature" />
                          <copy
                              file="src/test/resources/steps/scenarios/form.feature"
                              tofile="src/test/resources/steps/run/002-form.feature" />
                      </target>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      <plugin>
          <groupId>com.google.code.maven-replacer-plugin</groupId>
          <artifactId>replacer</artifactId>
          <version>1.5.3</version>
          <executions>
              <execution>
                  <id>replace order</id>
                  <phase>compile</phase>
                  <goals>
                      <goal>replace</goal>
                  </goals>
                  <configuration>
                      <basedir>${basedir}</basedir>
                      <includes>
                          <include>src/test/resources/steps/run/001-*.feature</include>
                      </includes>
                      <token>login</token>
                      <value>001-login</value>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 2021-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        相关资源
        最近更新 更多