当前位置:网站首页>Automated Testing and Selenium
Automated Testing and Selenium
2022-08-10 10:23:00 【Java rookie~】
自动化测试及Selenium
一、自动化测试
自动化测试
:将人为驱动的测试行为转化为机器执行的过程;
自动化测试的优点:
(1)可以进行回归测试;
(2)节约资源;
(3)High reliability and can complete repeatability test;
(4)能够完成手工测试无法完成的测试(精准计时)
二、Selenium
1. Selenium 介绍及安装
Selenium
是一款用于web
应用测试的工具,由Selenium IDE
,Webdriver
与Selenium Grid
组成;
(1)Selenium IDE:录制自动化脚本的工具;
(2)webDriver:Drive the browser toWeb
元素进行操作;
(3)Selenium Grid:是一个服务器,提供对浏览器实例访问的服务器列表,管理各个节点的注册和状态信息;
安装命令:pip install selenium ==3.12.0(指定版本)
2. Selenium 优势
- 免费、Cabinet and easy to install;
- 支持多语言:
Java、Python、C#、javaScript
; - 支持多平台:
windows、Linux、Mac
; - 支持多浏览器:
chrome、Firefox、edge、safari
; - 支持分布式测试;
3. 安装驱动
谷歌浏览器驱动:驱动链接
点开链接,Select corresponds with their browser version can,由于采用Python
To implement the script development,因此,解压后将chromedriver.exe
文件,放在安装Python
的目录下;
After finish can be the following~
三、webDriver API
1. 定位元素
<1> id 定位
id
是页面元素的属性,Is the most commonly used element localization way;
Mainly by viewing the page elementsid
来进行定位(唯一
);
如:Regularly visit baidu page,By looking at the can know its input box id
,如下图所示:
The same method to locate the other elements on the page id
;
方法:
find_element_by_id("kw") :Positioning input box to baidu;
<2> name 定位
If the page elements have
name
,并且元素的name
Named in the entire page is the only,那么我们可以用name
To locate the elements;
同样的,Can be found through the above methodname
属性的值;
方法:
find_element_by_name("wd") :Positioning input box to baidu
<3> class name 定位
Before positioning need to guarantee uniqueness!
方法:
find_element_by_class_name("s_ipt") :Positioning input box to baidu;
<4> tag name 定位
tag name
: 标签名
方法:
find_element_by_tag_name("input") :Positioning input box to baidu;
<5> link text 定位
link text
:Can through links to content,也就是link text
To locate text link;
需要注意的是:Links to content must this page only,否则会报错;
方法:
find_element_by_link_text("hao123")
<6> partial link text 定位
Only part of the text matching;
方法:
find_element_by_partial_link_text("hao")
<7> xpath 定位
The globally unique positioning way,查找方法:
To the positioning of page elements,右击—>检查---->copy----> copy xpath
<8> css selector 定位
- 方法1:
find_element_by_css_selector("#kw")
- 方法2:
页面元素-----右键----copy----copy selector 来获取
2. 操作测试对象
Good location after need operation on this element,What specific actions,取决于需求,常见的有以下几种:
- 鼠标点击:
click
; - 键盘输入:
send_keys
; - 清除元素的内容:
clear
; - 提交表单:
submit
; - 获取文本信息:
text
;
case1:打开百度,Search TV series《天才基本法》
代码:
# 打开百度,The basic law of search TV genius
# 导入需要的工具包
from selenium import webdriver
import time
# 获取浏览器驱动
driver = webdriver.Chrome()
time.sleep(3)
# 获取百度页面
driver.get("http://www.baidu.com")
time.sleep(3)
# 定位百度输入框,采用idPositioning way to locate the elements,After input the basic law of genius
driver.find_element_by_id("kw").send_keys("天才基本法")
time.sleep(3)
driver.find_element_by_id("su").click()
time.sleep(3)
# 关闭浏览器
driver.quit()
case2
:Access to baidu words on the bottom of the page
代码:
# 导入需要的工具包
from selenium import webdriver
import time
# 获取浏览器驱动
driver = webdriver.Chrome()
time.sleep(3)
# 获取百度页面
driver.get("http://www.baidu.com")
time.sleep(3)
data = driver.find_element_by_id("bottom_layer").text
print(data)
time.sleep(3)
# 关闭浏览器
driver.quit()
结果如下:
3. 添加等待
- 固定等待:
sleep()
- 智能等待:
implicitly_wait()
4. 打印信息
- 打印
URL
# 导入需要的工具包
from selenium import webdriver
import time
# 获取浏览器驱动
driver = webdriver.Chrome()
time.sleep(3)
# 获取百度页面
driver.get("http://www.baidu.com")
time.sleep(3)
print(driver.current_url)
打印结果:
- 打印
title
# 导入需要的工具包
from selenium import webdriver
import time
# 获取浏览器驱动
driver = webdriver.Chrome()
time.sleep(3)
# 获取百度页面
driver.get("http://www.baidu.com")
time.sleep(3)
print(driver.title)
打印结果:
5. 操作浏览器
- 浏览器最大化:
maximize_window()
# 导入需要的工具包
from selenium import webdriver
import time
# 获取浏览器驱动
driver = webdriver.Chrome()
time.sleep(3)
# 获取百度页面
driver.get("http://www.baidu.com")
time.sleep(3)
driver.maximize_window()
time.sleep(2)
driver.quit()
- 设置浏览器宽和高:
set_window_size(480,800)
# 导入需要的工具包
from selenium import webdriver
import time
# 获取浏览器驱动
driver = webdriver.Chrome()
time.sleep(3)
# 获取百度页面
driver.get("http://www.baidu.com")
time.sleep(3)
driver.set_window_size(480,800)
time.sleep(2)
driver.quit()
- Set the browser forward and backward:
forward()
,back()
from selenium import webdriver
import time
# 访问百度首页
driver = webdriver.Chrome()
first_url = 'http://www.baidu.com'
driver.get(first_url)
time.sleep(2)
# 访问新闻页面
second_url='http://news.baidu.com'
driver.get(second_url)
time.sleep(2)
# 返回(后退)到百度首页
print("back to %s "%(first_url))
driver.back()
time.sleep(1)
# 前进到新闻页
print("forward to %s"%(second_url))
driver.forward()
time.sleep(2)
driver.quit()
6. 键盘事件
注意:要使用键盘按键,必须引入keys
包
导包格式:from selenium.webdriver.common.keys import Keys
通过send_keys()
调用按键(A single form):
- Tab 键 :
send_keys(Keys.TAB)
; - 回车键:
send_keys(Keys.Enter)
; - 空格键:
send_keys(Keys.Space)
; Esc
回退键:send_keys(Keys.ESCAPE)
;
通过send_keys()
调用按键(组合形式):
- 全选:
send_keys(Keys.CONTROL,'a')
- 复制:
send_keys(Keys.CONTROL,'c')
- 粘贴:
send_keys(Keys.CONTROL,'v')
- 剪贴:
send_keys(Keys.CONTROL,'x')
case:打开百度,将浏览器最大化,之后输入“Brush your teeth steps”,完成后,Selection and shear,To search“webdriver
”
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.maximize_window()
time.sleep(3)
# 输入框输入内容
driver.find_element_by_id("kw").send_keys("Brush your teeth steps")
time.sleep(3)
# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
time.sleep(3)
# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
time.sleep(3)
# 输入框重新输入内容,搜索
driver.find_element_by_id("kw").send_keys("webdriver")
time.sleep(3)
7. 鼠标事件
注意:To use the mouse events,Must be guide package;
导包格式:from selenium.webdriver.common.action_chains import ActionChains
- 右击:
context_click()
(Premise to locate elements) - 双击:
double_click()
(Premise to locate elements) - 拖动:
drag_and_drop()
- 移动:
move_to_element()
case:打开百度浏览器,最大化,And then search zhang SAN,Then right click and double click operation
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
# 获取新闻页面
driver.get("http://www.baidu.com")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("张三")
time.sleep(3)
driver.find_element_by_id("su").click()
b = driver.find_element_by_id("su")
# 右击
ActionChains(driver).context_click(b).perform()
time.sleep(3)
# 双击
ActionChains(driver).double_click(b).perform()
time.sleep(3)
driver.quit()
小结
selenium
安装 Test automation and the advantages of
webDriver API:
- 定位元素方式:
id,name,tag name,xpath, link text,partial link text ,class name,css selectot
- 常用方法:
clear()
:清除文本send_keys(value)
:键盘输入click()
:单击元素submit()
:提交表单text
:获取元素的文本quit()
:Exit the browser operation
- 操作浏览器:
driver.set_window_size(value,value)
:设置浏览器的大小,单位是像素;driver.maximize_window()
:浏览器最大化;driver.back()
:控制浏览器后退 ;driver.forward()
:控制浏览器前进;
- 获取信息:
driver.title
:获取网页标题driver.current_url
:获取网页url
- 鼠标事件:
context_click()
:右击double_click()
:双击drag_and_drop()
:拖动move_to_element()
:移动
Today to share so much,The next positioning positioning a set of elements and level,加油,铁子们(别忘了点赞+收藏)~~
边栏推荐
猜你喜欢
哈希表,哈希桶的实现
[Internet of Things Architecture] The most suitable open source database for the Internet of Things
SQL中的字符串截取函数
Array of shell scripts
关于编程本质那些事
「第二部:容器和微服务架构」(1) 基于容器应用架构设计原则
Lasso regression (Stata)
Numpy学习
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
ESP8266 Tutorial 2 - Burn AT Firmware
随机推荐
[C language] Header file #include
, conio is Console Input/Output (console input and output) 多租户技术
"Data Architecture": How can master data management (MDM) help my industry?
ESP8266 Tutorial 2 - Burn AT Firmware
态势丨黑客侵扰加剧,靶场为网络安全架设“防御盾”
MySQL executes the query process
【Redis】Redis入门教程(介绍 下载安装 Jedis 图形化界面)
dedecms支持Word内容一键上传
「数据架构」:主数据管理(MDM)对我的行业有什么帮助?
LCD DRM驱动框架分析一
Dialogue with Chen Ciliang: Nezha wants to popularize high-end products
高通 msm8953 LCD 休眠/唤醒 流程
C语言空白符、空格符 与转义字符题点总结
2022.8.9-----leetcode.1413
C语言题解:倒置字符串
"Data Strategy" Results-Driven Enterprise Data Strategy: Organization and Governance
中国驻越南使馆提醒在越北部、中部地区中国公民做好台风“木兰”安全防范
database transaction
ESP8266-Arduino编程实例-MQ-8氢气传感器驱动
关于json转换器缺失的问题,报错内容:No converter found for return value of type