web自动化测试(3):web功能自动化测试selenium基础课

继上篇《web自动化测试(1):为何选择selenium作自动化测试》,本文介绍如selenium使用css

作UI自动化测试,须要什么技能

  1. 前端相关技术:HTML、XML、JavaScript、TCP/IP协议等html

  2. 合适的工具选型:好比selenium,好比UTF等;前端

  3. 编程语言:selenium支持多种语言,java、C++、python、JavaScript等java

  4. 需求分析:项目类型,特质,生命周期,是否适合开展自动化测试等;python

若是只是作UI差别与还原度对比,用不着selenium大驾,phantomjs截图对比就好,推荐一个基于dom diff算法UI监控工具:page-monitorjquery

原本准备万字长文,好好嗑叨一番,可是,以为实在务必要。官网的介绍都很细:https://selenium.dev/documentation/en/,中文把en改成zh-cn便可:https://selenium.dev/documentation/zh-cn/git

慕课网教程:Java Web自动化测试 Selenium基础到企业实际应用 https://coding.imooc.com/class/359.htmlgithub

selenium如今官网分为三件套:web

  1. Selenium IDE:是嵌入到浏览器的插件(目前仅支持chrome、Firefox),录制和回放Selenium脚本,录制好的脚本转换成各类Selenium WebDriver支持的程序语言,进而扩展到更普遍的浏览器类型;ajax

  2. Selenium WebDriver:可支持多种语言,用于操做浏览器的一套API;支持各种型浏览器,跨操做系统;

  3. Selenium Grid:用于远程控制、分布式部署等,都可实现Selenium脚本的高效执行与拓展;使得自动化测试能够并行运行,甚至在跨平台、异构的环境中运行,包括主流的移动端环境,如Android、iOS

Selenium IDE操做指北

关键是chrome因为伟大的墙存在,下载不了chrome Selenium IDE扩展插件,若有须要,去本人的收集列表看看《Chrome扩展程序导出备份与本地导入浏览器 》

首先是Selenium IDE录制页面操做脚本,这个其实不作测试,平时玩着也蛮有趣的,操做以下

一、点击Selenium IDE插件,弹出界面,新建工程,输入测试用例名,点击ok,打开一个新窗口,(后面默认开始录制,你全部的操做没有按中止前,都是在录制转态)

二、再次点击,Selenium IDE插件,弹出界面,关闭录制按钮

三、操做界面。

这里的图很草。其实能够随便玩。像小孩子玩手机同样,左monkey测试,点多了天然会了

WebDriver操做

Selenium 的核心是 WebDriver,这是一个编写指令集的接口,能够在许多浏览器中互换运行

它容许用户模拟终端用户执行的常见活动;虽然 Selenium 主要用于网站的前端测试,但其核心是浏览器用户代理库。

webDrive直接下便可,https://selenium.dev/downloads/,前端,npm i selenium-webdriver

接着就是编写脚本,也能够从上面Selenium IDE

// 不论是java 仍是python,都是须要导入webdriver包
// 库下载 https://selenium.dev/documentation/zh-cn/selenium_installation/installing_selenium_libraries/
const {Builder, By, Key, until} = require('selenium-webdriver');

(async function example() {
    let driver = await new Builder().forBrowser('chrome').build();
    try {
        // 控制浏览器:https://selenium.dev/documentation/zh-cn/webdriver/browser_manipulation/
        // 打开浏览器,WebDriver一般能够说有一个阻塞API。web平台本质上是异步的,因此WebDriver不跟踪DOM的实时活动状态。
        await driver.get('https://www.zhoulujun.cn/search.html?m=search&c=index&a=init&siteid=1&typeid=1&ajax=1&q=tar');

        // 找到搜索框(也能够 By.id('bdcsMain')),输入test,回车。
        await driver.findElement(By.name('q')).sendKeys('test', Key.ENTER);
        // 搜索页面,找到
        let firstResult = await driver.wait(until.elementLocated(By.css('h3>div')), 10000);
        console.log(await firstResult.getAttribute('textContent'));
        // 关闭浏览器
        browser.close()
    }
    finally{
        driver.quit();
    }
})();

webDrive库的安装:https://selenium.dev/documentation/zh-cn/selenium_installation/installing_selenium_libraries/

webDrive驱动安装:https://selenium.dev/documentation/zh-cn/selenium_installation/installing_webdriver_binaries/

若是不安装驱动,就会报错:The ChromeDriver could not be found on the current PATH. Please download the latest version of the

chrome驱动以下:http://chromedriver.storage.googleapis.com/80.0.3987.106/chromedriver_win32.zip

查找web元素,是selenium测试的重点,https://selenium.dev/documentation/zh-cn/webdriver/web_element/

查找元素的九种方法

By查找

  1. By.id

  2. By.name

  3. By.tagName

  4. By.className

  5. By.xpath  //经常使用

  6. By.css("#kw")

  7. By.partialLinkText //模糊匹配

  8. By.linkText("糯米") eg:<a>糯米<a/>

其实也就是咱们常见的 css jquery 元素选择器而已。

 

browser查找

browser.find_element_by_css_selector('.logo')

这个去查API,或者查找browser方法

操做页面元素的方法

获取元素后,能够对页面元素执行相应的动做

input.clear()

input.send_keys('zhoulujun',Key.ENTER)

input.click()

在页面执行JavaScript

也能够写js执行

browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')

browser.execute_script('alert("To Bottom")')

Selenium Grid

Grid服务网格, 一台服务器做为转发器(hub)将JSON格式的测试命令转发到1台或多台注册的节点。 测试任务经过跟转发器(hub)的交互来操做远端浏览器实例。 转发器(hub)维护了一个可供使用的注册服务器列表,也容许咱们经过转发器(hub)来控制这些实例。容许咱们在多台节点服务器上并行执行测试, 同时也中心化的管理多个浏览器版本,多种浏览器的配置。(以替代传统的基于我的的测试)

grid.png

按照官方的说法,下载 selenium-server-standalone JAR 文件,而后放在Tomcat起个服务就可。具体怎么配置,有空了再来写一篇。

 

同行文章推荐:

如何进行前端自动化测试? https://www.zhihu.com/question/29922082/answer/189594079

使用 Selenium 实现基于 Web 的自动化测试 https://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb/index.html

Selenium自动化测试入门(基于Python)http://www.javashuo.com/article/p-sxafxjlp-c.html

相关文章
相关标签/搜索