Java with Cucumber场景顺序

时间:2018-12-03 10:36:59

标签: java selenium cucumber gherkin

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

我的第一种情况是登录功能。我正在使用SaaS,并且需要先登录。

所以我在考虑第一个login.feature方案和第二个方案来填写表单。

我有两个文件:

  

login.feature

     

fill_form.feature

我使用

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

因此它打开了两个窗口,但是执行未按预期进行:它同时启动两个方案。

要使其正常工作,我只需要制作一个功能文件,但这不是我想要的体系结构。

欢迎提出建议!

谢谢

4 个答案:

答案 0 :(得分:1)

您的方法是正确的(您不希望代码重复),但是应该重新使用步骤,而不是重新使用功能文件。您在这里有两个选择:

  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. 从步骤定义中调用步骤

创建一个调用其他定义的步骤定义。对于您而言,这意味着创建一个填充表单步骤,该步骤将在开始时登录。

答案 1 :(得分:0)

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

您可以这样创建特征文件

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

给出我使用有效的凭据登录。

何时,我应该导航至表单。

然后,我应该填写所有详细信息。

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

答案 2 :(得分:0)

功能文件

功能:我登录并填写所有信息

方案:我以用户身份登录,以在表格中填写有效信息

给予从登录页面启动应用程序

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

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);

您可以在单独的类中启动Driver

public class DriverInitializer{


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

答案 3 :(得分:0)

在黄瓜中,所有情况都按字母顺序运行(因此@form在@login之前)。您可以将您的方案重命名为@ 001登录,@ 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>