美文网首页
Cucumber + Selenium实现UI自动化

Cucumber + Selenium实现UI自动化

作者: halfempty | 来源:发表于2018-12-16 18:06 被阅读0次

    前言

    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

    相关文章

      网友评论

          本文标题:Cucumber + Selenium实现UI自动化

          本文链接:https://www.haomeiwen.com/subject/bqexkqtx.html