自动化测试工具Selenium的语法续.

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

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

目录

一、定位一组元素

二、下拉框处理

三、上传文件

四、quit和close的区别

五、浏览器页面跳转

六、截图操作


一、定位一组元素

webdriver 可以很方便的使用 fifindElement 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用fifindElements 方法。
定位一组对象一般用于以下场景:
1、批量操作对象,比如将页面上所有的 checkbox 都勾选上;
2、先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的
checkbox ,然后选择最后一个;
OK,我们来做个具体的演示:
1、在我们的桌面上新建一个文本文件,然后把下面的前端代码填入保存;

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkbox</title>
</head>
<body>
<h3>checkbox</h3>
<div class="well">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="c1">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c1" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c2">checkbox2</label>
<div class="controls">
<input type="checkbox" id="c2" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c3">checkbox3</label>
<div class="controls">
<input type="checkbox" id="c3" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="r">radio</label>
<div class="controls">
<input type="radio" id="r1" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="r">radio</label>
<div class="controls">
<input type="radio" id="r2" />
</div>
</div>
</form>
</div>
</body>
</html>

2、保存之后,修改文件的名称为“xx.html”,我这里取的是demo6.html;

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

3、双击打开,观察界面展示;

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试 

那么这里我们想实现一次性勾选checkbox1,2,3这三个选项,即实现多选,那么怎么做呢?

也就是我们开头提到的定位一组元素,看代码实现:

package AutoTest.Selenium1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;

//定位一组元素
public class demo6 {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.get("file:///C:/Users/ASUS/Desktop/demo6.html");

        List<WebElement> webElements = driver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            if(webElements.get(i).getAttribute("type").equals("checkbox")){
                webElements.get(i).click();
            }else{
                //什么也不做
            }
        }
    }
}

我们发现是使用了List<WebElement>来存储所有带input标签的元素;

这个时候我们查看网页源代码,我们发现每个可以勾选的按钮都带有type属性;

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

 那么就可以使用方法getAttribute("type"),括号中放的是数据类型,来定位到所有类型带type的元素;定位到之后进行click()点击操作;

运行结果:

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

二、下拉框处理

那么我们在设计自动化用例的时候也经常会遇到input带下拉框的这种情况,比如下面:

源代码:

<html>
<body>
<select id="ShippingMethod"
onchange="updateShipping(options[selectedIndex]);" name="ShippingMethod">
<option value="12.51">UPS Next Day Air ==> $12.51</option>
<option value="11.61">UPS Next Day Air Saver ==> $11.61</option>
<option value="10.69">UPS 3 Day Select ==> $10.69</option>
<option value="9.03">UPS 2nd Day Air ==> $9.03</option>
<option value="8.34">UPS Ground ==> $8.34</option>
<option value="9.25">USPS Priority Mail Insured ==> $9.25</option>
<option value="7.45">USPS Priority Mail ==> $7.45</option>
<option value="3.20" selected="">USPS First Class ==> $3.20</option>
</select>
</body>
</html>

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

那么这种情况的话直接使用普通的cssSelector或者xpath方法是无法完成页面元素定位的,那么如何操作呢?这里主要会用到一个Select函数,创建一个Select对象,然后调用里面对应的方法;常见的使用下标定位或者通过value来进行定位;

package AutoTest.Selenium1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;

//下拉框处理
public class demo7 {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("file:///C:/Users/ASUS/Desktop/demo7.html");

        WebElement webElement = driver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
        select.selectByIndex(3);
        select.selectByValue("9.03");
    }
}

value值查看:

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

三、上传文件

 先构建一个页面,代码如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>upload_file</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
<link
href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstra
p-combined.min.css" rel="stylesheet" />
<script type="text/javascript">
</script>
</head>
<body>
<div class="row-fluid">
<div class="span6 well">
<h3>upload_file</h3>
<input type="file" name="file" />
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitterbootstrap/2.3.2/js/bootstrap.
min.js"></script>
</html>

效果:

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

自动化代码:

package AutoTest.Selenium1;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

//上传文件
public class demo8 {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("file:///C:/Users/ASUS/Desktop/demo8.html");

        driver.findElement(By.cssSelector("input")).sendKeys("C:\\Users\\ASUS\\Desktop\\UPUP.txt");

    }
}

 定位到上传文件的按钮,然后输入自己要上传文件的路径即可,这里使用绝对路径和相对路径均可;运行结果:

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

四、quit和close的区别

quit是关闭整个浏览器,close只是关闭当前窗口,即quit会清空缓存,而close不会清空缓存。

代码验证:

package AutoTest.Selenium1;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import static java.lang.Thread.sleep;

//quit和close的区别
public class demo9 {
    public static void main(String[] args) throws InterruptedException {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            ChromeDriver driver = new ChromeDriver(options);
            driver.manage().window().maximize();
            driver.get("https://www.baidu.com");

//            driver.findElement(By.cssSelector("#kw")).sendKeys("杨幂");
            driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
            sleep(3000);
            //quit是关闭整个浏览器
//            driver.quit();
            //close只是关闭当前窗口
            driver.close();
            //即quit会清空缓存,而close不会清空缓存。
    }
}

 使用close()函数时,现在是打开了两个窗口句柄;

随着代码的运行,关闭了一个窗口句柄;

如果使用quit的话是运行结束之后直接退出浏览器;

五、浏览器页面跳转

那么我们在设计自动化代码的时候,可能会遇到页面从当前页面跳转到另一个新的页面,那么这个时候再去使用cssSelector或者Xpath方法去定位元素的话,肯定是定位不到的,因为跳转到了新的页面,get方法打开的是旧的页面,那么如何解决呢?

比如在百度首页,我们点击新闻这个超链接,那么浏览器会打开两个页面;

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

点击“新闻”按钮,来到了一个新的页面;

 

那么我们发现这个页面也是可以进行搜索框输入数据进行百度一下的;

看代码注释:

package AutoTest.Selenium1;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Set;

import static java.lang.Thread.sleep;

public class demo10 {
    public static void main(String[] args) throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("https://www.baidu.com");

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

        //通过getWindowHandles获取所有的窗口句柄
        //通过getWindowHandle获取get打开的页面窗口句柄
        Set<String> handles =  driver.getWindowHandles();
        //target用来获取当前最新的页面地址
        String target = "";
        for (String handle:handles) {
            target = handle;
        }
        driver.switchTo().window(target);
        driver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        driver.findElement(By.cssSelector("#s_btn_wr")).click();
    }
}

运行结果:

六、截图操作

这个操作的话就是会在指定的页面进行截图,然后保存到对应的路径,在实际工作中对比与我们的预期结果是否一致;

1、首先需要在我们的配置文件pom.xml中导入依赖;

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

 依赖代码:

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

自动化代码:

package AutoTest.Selenium1;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;

import static java.lang.Thread.sleep;

/**
 * 截图操作:需要导入依赖,可以去pom.xml文件中查看依赖,依赖从maven中央仓库中搜索common-io,下载。
 */
public class dmeo11 {
    public static void main(String[] args) throws IOException, InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("https://www.baidu.com");

        driver.findElement(By.cssSelector("#kw")).sendKeys("自动化测试");
        driver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D://demo10.png"));
    }
}

运行可以去我们自己设定的目录路径下面查看是否生成了对应的截图:

自动化测试工具Selenium的语法续.,【测试开发工程师】,selenium,测试工具,单元测试

OK,以上就是selenium的所有常见操作的方法了,创作不易,可以动动小手一键三连啦,我们下篇博客更新自动化测试框架Junit的用法~文章来源地址https://www.toymoban.com/news/detail-688032.html

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

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

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

相关文章

  • python自动化测试工具selenium

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

    2024年02月08日
    浏览(73)
  • 如何使用自动化测试工具Selenium?

    哈喽,大家好,我是小浪。那么有一段时间没有更新了,还是在忙实习和秋招的事情,那么今天也是实习正式结束啦,开始继续更新我们的学习博客,后期主要是开发和测试的学习博客内容巨多,感兴趣的小伙伴们可以一键三连支持一下欧~ 目录 一、什么是自动化测试? 二、

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

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

    2023年04月09日
    浏览(106)
  • 自动化测试工具selenium的安装方法

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

    2024年02月09日
    浏览(40)
  • Selenium教程:自动化浏览器测试工具

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

    2024年02月07日
    浏览(52)
  • Web测试自动化工具Selenium的使用

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

    2024年01月16日
    浏览(74)
  • 自动化测试工具-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日
    浏览(39)
  • 学会自动化必备工具-Selenium-再想着入坑自动化测试吧

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

    2024年01月23日
    浏览(46)
  • Python自动化测试工具selenium使用指南

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

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

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

    2024年02月08日
    浏览(38)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包