0
点赞
收藏
分享

微信扫一扫

网络性能监控(NPM)工具

目录

一、初试自动化测试

1.自动化测试的分类(简单了解)

2.如何实施自动化单元测试

二、selenium 介绍(重点内容)

1.工作原理

2.Selenium 环境搭建

2.1 Chrome  + java

2.2 常见问题及解决方案

1.元素的定位

1.1 CSS 定位

1.2 CSS选择语法(面试高频)

1.3 XPath 定位

1.5 实现一个自动化需求

2.操作测试对象

2.1 clear 清除对象输入的文本内容

2.2 submit 提交

2.3 getAttribute  获取元素对应属性的值


一、初试自动化测试

自动化测试指软件测试的自动化,在预设状态下运行应用程序或者系统,预设条件包括正常和异常,最后评估运行结果。将人为驱动的测试行为转化为机器执行的过程;也就是说自动化测试相当于将人工测试手段进行转化,让代码去执行

1.自动化测试的分类(简单了解)

f9297cb053894c74829c1ddfdf4de632.png (701×458)

自动化测试包括 UI测试、接口自动化、单元测试。按照这个金字塔模型来进行自动化测试规划,可以产生最佳的自贡话测试产出投入比(ROI),可以用较少的投入获得很好的收益

  • 单元测试:最大的投入应该在单元测试上,单元测试运行的频率也更加高。
  • 接口自动化 :接口测试就是API测试,相对于UI自动化API自动化更加容易实现,执行起来也更稳定。
  • UI自动化:虽然测试金字塔告诉我们尽量多做API层的自动化测试,但是UI层的自动化测试更加贴近用户的需求和软件系统的实际业务。并且有时候我们不得不进行UI层的测试。

UI 自动化测试的好处:

UI层自动化测试框架:

UI自动化测试的适用对象:

2.如何实施自动化单元测试

1️⃣分析:总体把握系统逻辑,分析出系统的核心体系架构

2️⃣设计:设计测试用例,测试用例要足够明确和清晰,覆盖面广而精

3️⃣实现:实现脚本,有两个要求一是断言,二是合理的运用参数化

4️⃣执行:执行脚本远远没有我们想象中那么简单。脚本执行过程中的异常需要我们仔细的去分析原因。

5️⃣总结:测试结果的分析,和测试过程的总结是自动化测试的关键。

6️⃣维护:自动化测试脚本的维护是一个难以解决但又必须要解决的问题。

7️⃣分析:在自动化测试过程中深刻的分析自动化用例的覆盖风险和脚本维护的成本

二、selenium 介绍(重点内容)

是用来做 web 自动化测试框架

1.工作原理

d96eae3ab1fa47c9bc7ac39b35600610.png (914×592)

用Selenium实现自动化,主要需要三个东西: 

Selenium脚本执行时后端实现的流程:

  1. 对于每一条Selenium脚本,一个http请求会被创建并且发送给浏览器的驱动
  2. 浏览器驱动中包含了一个HTTP Server,用来接收这些http请求
  3. HTTP Server接收到请求后根据请求来具体操控对应的浏览器
  4. 浏览器执行具体的测试步骤
  5. 浏览器将步骤执行结果返回给HTTP Server
  6. HTTP Server又将结果返回给Selenium的脚本,如果是错误的http代码我们就会在控制台看到对应的报错信息。

2.Selenium 环境搭建

环境搭建需要 java 版本最低要求为8,浏览器的使用为 Edge 或者 Chrome 浏览器,我们以 Chrome 浏览器来搭建环境

2.1 Chrome  + java

1️⃣下载chorme 浏览器:Google Chrome 网络浏览器

2️⃣查看chrome浏览器版本

84c299034f634a588f87505aa323a4ef.png (856×283)

3️⃣下载chrome浏览器驱动

ChromeDriver - WebDriver for Chrome - Downloads (chromium.org)

fbbd45f60ae44113bfab01ca301d0a39.png (1850×771) 

 b880772094e84b7794e286f88d7cb7b3.png (1084×356)

4️⃣配置环境:在这里配置环境不做解释,如果不会配置和没有配置的小伙伴,可以去看我的博客,有详细的配置环境步骤:(29条消息) Windows下最简单的JDK和IDEA安装教程_idea jdk安装教程_奋斗小温的博客-CSDN博客

5️⃣ 将 .exe 放到 jdk 的 bin 文件中

664ee66db53a495f8ea5b16e7c2c3849.png (1544×529)

6️⃣打开idea,创建 maven 项目:在pom文件中添加依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
</dependencies>

在这里需要注意依赖可能添加不成功:这时候我们需要更换一下使用目录

e7588e8e3fbe44eeb56dd440851d5926.png (1231×662)

7️⃣验证环境是否搭建成功

5a7759bcdaf34fb78a01bb35e8526e39.png (1771×618)

2.2 常见问题及解决方案

1️⃣SessionNotCreatedException

64799c5a7f214bf284ba91209461d9cb.png (1075×126)

解决办法:前提——请先确认您下载的谷歌浏览器不是盗版。

驱动实例化中使用绝对路径:

ChromeDriver driver = new ChromeDriver("E:/browser/Google/Chrome/Application/chrome.exe);❗❗注意:这里放同学自己的Chrome.exe所在路径哦,不要盲目复制粘贴~~

2️⃣The version of ChromeDriver only support xxxxxxxxx

3️⃣The path to the driver executable the path to

9fb1a0741427418e8fc2235a562ec8a3.png (1079×391)

解决办法(二选一即可):

  1. 找到驱动所在路径,复制路径并添加系统环境变量
  2. 将驱动放到jdk路径下

三、webdriver API

Webdriver (Selenium2.0)是一种用于Web应用程序的自动测试工具

1.元素的定位

元素的定位方法有很多:

  • id 定位
find_element_by_id( )
  • name 定位
find_element_by_name( )
  • tag name 定位和 class name 定位
find_element_by_tag_name( )
  • CSS 定位
  • XPath 定位
  • link text 定位:通过元素标签对之间的文本信息来定位元素。
find_element_by_link_text( )
  • Partial link 定位:是对link定位的补充,有些文本链接比较长,这个时候可以取文本链接的一 部分定位,只要这一部分信息可以唯一地标识这个链接
find_element_by_partial_link_text( )

最主要的是 CSS 定位 和 XPath 定位,也是功能最为强大的,可以找到页面几乎所有的元素

1.1 CSS 定位

首先我们在百度框输入“软件测试”,需要自动化输入:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框:做任何操作都需要驱动进行操作
        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));//类选择器
        //输入软件测试
        element.sendKeys("软件测试");
    }

}

a23ff0d4aa6e424a9ab1cd3365a1f864.png (1891×650)

1.2 CSS选择语法(面试高频)

CSS选择语法,我们在学习 CSS 中已经讲解过,在这里不做过多介绍

1️⃣id选择器:#id

2️⃣类选择器:.class名

3️⃣标签选择器:标签名

4️⃣后代选择器:父级选择器/子级选择器

1.3 XPath 定位

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框:做任何操作都需要驱动进行操作
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));//xpath 类选择器
        //输入软件测试
        element.sendKeys("软件测试");
    }

}

2299a28b339d4b378871042dc5c5f82f.png (1854×750)

1.4 XPath选择语法(面试高频) 

语法大家可以参考在线教程,讲解的很详细:w3school 在线教程

2ebcaefb3584447880f9c64e82680b44.png (1062×2567)

1️⃣绝对路径:/html/head/title (不常用)

c15f7aeac1d34a6c86b4365946e687e1.png (1328×305)

2️⃣相对路径:

  • 相对路径+索引(找下标)://form/span[1]/input

例如我们找百度的输入框:

fb64636c07814cc9a11563acb17b9e6f.png (1372×403)

  • 相对路径+属性值(找相关属性)://input[@class="s_ipt"]

例如我们找百度的输入框class的属性:

​ 1f134975c8eb4dc69a9308ae0a14acb5.png (882×268)

  • 相对路径+通配符://*[@*="s_ipt"]

找所有属性下的s_ipt:

​ 68e0ef60029f48afb09690c818747307.png (930×347)

  • 相对路径+文本匹配:

1.5 实现一个自动化需求

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import javax.xml.bind.Element;
import java.util.List;
import static java.lang.Thread.sleep;
public class Main {
    public static void main(String[] args) throws InterruptedException {
        test01();
    }
 
    private static void test01() throws InterruptedException {
        int flg = 0;
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索输入框:做任何操作都需要驱动进行操作
        //WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));//类选择器
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));//xpath 类选择器
        //输入软件测试
        element.sendKeys("软件测试");
        //找到百度按钮点击并校验
        //1.找到百度按钮并 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);//等到3s
        //2.校验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
            //System.out.println(elements.get(i).getText());
            //如果返回的结果有软件测试,证明测试通过,否则测试不通过
            if (elements.get(i).getText().equals("测试")) {
                flg = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if (flg == 0) {
            System.out.println("测试不通过");
        }
    }

}

98bc34d9dbc54171be853a6a9b5a3a57.png (1847×385)

2.操作测试对象

前面我们学习了元素的定位,但是定位只是第一步,定位之后需要对玄素进行操作,是鼠标点击还是键盘输入,或者清除元素的内容,或者提交表单等。这个取决于定位元素需要进行的下一步操作

webdriver 中比较常用的操作对象的方法有下面几个:

  • click 点击对象
  • send_keys 在对象上模拟按键输入
  • clear 清除对象输入的文本内容
  • submit 提交
  • text 用于获取元素的文本信息
  • getAttribute  获取元素对应属性的值

2.1 clear 清除对象输入的文本内容

如何找输入框和点击一下:打开百度网页,F12,利用选择元素找到输入框和百度一下,查看id:

90a1ca34292b44c1a495de9cb89c734c.png (804×242)

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.List;
import static java.lang.Thread.sleep;
public class Main {
    public static void main(String[] args) throws InterruptedException {
        test02();
    }
 
    //clear 清除对象输入的文本内容
    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        //找到百度输入框的:id为kw,并且输入“什么是测试”
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("什么是测试");
        //找到百度一下按钮:“su”,并且点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //清除百度输入框的中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }
}

37f0606dc7154643b75c21f2038f2d03.png (1556×1194)

2.2 submit 提交

如果点击的元素放在 form 标签中,此时使用 submit 实现的效果和 click 是一样的;如果点击的元素放在非 form 标签中,此时使用 submit 回报错


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
import static java.lang.Thread.sleep;
public class Main2 {
    public static void main(String[] args) throws InterruptedException {
        test();
    }
 
    //submit
    //如果点击的元素放在 form 标签中,此时使用 submit 实现的效果和 click 是一样的;
    //如果点击的元素放在非 form 标签中,此时使用 submit 回报错
    //点击百度中的“新闻”超链接,这个超链接没有放在form标签中,则会报错
    private static void test() {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //使用xpath定位,并提交
        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).submit();
 
    }
}

396341019cc84924a3c2fb3c5763dd73.png (1787×383)

2c04bda9b3f54326877bad1f6e81c087.png (759×167)

2.3 getAttribute  获取元素对应属性的值


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
 
public class Main3 {
    public static void main(String[] args) throws InterruptedException {
        test();
    }
 
    //getAttribute 获取元素对应属性的值
    private static void test() {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //获取属性的值
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if (button_value.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }
}

3038e6ba95ae48d8a079e9b1ee337c93.png (1793×381)

 


🌈目前我们已经学习了webdriver API 中元素的定位、操作测试对象,但是不仅仅是这两个方面,还有很多,本文我们简单介绍一下自动化测试,由于今天要学的知识有很多,所以下次测试课我们继续讲解 webdriver API,请继续关注我,下节课是本章自动化测试的一个核心知识点+高频面试题❗❗❗

举报

相关推荐

0 条评论