简体   繁体   English

带有 python 的 BDD 框架

[英]BDD framework with python

I need to write a test using cucumber for a course.我需要使用 cucumber 为课程编写测试。

Scenario:设想:

  1. Login,登录,
  2. Select first item link, Select 第一项链接,
  3. Add item to the shopping cart,将商品添加到购物车,
  4. Proceed shopping cart page,继续购物车页面,
  5. Check the item on that list is correct,检查该清单上的项目是否正确,
  6. Proceed to checkout,进行结算,
  7. Complete and Logout.完成并注销。

The point I don't understand is, do I need to open a feature file for each step and do I need to close and reopen the browser for each step?我不明白的一点是,我是否需要为每个步骤打开一个功能文件,是否需要为每个步骤关闭并重新打开浏览器? How should I do this?我该怎么做? What kind of path should I follow?我应该走什么样的路?

(Note that I am a beginner and my English skills are limited, so I need a simple explanation.) (注意,我是初学者,英语能力有限,需要简单解释一下。)

Yes, you should make a new file for each feature.是的,您应该为每个功能创建一个新文件。 For example, login.gherkin, upload.gherkin, logout.gherkin.例如,login.gherkin、upload.gherkin、logout.gherkin。

PS. PS。 Sorry, I didn't realize you said Python, but it's the same idea.对不起,我没有意识到你说的是 Python,但这是同一个想法。

Each file should have a layout like this:每个文件应该有这样的布局:

# 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

Then make the corresponding step file like this:然后制作相应的步骤文件如下:

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

If you're new to browser automation, I recommend learning the "Page Object Model Methodology".如果您不熟悉浏览器自动化,我建议您学习“Page Object Model Methodology”。 Also, I stopped using this annoying framework, it's supposed to make things easier but to me it just adds extra work.另外,我停止使用这个烦人的框架,它应该让事情变得更容易,但对我来说它只是增加了额外的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM