web自动化测试(python+selenium)


环境配置

Python安装

Windows

https://www.python.org/

Linux

下载文件
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
进行安装
    tar xf Python-3.7.1.tar.xz
    cd Python-3.7.1
    yum -y install gcc-* openssl-* libffi-devel sqlite-devel
    ./configure --enable-optimizations --with-openssl=/usr/bin/openssl
    make -j4
    make install
    默认安装路径:/usr/local/lib/python3.7
更改安装源
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
升级pip
pip3 install --upgrade pip
安装虚拟环境工具
pip3 install virtualenv
创建虚拟环境
virtualenv web01
切换到虚拟环境
source web01/bin/activate
退出虚拟环境
   deactivate

Selenium

检查

打开dos窗口输入:

pip

正常应显示以下内容:

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to
                              WARNING, ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
                              (a)bort).
  --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the
                              certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output

安装

dos输入在线安装selenium指令:

pip install selenium==2.53.6

验证安装

确保已经安装Firefox/Chrome/Ie浏览器中的一个

以Firefox为例(Firefox请安装46以下的版本),打开dos窗口输入指令:

python
from selenium import webdriver
webdriver.Firefox()

如果Firefox浏览器被调用启动,则表示selenium安装成功,其他浏览器请查阅“浏览器驱动”

浏览器驱动

Firefox

请勿安装47以上的版本(selenium2不兼容47以上版本)

firefox历年版本的官方镜像地址:
https://download-installer.cdn.mozilla.net/pub/firefox/releases/

Chrome

selenium启动Chrome浏览器需要安装驱动包,不同的Chrome浏览器版本号对应的驱动文件版本号也不同,若不匹配,则无法启动。

查看Chrome版本号?

浏览器内:设置->关于->版本号

浏览器外:打开Chrome安装文件夹

Chrome浏览器chromedriver各版本驱动大全,下载地址:
http://chromedriver.storage.googleapis.com/index.html

下载驱动成功后,确保chromedriver.exe文件放置于path路径下,我之前将环境变量配置到了python根目录,所以一般将chromedriver.exe放置于python安装的根目录。

IE

IE浏览器的IEdriver各版本驱动大全,下载地址:
http://selenium-release.storage.googleapis.com/index.html

selenium之webdriver框架

webdriver API

# 导入webdriver模块
from selenium import webdriver

# 导入计时器模块
import time

# 打开Chrome浏览器
# 其他浏览器替换浏览器名即可
driver = webdriver.Chrome()

# 使用webdriver模块api导入网址
driver.get("https://auntyang.tk")

# 计时器6s等待时间,一些异常报错可能就是因为没有在语句下设置等待时间
# time模块的单位是s(秒),参数可以是小数也可以是整数
time.sleep(6)

# 截屏保存到指定目录,参数格式:保存路径+文件名称+后缀
driver.get_screenshot_as_file("D:\\test.jgp")

# 设置指定浏览器窗口尺寸
driver.set_window_size(540, 960)
# 设置浏览器窗口最大化
driver.maximize_window()

# 上一页,相当于浏览器左上角的左箭头按钮
driver.back()
# 下一页,相当于浏览器左上角的右箭头按钮
driver.forward()

# 使用webdriver模块api进行刷新
# 相当于浏览器输入框后面的刷新按钮
driver.refresh()

# 结束浏览器进程
driver.quit()
# 关闭当前窗口
driver.close()

元素定位

webdriver框架提供了18种元素定位方法,前8种通过元素属性定位,后10种通过xpan和css定位

#id定位:
find_element_by_id(self, id_)
#name定位:
find_element_by_name(self, name)
#class定位:
find_element_by_class_name(self, name)
#tag定位:
find_element_by_tag_name(self, name)
#link定位:
find_element_by_link_text(self, link_text)
#partial_link定位
find_element_by_partial_link_text(self, link_text)
#xpath定位:
find_element_by_xpath(self, xpath)
#css定位:
find_element_by_css_selector(self, css_selector)

复数形式:

#id复数定位
find_elements_by_id(self, id_)
#name复数定位
find_elements_by_name(self, name)
#class复数定位
find_elements_by_class_name(self, name)
#tag复数定位
find_elements_by_tag_name(self, name)
#link复数定位
find_elements_by_link_text(self, text)
#partial_link复数定位
find_elements_by_partial_link_text(self, link_text)
#xpath复数定位
find_elements_by_xpath(self, xpath)
#css复数定位
find_elements_by_css_selector(self, css_selector)

参数化方法:

find_element(self, by='id', value=None)
find_elements(self, by='id', value=None)

xpath定位

标签

1.有时候同一个属性,同名的比较多,这时候可以通过标签筛选下,定位更准一点
2.如果不想制定标签名称,可以用*号表示任意标签
3.如果想制定具体某个标签,就可以直接写标签名称

层级

1.当某个元素属性不明显,无法直接定位时,可以先找其父元素

2.找到父元素后,向下一个层级就能定位到该元素

索引

1.某元素标签和另一元素标签一样,且无法通过层级定位时,属于同父元素

2.同父元素的元素存在顺序,用索引定位

逻辑运算

1.xpath可以对多个属性进行逻辑运算,支持与(and)、或(or)、非(not)

模糊匹配

1.xpath可以对元素进行模糊匹配

小测试

编写脚本,打开百度首页,输入关键字“robotframework”,点击搜索

结果如下图所示:

Web自动化测试

CSS定位


文章作者: auntyang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 auntyang !
 上一篇
WPF开发小记 WPF开发小记
用.NET&C#为一个农业自动化灌溉的物联网项目写的WPF用户端系统,.NET作为微软自家的框架在win下的软件开发和ui设计都非常方便,可惜兼容性不如Qt。
2020-09-07
下一篇 
使用网络协议分析仪Wireshark 使用网络协议分析仪Wireshark
掌握安装和配置网络协议分析仪Wireshark的方法、掌熟悉使用Wireshark工具分析网络协议的基本方法,加深对协议格式、协议层次和协议交互过程的理解
2020-05-16
  目录