Selenium + Cucumber,行为驱动

前言

Cucumber为BDD测试框架, 在于以直白的语言表达业务场景, 统一产品经理, 开发人员, 测试人员, 运营人员等角色的系统认知
关于Cucumber的基础知识可以参阅官网: https://docs.cucumber.io/guides/
Selenium应该都比较熟悉, 通过webdriver实现UI自动化的测试工具

总结:

  • 虽然Scenario要求每一个Step都直白易懂, 不要引入代码层面的元素, 但考虑到页面元素众多, 不得不考虑参数化
  • 由于上一条的原因, 整体风格更像是关键字驱动
  • 无法使用Cucumber Expression, 只好使用正则表达式
  • hook方法还不太会使用, scenario 和 step 范围比较大, 无法就特定场景, 或者特定stroy专门定制

环境搭建

IntelliJ + gradle

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'info.cukes:cucumber-java8:1.2.4'
    testCompile 'info.cukes:cucumber-junit:1.2.4'
    testCompile 'org.seleniumhq.selenium:selenium-java:3.14.0'
}

工程结构
image.png

Story: youdao.feature

Feature: youdao translation

  Background:
    Given open chrome browser

  Scenario: Chinese to English
    Given go to 'http://fanyi.youdao.com/'
    When type '中国' into '#inputOriginal' field
    And click '#transMachine' button
    Then value from '#transTarget' field should be 'China'
    And value from '#langSelect > span.select-text' field should be '检测到:中文  »  英语'
    And element '.dict__more.clog-js' should exist

    @end
    Scenario: go to detail page
      When click '.dict__more.clog-js' link
      And switch to new tab
      Then value from '#query' field should be '中国'

  @end
  Scenario: English to Chinese
    Given go to 'http://fanyi.youdao.com/'
    When type 'China' into '#inputOriginal' field
    And click '#transMachine' button
    Then value from '#transTarget' field should be '中国'
    And value from '#langSelect > span.select-text' field should be '检测到:英语  »  中文'

Step实现

DriverStep.java

public class DriverStep {
    private static WebDriver driver = null;

    public static WebDriver getDriver() {
        return driver;
    }

    @Given("open (\\w+) browser")
    public void open_browser(String type) {
        if(driver != null) return;

        switch (type) {
            case "firefox":
                driver = new FirefoxDriver();
                break;
            case "chrome":
                System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
                driver = new ChromeDriver();
                break;
            case "ie":
                driver = new InternetExplorerDriver();
                break;
            default:
                throw new PendingException();
        }
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    @After
    public void closeBrowser(Scenario scenario) {
        if (scenario.getSourceTagNames().contains("@end")) {
            driver.quit();
            driver = null;
        }
    }
}

YoudaoStep.java

public class YoudaoStep {
    private WebDriver driver;

    public YoudaoStep() {driver = DriverStep.getDriver();}

    @Given("^go to '(.*)'$")
     public void go_to_url(String url) {
        driver.get(url);
    }

    @When("^type '(.*)' into '(.*)' field$")
    public void type_into_field(String text, String location) throws InterruptedException {
        WebElement el = driver.findElement(By.cssSelector(location));
        el.sendKeys(text);
        Thread.sleep(1000);
    }

    @When("^click '(.*)' (?:button|link)$")
    public void click_button(String location) {
        driver.findElement(By.cssSelector(location)).click();
    }

    @When("^switch to new tab$")
    public void switch_to_new_tab() {
        String currentHandle = driver.getWindowHandle();

        for(String handle: driver.getWindowHandles()) {
            if(handle.equals(currentHandle)) continue;
            driver.switchTo().window(handle);
        }
    }

    @Then("^value from '(.*)' field should be '(.*)'$")
    public void get_from_field(String location, String expected) {
        WebElement el = driver.findElement(By.cssSelector(location));
        String actual = el.getText();
        Assert.assertEquals(expected, actual);
    }

    @Then("^element '(.*)' should exist$")
    public void element_should_exist(String location) {
        WebElement el = new WebDriverWait(driver, 5, 500)
                .until((driver) -> driver.findElement(By.cssSelector(location)));
        Assert.assertNotNull(el);
    }

}

程序入⼝

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/features"}
)
public class App {
}

测试报告

image.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值