20221030 web自动化测试实战一

实战目标

  • 企业微信的用户管理

课堂ppt

PPT地址

框架

Selenium

  • Web自动化框架

控制浏览器
在浏览器上进行操作,输入、获取页面内容、点击、js

JUnit5

  • 测试框架 ,管理测试用例

@Before. @Test @Tag
@Suite

管理测试用例集
环境的区分
测试用例执行顺序。@Suite @Order
数据的隔离、数据驱动

qa : test1.yaml
master: dev

Allure

  • 报告

知识汇总

汇总.zip (1.9 MB)

chromedriver

$ chromedriver
Starting ChromeDriver 98.0.4758.80 (7f0488e8ba0d8e019187c6325a16c29d9b7f4989-refs/branch-heads/4758@{#972}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

下载地址

webdrivermanager

官网
github地址

使用方法

       //判断是否有浏览器
        Optional<Path> browserPath = WebDriverManager.chromedriver().getBrowserPath();
        assumeThat(browserPath).isPresent();

        //使用WebDriverManager创建driver打开浏览器  .cache/selenium
        WebDriver webDriver = WebDriverManager.chromedriver().create();

pom文件

<properties>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <maven.compiler.version>3.10.1</maven.compiler.version>
        <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
        <!-- 使用 Java 11 语言特性 ( -source 11 ) 并且还希望编译后的类与 JVM 11 ( -target 11 )兼容,
        可以添加以下两个属性,它们是默认属性插件参数的名称 -->
        <java.version>11</java.version>
        <!-- 对应junit Jupiter的版本号;放在这里就不需要在每个依赖里面写版本号,导致对应版本号会冲突 -->
        <junit.jupiter.version>5.9.1</junit.jupiter.version>
        <!-- log日志 -->
        <slf4j.version>2.0.3</slf4j.version>
        <logback.version>1.4.3</logback.version>
        <!-- allure报告 -->
        <allure.version>2.19.0</allure.version>
        <aspectj.version>1.9.9.1</aspectj.version>
        <allure.maven.version>2.11.2</allure.maven.version>
        <!--  旧版本      <allure.maven.version>2.10.0</allure.maven.version>  -->
        <allure.cmd.download.url>
            https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline
        </allure.cmd.download.url>
        <!-- hamcrest断言 -->
        <hamcrest.version>2.2</hamcrest.version>
        <!-- yaml对应解析 -->
        <jackson.version>2.13.4</jackson.version>
        <!-- selenium安装 -->
        <selenium.verison>4.5.3</selenium.verison>
        <webdriver.manager.version>5.3.0</webdriver.manager.version>
        <assertj.version>3.23.1</assertj.version>
    </properties>
    <!--    物料清单 (BOM)-->
    <dependencyManagement>
        <dependencies>
            <!--当使用 Gradle 或 Maven 引用多个 JUnit 工件时,此物料清单 POM 可用于简化依赖项管理。不再需要在添加依赖时设置版本-->
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit.jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- junit5 -->
        <!-- 创建 Junit5 测试用例的 API-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <!--对应添加的依赖的作用范围-->
            <scope>test</scope>
        </dependency>
        <!-- 兼容 JUnit4 版本的测试用例-->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <!--suite套件依赖 -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- log日志 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>compile</scope>
        </dependency>

        <!--        allure报告-->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>${allure.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <!--        yaml文件解析-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <!-- hamcrest断言 -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- selenium安装 -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.verison}</version>
        </dependency>
        <!-- webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdriver.manager.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.23.1</version>
            <scope>test</scope>
        </dependency>
<dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.version}</version>
                <configuration>
                    <!-- 设置jre版本为 11 -->
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <!-- 设置编码为 UTF-8 -->
                    <encoding>${maven.compiler.encoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <!--                    <argLine>-->
                    <!--                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"-->
                    <!--                    </argLine>-->
                    <includes>
                        <include>**/SubTest</include>
                        <!--                        <include>**/*Test</include>-->
                        <!--                        <include>**/Test*</include>-->
                    </includes>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>io.qameta.allure</groupId>
                <artifactId>allure-maven</artifactId>
                <version>${allure.maven.version}</version>
                <configuration>
                    <reportVersion>${allure.version}</reportVersion>
                    <allureDownloadUrl>${allure.cmd.download.url}/${allure.version}/allure-commandline-${allure.version}.zip</allureDownloadUrl>
                </configuration>
            </plugin>
        </plugins>
    </build>

Cookie

  • 获取cookie
  • 保存为yaml文件

15分钟

 @Test
    void saveCookie() throws InterruptedException, IOException {
        //登录
        driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx");
        //强制等待
        Thread.sleep(5000);
        //获取cookies
        Set<Cookie> cookies = driver.manage().getCookies();
        //写入yaml文件
        ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
        objectMapper.writeValue(new File("cookies.yaml"),cookies);
    }

作业
https://gitee.com/neej/autoApi/blob/master/src/test/java/webauto/study/auto/AutoWebWorkTest.java

package com.ceshiren.basic;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AddDepartmentTest extends BaseTest{
    @Test
    void addDepartment() throws InterruptedException {

        //通讯录
        WebElement menu_contacts = webDriver.findElement(By.id("menu_contacts"));
        menu_contacts.click();
        //等待加号按钮可以被定位到
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".member_colLeft_top_addBtn")));
        webDriver.findElement(By.cssSelector(".member_colLeft_top_addBtn")).click();
        //添加部门
        WebElement js_create_party = webDriver.findElement(By.className("js_create_party"));
        js_create_party.click();
        //等待填写部门的弹框可以被定位到
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".member_tag_dialog_inputDlg")));
        WebElement input = webDriver.findElement(By.cssSelector(".member_tag_dialog_inputDlg .ww_inputText"));
        input.clear();
        input.sendKeys("测试部门4");
        WebElement partyName = webDriver.findElement(By.cssSelector(".js_toggle_party_list"));
        partyName.click();
        //下拉选项选择所属部门
       WebElement jsTree = webDriver.findElement(By.cssSelector(".qui_dropdownMenu .jstree-last"));
        jsTree.click();
        //确认按钮
        WebElement sumbitBtn = webDriver.findElement(By.cssSelector(".ww_dialog_foot .ww_btn_Blue"));
        sumbitBtn.click();
        //等待部门加载出来并被选中
        sleep(5000);
        WebElement element = webDriver.findElement(By.id("party_name"));
        System.out.println(element.getText());
        assertEquals("测试部门4",element.getText());



    }

}