如何在黄瓜中运行特定方案

时间:2017-04-21 11:04:01

标签: selenium-webdriver cucumber cucumber-java

如何在多种情况下运行黄瓜中的特定场景?

功能文件

Feature: Test Milacron Smoke scenario

  Scenario: Test login with valid credentials
    Given open firefox and start application
    When I click on Login
    And enter valid "kumar.rakesh@thoughtfocus.com" and valid "Thought@123"
    Then Click on login and User should be able to login successfully

  Scenario: Test shop for cart
    Given Click on shop for carts
    And select plates
    When Click on Add to cart
    Then product should be added in the cart successfully
    And verify the product

Scenario: Test login with valid credentials1
    Given open firefox and start application
    When I click on Login
    And enter valid "kumar.rakesh@thoughtfocus.com" and valid "Thought@123"
    Then Click on login and User should be able to login successfully

  Scenario: Test shop for cart1
    Given Click on shop for carts
    And select plates
    When Click on Add to cart
    Then product should be added in the cart successfully
    And verify the product

测试赛跑者

package runner;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"})

public class TestRunnr {

}

4 个答案:

答案 0 :(得分:2)

在下面的黄瓜中使用标签。

Feature: Test Milacron Smoke scenario

  @Test1
  Scenario: Test login with valid credentials
    Given open firefox and start application
    When I click on Login
    And enter valid "kumar.rakesh@thoughtfocus.com" and valid "Thought@123"
    Then Click on login and User should be able to login successfully

  @Test2
  Scenario: Test shop for cart
    Given Click on shop for carts
    And select plates
    When Click on Add to cart
    Then product should be added in the cart successfully
    And verify the product

  @Test3
  Scenario: Test login with valid credentials1
    Given open firefox and start application
    When I click on Login
    And enter valid "kumar.rakesh@thoughtfocus.com" and valid "Thought@123"
    Then Click on login and User should be able to login successfully

  @Test4
  Scenario: Test shop for cart1
    Given Click on shop for carts
    And select plates
    When Click on Add to cart
    Then product should be added in the cart successfully
    And verify the product

如果您只想运行如下的Test1场景更新运行器文件。

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"},tags={"@Test1"})
public class TestRunner {

}

如果您想执行多个方案,请保留逗号分隔标签,如下所述。

import org.junit.runner.RunWith;

    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;

    @RunWith(Cucumber.class)
    @CucumberOptions(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"},tags={"@Test1,@Test2"})
    public class TestRunner {

    }

答案 1 :(得分:2)

与其他答案建议一样,使用标签。如果您使用maven,则无需更改跑步者文件 - 只需将其添加到您的maven通话中

-Dcucumber.options="--tags @Test1"

我喜欢这种方法的方法是,我不会冒险在转轮文件中提交标签。

此外,这是一个在maven中运行多个标签的示例。

答案 2 :(得分:1)

您可以使用标签在功能中使用选择性功能文件或选择性方案。请尝试使用此解决方案。

让我们考虑您有多个功能文件,并且您只需要运行选择性功能。然后使用@tag name命名每个要素文件。

例如:在此文件夹下,如果您有多个功能 - " src / main / resources / publish"

第一个功能文件名:

Login.feature

//文件内部以功能标签名称

开头
@Login
Feature: To Login to Email

//Then feature name followed with scenario tag name
@User1

#Scenario1:
Scenario Outline: Navigate and logon to gmail application

Given User launches gmail application
When User updates emailID <emailID>
And User updates pwd <pwd>    
Then User clicks on Login Button


Examples: 
  | emailID    | pwd |
  | a@gmail.com| 123 | 


@User2

#Scenario2:
Scenario Outline: Navigate and logon to facebook application

//Write the code for scenario 2 as similar to above  

第二个功能文件名:

CreateEmail.feature

@Createmail
Feature: Create email

Scenario: Blah blah blah...

//Write all Given when And Then

第3个特征文件名:

SendEmail.feature

@Sendemail
Feature: Send email

Scenario: Blah blah blah...

//Write all Given when And Then

所以从上面的测试文件中。让我们考虑你想单独测试第一和第三个功能,然后你可以使用如下代码:

例如:

//这是运行特定的功能文件,即1和3.同样,如果在同一个功能文件中有n个场景,也可以使用场景标记。

@CucumberOptions(features= "src/main/resources/publish", tags="@Login, @Sendemail",  format = {"pretty"} )

//这是在要素文件中运行特定方案。如果您有多个场景,则可以编写指定场景标记,后跟逗号。

@CucumberOptions(features= "src/main/resources/publish/Login.feature", tags="@User2",  format = {"pretty"} )

答案 3 :(得分:0)

您需要使用标记来过滤场景。将标记放在要素文件上,并将其添加到跑步者的黄瓜选项中。

@RunScenarioExample
Scenario: Test login with valid credential

@Cucumber.Options(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"}, tags={"@RunScenarioExample"})