自动化测试-Selenium

这篇具有很好参考价值的文章主要介绍了自动化测试-Selenium。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

什么是自动化测试

Selenium介绍

Selenium是什么

Selenium特点

Selenium工作原理

Selenium+Java环境搭建

1.下载chrome浏览器,查看版本号.

2.下载chrome浏览器驱动

3.配置环境变量

4.创建java项目,在pom文件中添加依赖

5. 验证环境是否搭建成功

Selenium常用API的使用

定位元素findElement

css定位

XPath定位

 css和XPath哪个更好

操作测试对象

等待

浏览器的操作

键盘事件

鼠标事件

一组元素的获取

下拉框处理

alert弹窗

文件的上传

关闭浏览器

切换窗口

截图


什么是自动化测试

自动化测试指软件测试的自动化,在预设状态下运行应用程序或者系统,预设条件包括正常和异常,最后评估运行结果.将人为驱动的测试行为转化为机器执行的过程.

自动化测试就相当于将人工测试手段进行转换,让代码去执行.

自动化分类

单元测试,接口测试,UI自动化测试.


Selenium介绍

Selenium是什么

是针对web界面进行自动化测试的框架.

Selenium特点

支持各种浏览器,支持各种平台(linux,mac,windows),支持各种语言(phython,java,C#,js...),有丰富的API.

Selenium工作原理

自动化测试-Selenium,selenium,测试工具

自动化脚本代码:通过idea编写的代码.

webdriver浏览器驱动:需要手动下载.

浏览器:chrome浏览器等等..

webdriver 的工作原理:
启动浏览器后, selenium-webdriver 会将目标浏览器绑定到特定的端口,启动后的浏览器则作为
webdriver remote server
客户端 ( 也就是测试脚本 ) ,借助 ComandExecutor 发送 HTTP 请求给 sever 端(通信协议: The
WebDriver Wire Protocol ,在 HTTP request body 中,会以 WebDriver Wire 协议规定的 JSON
式的字符串来告诉 Selenium 我们希望浏览器接下来做什么事情)。
Sever 端需要依赖原生的浏览器组件,转化 Web Service 的命令为浏览器 native 的调用来完成操
作。

Selenium+Java环境搭建

1.下载chrome浏览器,查看版本号.

自动化测试-Selenium,selenium,测试工具

2.下载chrome浏览器驱动

https://chromedriver.chromium.org/downloads

 自动化测试-Selenium,selenium,测试工具

自动化测试-Selenium,selenium,测试工具

3.配置环境变量

解压下载好的驱动压缩包,将下载好的chromedriver.exe放到java系统环境变量bin目录下.

自动化测试-Selenium,selenium,测试工具

4.创建java项目,在pom文件中添加依赖

在maven仓库中搜索Selenium-Java对应的依赖 

<!-- 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>

5. 验证环境是否搭建成功

编译运行以下代码

import org.openqa.selenium.WebDriver;
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");
    }
}

如果打开了浏览器,则说明安装成功了.

自动化测试-Selenium,selenium,测试工具


Selenium常用API的使用

定位元素findElement

css定位

id选择器:#id

自动化测试-Selenium,selenium,测试工具

类选择器:.id

自动化测试-Selenium,selenium,测试工具

标签选择器:标签名

自动化测试-Selenium,selenium,测试工具

后代选择器:父级选择器 子级选择器

自动化测试-Selenium,selenium,测试工具

XPath定位

绝对路径:/html开头的,比如/html/head/title(不常用)

自动化测试-Selenium,selenium,测试工具

相对路径

相对路径+索引://form/span[1]/input

自动化测试-Selenium,selenium,测试工具

相对路径+属性值://input[@class="s_ipt"]

自动化测试-Selenium,selenium,测试工具

 自动化测试-Selenium,selenium,测试工具

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

自动化测试-Selenium,selenium,测试工具

相对路径+文本匹配://a[text()="新闻"]

自动化测试-Selenium,selenium,测试工具


完整的自动化测试代码还要有预期结果和对实际结果的校验

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 {
        test01();
    }

    private static void test01() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        //元素的定位,找到百度搜索输入框
        //类选择器css
        //WebElement element =  webDriver.findElement(By.cssSelector(".s_ipt"));
        //xpath定位元素
        WebElement element =  webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入软件测试
        element.sendKeys("软件测试");
        //找到百度一下按钮
        //点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //校验
        //找到搜索结果
        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("软件测试")){
                System.out.println("测试不通过");
            }else {
                System.out.println("测试通过");
            }
        }
    }
}

 css和XPath哪个更好

css选择器定位元素效率更高,所以推荐使用css选择器.


操作测试对象

定位只是第一步,定位之后需要对这个元素进行操作。是鼠标点击还是键盘输入,或者清除元素的内容,或者提交表单等。这个取决于定位元素需要进行的下一步操作。
webdriver 中比较常用的操作对象的方法有下面几个:
  • click 点击对象
  • sendkeys 在对象上模拟按键输入
  • clear 清除对象输入的文本内容
  • submit 提交
  • text 用于获取元素的文本信息
  • getAttribute 获取元素的属性信息

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


等待

sleep强制等待

智能等待

智能等待包括隐式等待和显示等待

隐式等待:如果等待时间是3天,强制等待会一直等待,等待的时间是3天.隐式等待,最长等待时间是3天,如果在3天时间内获取到了页面上的元素,此时执行下面的代码,如果等待3天的时间还是没有获取到页面上的元素,此时就会报错.

隐式地等待并非一个固定的等待时间,当脚本执行到某个元素定位时,如果元素可以定位,则继续执行;如果元素定位不到,则它以轮询的方式不断的判断元素是否被定位到。直到超出设置的时长

自动化测试-Selenium,selenium,测试工具

显示等待

自动化测试-Selenium,selenium,测试工具

区别:

隐式等待等待的是所有的元素,显示等待等待的是一定的条件,条件是程序员自己设定的.

但是它们都属于智能等待.


浏览器的操作

浏览器的前进,浏览器的后退,浏览器滚动条的操作

private static void test04() throws InterruptedException {
    WebDriver webDriver = new ChromeDriver();
    //打开百度首页
    webDriver.get("https://www.baidu.com");
    // 搜索java
    webDriver.findElement(By.cssSelector("#kw")).sendKeys("java");
    webDriver.findElement(By.cssSelector("#su")).click();
    //强制等待3s
    sleep(3000);
    //浏览器后退
    webDriver.navigate().back();
    sleep(3000);
    //浏览器刷新
    webDriver.navigate().refresh();
    //浏览器前进
    sleep(3000);
    webDriver.navigate().forward();
    //滚动条操作
    sleep(3000);
    ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
    //浏览器全屏
    webDriver.manage().window().fullscreen();
    //设置浏览器的宽高
    sleep(3000);
    webDriver.manage().window().setSize(new Dimension(600,1000));
}

键盘事件

private static void test05() throws InterruptedException {
    WebDriver webDriver = new ChromeDriver();
    //打开百度首页
    webDriver.get("https://www.baidu.com");
    // 搜索java
    webDriver.findElement(By.cssSelector("#kw")).sendKeys("java");
    webDriver.findElement(By.cssSelector("#su")).click();
    //ctrl+A
    sleep(3000);
    webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
    //ctrl+x
    sleep(3000);
    webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
    //ctrl+v
    sleep(3000);
    webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");
}
send_keys(Keys.CONTROL,'a') # 全选( Ctrl+A
send_keys(Keys.CONTROL,'c') # 复制( Ctrl+C
send_keys(Keys.CONTROL,'x') # 剪贴( Ctrl+X
send_keys(Keys.CONTROL,'v') # 粘贴( Ctrl+V

鼠标事件

private static void test06() throws InterruptedException {
    WebDriver webDriver = new ChromeDriver();
    //打开百度首页
    webDriver.get("https://www.baidu.com");
    // 搜索java
    webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
    webDriver.findElement(By.cssSelector("#su")).click();
    sleep(3000);
    //找到图片按钮
    WebElement webElement =webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
    //鼠标右击
    Actions actions = new Actions(webDriver);
    sleep(3000);
    actions.moveToElement(webElement).contextClick().perform();
}
context_click() 右击
double_click() 双击
drag_and_drop() 拖动
move_to_element() 移动

一组元素的获取

获取复选框checkbox.

自动化测试-Selenium,selenium,测试工具

 

 
   

private static void page01() {

WebDriver webDriver = new ChromeDriver();

webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test01.html?_ijt=4brheilpuaaaoa465u843m3sth&_ij_reload=RELOAD_ON_SAVE");

webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);

List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));

for(int i = 0; i < webElements.size(); i++) {

// 如果每个元素type值等于checkbox进行点击

// getAttribute获取页面上的元素属性值,里面的type是当前元素属性

if(webElements.get(i).getAttribute("type").equals("checkbox")){

webElements.get(i).click();

} else {

// 否则什么也不操作

;

}文章来源地址https://www.toymoban.com/news/detail-589774.html

}

}


下拉框处理

下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框对下拉框进行操作后,再定位到下拉框内里的选项。
private static void page03() {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test03.html?_ijt=qje1h6c6groepeefv0ae9bnla2&_ij_reload=RELOAD_ON_SAVE");
WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
Select select = new Select(webElement);
// select.selectByIndex(3);//通过下标定位,从0开始
//通过源代码属性里的value值定位
select.selectByValue("12.51");

}


alert弹窗

自动化测试-Selenium,selenium,测试工具

自动化测试-Selenium,selenium,测试工具 

点击弹窗按钮,弹出一个弹框输入姓名,点击确认.

 
 

private static void page04() throws InterruptedException {

WebDriver webDriver = new ChromeDriver();

webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test04.html?_ijt=u8fai3bvc2dvsjigccqcapieg1&_ij_reload=RELOAD_ON_SAVE");

webDriver.findElement(By.cssSelector("button")).click();

sleep(3000);

// alert弹窗取消

webDriver.switchTo().alert().dismiss();

sleep(3000);

// 点击按钮

webDriver.findElement(By.cssSelector("button")).click();

// 在alert弹窗中输入字节

webDriver.switchTo().alert().sendKeys("字节");

// alert弹窗确认

sleep(3000);

webDriver.switchTo().alert().accept();


文件的上传

上传过程一般要打开一个本地窗口,从窗口选择本地文件添加。所以,一般会卡在如何操作本地窗口添加上传文件。其实,在selenium webdriver 没我们想的那么复杂;只要定位上传按钮,通过 send_keys 添加本地文件路径就可以了。绝对路径和相对路径都可以,关键是上传的文件存在。
private static void page05() {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test05.html?_ijt=7asvni2vctahrdl90k8v4lvq5&_ij_reload=RELOAD_ON_SAVE");
webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\javaword");
}

关闭浏览器

关闭浏览器的方式有两种.

private static void test10() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
//找到新闻按钮
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

sleep(4000);
// webDriver.quit();
webDriver.close();
}

quit和close的区别

  • quit是关闭了整个浏览器,close关闭只是当前的页面(get请求的哪个页面,就关闭哪个)
  • quit会清空缓存,close不会清空缓存

切换窗口

我们打开百度页面,点击新闻,页面会跳转到新闻页面,在新闻页面里的输入框里输入新闻联播,点击百度一下.

自动化测试-Selenium,selenium,测试工具

常规的这种方式会报错,因为页面发生了切换,元素的定位只是在最开始的页面里,没有转换到新的页面里,所以发生了报错.

没有找到元素,是因为元素默认是在get打开的页面里寻找元素的.

 
  

private static void test11() throws InterruptedException {

WebDriver webDriver = new ChromeDriver();

webDriver.get("https://www.baidu.com/");

webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

sleep(3000);

// 通过getWindowHandles获取所有的窗口句柄

// 通过getWindowHandle获取的get打开的页面窗口句柄

System.out.println(webDriver.getWindowHandle());

Set<String> handles = webDriver.getWindowHandles();

String target_handle = "";

for(String handle:handles) {

target_handle = handle;

}

webDriver.switchTo().window(target_handle);

sleep(3000);

webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");

webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

}

 


截图

在maven仓库里搜索common-io,添加对应的依赖.

自动化测试-Selenium,selenium,测试工具

 

 
  

private static void test12() throws InterruptedException, IOException {

WebDriver webDriver = new ChromeDriver();

webDriver.get("https://www.baidu.com/");

webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");

webDriver.findElement(By.cssSelector("#su")).click();

sleep(3000);

File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(file, new File("D://20230521jietu.png"));

}


到了这里,关于自动化测试-Selenium的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 自动化测试工具-Selenium:Selenium的核心三大组件详解

    目录 1. WebDriver 1.1 WebDriver的通信方式 1.2 WebDriver的功能 1.3 W3C推荐标准 2. Grid 3. IDE Selenium 是支持 web 浏览器自动化的一系列工具和库的综合项目。官方对Selenium认可的三大组件或API分别是: WebDriver、Selenium IDE、Grid。 其中,WebDriver又被称为Selenium的核心。 下面本篇文章将深度介

    2024年02月03日
    浏览(41)
  • 自动化测试工具Selenium的语法续.

    OK,那么上篇博客我们介绍了如何搭建基于Java+selenium的环境,并且使用selenium的一些语法给大家演示了如何进行自动化测试的案例,那么本篇博客我们来继续学习selenium的一些其他的比较重要的语法,感谢关注,期待三连~ 目录 一、定位一组元素 二、下拉框处理 三、上传文件

    2024年02月10日
    浏览(51)
  • 自动化测试工具selenium的安装方法

    一、什么是selenium Selenium 是一套 Web网站 的程序自动化操作 解决方案。 通过它,我们可以写出自动化程序,像人一样在浏览器里操作web界面。 比如点击界面按钮,在文本框中输入文字 等操作。 Selenium 通过使用  WebDriver  支持市场上所有主流浏览器的自动化。 Webdriver 是一个

    2024年02月09日
    浏览(43)
  • 如何使用Python自动化测试工具Selenium进行网页自动化?

    Selenium 是一个流行的Web自动化测试框架, 它支持多种编程语言和浏览器,并提供了丰富的API和工具来模拟用户在浏览器中的行为 。 Selenium可以通过代码驱动浏览器自动化测试流程,包括页面导航、元素查找、数据填充、点击操作等。 与PyAutoGUI和AutoIt相比, Selenium更适合于处

    2023年04月09日
    浏览(110)
  • Web测试自动化工具Selenium的使用

    Selenium是一个Web应用测试的自动化工具,它通过模拟点击实现对Web应用的功能测试。测试时,除了Selenium,还需要对应的浏览器驱动,如在Chrome实现自动点击,则需要chromedriver。 Selenium支持多种语言和多种浏览器,本文仅记录python+chrome的使用。 1. 安装python 略 2. 安装Selenium 注

    2024年01月16日
    浏览(77)
  • Selenium教程:自动化浏览器测试工具

    Selenium是一款用于自动化浏览器测试的工具,它提供了一系列的API和功能,使得开发人员可以编写脚本来模拟用户在浏览器中的行为。无论是在Web应用程序的功能测试、性能测试还是数据抓取方面,Selenium都是一个强大且广泛使用的工具。 在开始使用Selenium之前,您需要进行安

    2024年02月07日
    浏览(61)
  • 自动化测试工具之Selenium IDE录制教程

            下载传送带:Selenium IDE · Open source record and playback test automation for the web         这里Darren洋以firefox火狐浏览器为例,将以上下载url直接在firefox浏览器中打开,点击对应下载按钮后,就会进入添加页面。         这里直接点击添加到Firefox浏览器的按钮即可,谷歌浏

    2024年02月08日
    浏览(39)
  • Python自动化测试工具selenium使用指南

    概述 selenium 是网页应用中最流行的自动化测试工具,可以用来做自动化测试或者浏览器爬虫等。官网地址为:相对于另外一款web自动化测试工具QTP来说有如下优点: 免费开源轻量级,不同语言只需要一个体积很小的依赖包 支持多种系统,包括Windows,Mac,Linux 支持多种浏览器

    2024年02月04日
    浏览(54)
  • WEB自动化测试(selenium工具)框架、面试题

                让程序员代替人为去验证web项目功能的过程      1)需求变动不频繁 测试脚本的稳定性决定了自动化测试的维护成本。如果软件需求变动过于频繁,测试人员需要根据变动的需求来更新测试用例以及相关的测试脚本,而脚本的维护本身就是一个代码开发的过程,

    2024年02月03日
    浏览(46)
  • 学会自动化必备工具-Selenium-再想着入坑自动化测试吧

    随着近些年IT行业的发展,软件测试人才的需求越来越大,也有很多小伙伴在考虑入坑,而软件测试中,收入相对较高的就是自动化了,所以这次就专门为大家简单介绍下自动化测试的必备工具Selenium。 Selenium是一款基于Web应用程序的开源测试工具 ,直接运行在浏览器中,支

    2024年01月23日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包