【问题标题】:BDD framework with python使用 python 的 BDD 框架
【发布时间】:2021-01-14 01:21:44
【问题描述】:

我需要为课程编写一个使用黄瓜的测试。

场景:

  1. 登录,
  2. 选择第一个项目链接,
  3. 将商品添加到购物车,
  4. 继续购物车页面,
  5. 检查该列表中的项目是否正确,
  6. 继续结帐,
  7. 完成并退出。

我不明白的一点是,我是否需要为每个步骤打开一个功能文件,是否需要为每个步骤关闭并重新打开浏览器?我该怎么做?我应该走什么样的路?

(注意我是初学者,英语水平有限,需要简单解释一下。)

【问题讨论】:

    标签: python testing cucumber bdd gherkin


    【解决方案1】:

    是的,您应该为每个功能创建一个新文件。例如,login.gherkin、upload.gherkin、logout.gherkin。

    附言。抱歉,我没有意识到你说的是 Python,但它是同一个想法。

    每个文件都应该有这样的布局:

    # Created by Mick Jagger at 1/13/2021
      @login
    Feature: Login
    
      Scenario: Login to Website Homepage
        When Launch website
        Then Enter username and password
        And Log in
    

    然后制作相应的step文件如下:

    package glue;
    
    import cucumber.api.java.en.*;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    
    import java.util.List;
    
    public class LoginSteps extends Driver {
        public String username = "username";
        public String password = "password";
    
        @When("Launch Website")
        public void launch_website() throws Throwable {
            driver.get("https://www.website.com/");
            Thread.sleep(2000);
        }
    
        @Then("^Enter username and password$")
        public void enter_credentials() throws Throwable {
            driver.findElement(By.xpath("//input[@aria-label='Phone number, username, or email']")).sendKeys(username);
            driver.findElement(By.xpath("//input[@aria-label='Password']")).sendKeys(password);
        }
    
        @And("^Log in$")
        public void log_in() throws Throwable {
            Thread.sleep(1000);
            List<WebElement> buttons = driver.findElements(By.tagName("button"));
            buttons.get(1).click();
            Thread.sleep(2000);
            driver.findElement(By.tagName("button")).click();
            Thread.sleep(1000);
            buttons = driver.findElements(By.xpath("//button[@tabindex='0']"));
            buttons.get(1).click();
        }
    }
    

    如果您不熟悉浏览器自动化,我建议您学习“页面对象模型方法”。另外,我停止使用这个烦人的框架,它应该让事情变得更容易,但对我来说它只是增加了额外的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多