您的位置 首页 java

「BDD系列」Cucumber 框架Java集成Selenium进行浏览器自动化测试

用户验收测试中用户会经常使用浏览器来验证新开发的功能,为了自动化的验收用户故事,需要使用 Selenium 进行Web UI自动化测试,有时候需在不同的操作系统上(例如 Mac OS Windows 10 、Linux),对不同的浏览器(例如Chorme、Firefox、 Edge IE )中对新开发功能进行测试。我们这次ß介绍如何在Cucumber中集成Selenium进行Web测试。

1. 一个用户验收场景

我们从最简单的用户验收测试场景开始,把Selenium集中到Cucumber中实现对浏览器的操作。

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
 # language: zh-CN

功能: 博客后台用户登录

  场景大纲: 账号密码登录博客后台
  假如 用户进入后台登录页面
  当 用户需要输入用户名<UserName>和密码<Password>然后点击登录按钮
  那么 用户登录平台并显示首页

    @positive
    例子: 有效用户名和密码
      | UserName | Password | Expectation |
      | mogu2021 | mogu2021 | 首页          |

  

2. 生成测试步骤

编写测试场景对应的测试步骤。与用户验收场景有一一对应的测试步骤,这些测试步骤是自然文字语音描述用户测试场景与 编程语言 编写的自动化测试代码的粘合剂。用Model-View-Controller的设计模式看,测试步骤对应与Controller层,把自然语言描述的验收测试场景,编程语言编写的测试逻辑连接起来。

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  
 package org.bddall.stepdefs;

import io.cucumber.java.zh_cn.假如;
import io.cucumber.java.zh_cn.当;
import io.cucumber.java.zh_cn.那么;

public class LoginStepdefs {
    @假如("用户进入后台登录页面$")
    public void 用户进入后台登录页面() {
        // Write code here that turns the phrase above into concrete actions

    }
    @当("用户需要输入用户名([^\\\"]*)和密码([^\\\"]*)然后点击登录$")
    public void 用户需要输入用户名_和密码_然后点击登录(String userName,String password) {
        // Write code here that turns the phrase above into concrete actions

    }
    @那么("用户登录平台并显示首页$")
    public void 用户登录平台并显示首页() {
        // Write code here that turns the phrase above into concrete actions

    }
}



  

3. 集成Selenium支持 Chrome 浏览器测试

3.1 浏览器驱动下载

Selenium 浏览器自动化测试需要依赖浏览器驱动,为了支持Chrome我们需要先下载Chrome驱动程序。
不同的操作系统(OS)下不同的浏览器需要不同的浏览器驱动程序,Chrome浏览器的不同操作系统(OS)下不同的浏览器驱动下载地址如下:

 1
  
 
  


Chome浏览器点击关于 Google Chrome菜单,查看Chrome 浏览器版本,我的浏览器版本是版本 95.0.4638.69。

可选的分别有Linux、MAC OS、Window等不同平台下Chrome浏览器驱动。选择您的操作系统(OS)和浏览器对应版本的浏驱动。

3.2 创建Selenium驱动

浏览器的驱动位置需要设置到系统的环境变量中,这样Selenium在初始化浏览器驱动的时候才能读取到对应的浏览器驱动。
要使用Selenium进行浏览器自动化测试,需要先在Maven工程的pom.xml文件中引入Selenium的依赖包
pom.xm

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
 <properties>
    <selenium.version>4.0.0</selenium.version> 
</properties>

<dependencies>
   
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
        <scope>test</scope>
    </dependency>
   
</dependencies>



  

初始化 chrome 浏览器驱动代码如下:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  
 package org.bddall.util;

import org. open qa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import static java.lang.System. setProperty ;

public class WebDriverFactory {

     private  static WebDriver driver;

    public static WebDriver driver(){


       
        //Linux、Mac OS 平台下浏览器驱动配置
        setProperty("webdriver.chrome.driver", "webdriver/osx/chrome/64bit/95/chromedriver");

        //Windows平台下浏览器驱动配置
        //setProperty("webdriver.chrome.driver", "webdriver\win10\chrome\32bit\95\chromedriver.exe");

        driver = new ChromeDriver();
        return driver;
    }
}


  

其中

  • setProperty(“webdriver.chrome.driver”, “webdriver/osx/chrome/95/chromedriver”); 是设置Mac、Linux平台下的chrome浏览器驱动的位置。` setProperty(“webdriver.chrome.driver”;
  • “setProperty(“webdriver.chrome.driver”, “webdriver\win10\chrome\95\chromedriver.exe” ); 是Windows平台下Chrome浏览器驱动位置。

4. 实现浏览器自动化测试

4.1 浏览器自动化代码

下面我们使用Selenium实现在Chrome浏览器上登录Web应用功能。基于MVC模型,我们把Selenium操作Chrome浏览器的代码封装在一个Page类中。
需要登录的Web页面如下


Selenium操作Web页面实现登录操作代码如下:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  
 package org.bddall.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static java.lang.Thread.sleep;

public class LoginPage {
    private WebDriver driver;

    public LoginPage(WebDriver driver){
        this.driver = driver;
    }
    public void setUserName(String userName){
        WebElement e = driver.findElement(By.name("username"));
        e.sendKeys(userName);

    }

    public void setPassword(String password){
        WebElement e = driver.findElement(By.name("password"));
        e.sendKeys(password);
    }

    public void login(){
        WebElement e = driver.findElement(By.xpath("//*[@id=\"app\"]/div/form/div[3]/div/button"));
      WebElement ew =  new WebDriverWait(driver, Duration.ofSeconds(10))
                .until(ExpectedConditions.elementToBeClickable(e));
       ew.click();
    }

    public boolean isLoninSuccessful(){
        try {
            sleep(5000);
            WebElement ew =  new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div[5]/div/div[1]/button"))));
            ew.click();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

       return driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/ul/div[2]/span/span/span[1]/a")).isDisplayed();
    }

}

  

4.3 步骤中集成浏览器自动化测试

Cucumber 步骤(Step Definitions)是产品特性测试场景与自动化测试代码粘合的地方,属于Controller层,在这里调用Selenium自动化测试代码,完成对浏览器的自动化测试。

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  
 package org.bddall.stepdefs;

import io.cucumber.java.zh_cn.假如;
import io.cucumber.java.zh_cn.当;
import io.cucumber.java.zh_cn.那么;
import org.bddall.page.LoginPage;
import org.bddall.util.WebDriverFactory;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;

public class LoginStepdefs {
    private WebDriver driver;
    private LoginPage loginPage;
    public LoginStepdefs(){
        this.driver = WebDriverFactory.driver();
        loginPage = new LoginPage(driver);
    }
    @假如("用户进入后台登录页面$")
    public void 用户进入后台登录页面() {
        // Write code here that turns the phrase above into concrete actions
        driver.get("#34;);
    }
    @当("用户需要输入用户名([^\\\"]*)和密码([^\\\"]*)然后点击登录$")
    public void 用户需要输入用户名_和密码_然后点击登录(String userName,String password) {
        // Write code here that turns the phrase above into concrete actions
        //从Feature文件获取用户名并在浏览器中输入用户名
        loginPage.setUserName(userName);
        //从Feature文件获取密码并再浏览器中输入密码
        loginPage.setPassword(password);
        //点击登录按钮登录平台
        loginPage.login();

    }
    @那么("用户登录平台并显示首页$")
    public void 用户登录平台并显示首页() {
        // Write code here that turns the phrase above into concrete actions
        Assert.assertTrue(loginPage.isLoninSuccessful());
        driver.quit();
    }
}

  

5. Demo 一下

最重要事项:

源码地址

Cucumber Start系列文章源码会持续更新,欢迎关注代码库。

  • Github 地址
  • Gitee 地址

文章来源:智云一二三科技

文章标题:「BDD系列」Cucumber 框架Java集成Selenium进行浏览器自动化测试

文章地址:https://www.zhihuclub.com/177879.shtml

关于作者: 智云科技

热门文章

网站地图