【问题标题】:want to execute a single step multiple times想要多次执行一个步骤
【发布时间】:2019-07-16 00:05:12
【问题描述】:

我已经用java在黄瓜中编写了一个功能文件,用于登录操作。 我只想在应用程序启动后多次执行登录操作而不关闭应用程序。

这里是功能文件

Feature: Login Scenario

Scenario Outline: Execute me multiple times
    Given I open the application
    When I enter `"<username>"` and `"<password>"`
    Then I click on Login button
    And I close the application

Examples:
  | username | password  |
  | user1    | password1 |
  | user2    | password2 |
  | user3    | password3 |
  | user4    | password4 |
  | user5    | password5 |

在这种情况下,每次启动和关闭应用程序时。但是我只想启动和关闭一次并执行多个登录操作。

跑步者类:

import cucumber.api.CucumberOptions;
@CucumberOptions(features="feature",glue= {"steps"})
public class TestRunner {}

和步骤定义:

public class TestMe {
WebDriver driver;
String chrome="webdriver.chrome.driver";
String path="./drivers/chromedriver1.exe";

@Given("^I open the application$")
public void i_open_the_application() throws Throwable {
    System.setProperty(chrome, path);
    driver=new ChromeDriver();
driver.get("file:///C:/Users/AZ/Desktop/webpages/loginPage.html");
}
@When("^I enter \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_enter_and(String user, String pass) throws Throwable {
    driver.findElement(By.id("user")).sendKeys(user);
    driver.findElement(By.id("pass")).sendKeys(pass);
}
@Then("^I click on Login button$")
public void i_click_on_Login_button() throws Throwable {
    driver.findElement(By.id("btn")).click();
}

@Then("^I close the application$")
public void i_close_the_application() throws Throwable {
    driver.close();
}

}

网页 HTML 代码:

<html>
<head>
<title>Login</title>
</head>
<body>
<div align="center">
Username<input type="text" id="user"></br></br>
Password<input type="password" id="pass"></br></br>
<input type="button" value="Login" id="btn">
</div>
</body>
</html>

预期结果:仅启动和关闭一次,登录操作多次。

实际结果:多次启动,登录操作并关闭。

【问题讨论】:

    标签: maven cucumber bdd


    【解决方案1】:

    结果:以下方法只会启动和关闭浏览器一次并执行多次登录尝试

    重点领域:您应从场景大纲中排除浏览器启动和关闭代码,因为我们将其保留在注释 @BeforeClass@AfterClass强>

    Scenario Outline: Execute me multiple times
     When I enter "<username>" and "<password>"
     Then I click on Login button
    
    Examples:
    
      | username | password  |
      | user1    | password1 |
      | user2    | password2 |
      | user3    | password3 |
      | user4    | password4 |
      | user5    | password5 |
    

    TestRunner.Java

    @CucumberOptions(features = "feature",
                         glue = {"steps" },
                       plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                                "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   monochrome = true)
    
    public class TestRunner {
    
    WebDriver driver;
        String chrome="webdriver.chrome.driver";
        String path="./drivers/chromedriver1.exe";
    
        @BeforeClass
        void launchBrowser() {
            System.setProperty(chrome, path);
            driver=new ChromeDriver();
            DriverManager.setWebDriver(driver);
            driver.get("file:///C:/Users/AZ/Desktop/webpages/loginPage.html");
        }
    
        @AfterClass
        void closeBrowser() {       
             driver.close();
        }
    }
    

    DriverManager.Java

    public class DriverManager {
    
        public static ThreadLocal<WebDriver> dr = new ThreadLocal<WebDriver>();
    
        public static WebDriver getDriver() {
            return dr.get();
        }
    
        public static void setWebDriver(WebDriver driver) {
            dr.set(driver);
        }
    }
    

    StepDefinition.Java

    class StepDefinition
    {
    
    WebDriver driver;
    
    public StepDefinition() {
            driver = DriverManager.get();
        }
    
    @When("^I enter \"([^\"]*)\" and \"([^\"]*)\"$")
    public void i_enter_and(String user, String pass) throws Throwable {
        driver
        driver.findElement(By.id("user")).sendKeys(user);
        driver.findElement(By.id("pass")).sendKeys(pass);
    }
    @Then("^I click on Login button$")
    public void i_click_on_Login_button() throws Throwable {
        driver.findElement(By.id("btn")).click();
    }
    
    }
    

    【讨论】:

      【解决方案2】:

      您必须使用带有 Data Tables 的场景,在不关闭浏览器的情况下多次迭代同一步骤。

      所以改变你的功能文件如下。

      Feature: Login Scenario
      
      Scenario: Execute me multiple times
          Given I open the application
          And validate the credentials
          | username | password  |
            | user1    | password1 |
            | user2    | password2 |
            | user3    | password3 |
          And I close the application
      

      然后执行下面的stepdef。

      @Given("validate the credentials:")
          public void validate_the_credentials(List<String> animals) {
      }
      

      【讨论】:

        【解决方案3】:
        package steps;
        import java.util.Map;
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.chrome.ChromeDriver;
        import cucumber.api.DataTable;
        import cucumber.api.java.en.Given;
        import cucumber.api.java.en.When;
        public class Test {
        WebDriver driver;
        String gecko="webdriver.chrome.driver";
        String path="./drivers/chromedriver1.exe";
        @Given("^I open the application$")
        public void i_open_the_application() throws Throwable {
            System.setProperty(gecko, path);
            driver=new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("file:///C:/Users/AZ/Desktop/webpages/loginPage.html");
        }
        @When("^I enter username and password$")
        public void i_enter_username_and_password(DataTable table) throws Throwable {
            for (Map<String,String> data : table.asMaps(String.class,String.class)) {
                driver.findElement(By.id("user")).sendKeys(data.get("username"));
                driver.findElement(By.id("pass")).sendKeys(data.get("password"));
                driver.findElement(By.id("btn")).click();
                Thread.sleep(3000);
                driver.navigate().refresh();
                Thread.sleep(3000);
            }
        }
        @When("^I close the application$")
        public void i_close_the_application() throws Throwable {
            driver.close();
        }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-10
          • 2018-12-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多