selenium基本介绍和安装配置

selenium

  1. Selenium 的基础架构如上,它是用来做 Web 自动化测试的一个框架。可以使用 Python、Java、Ruby 等各种语言去编写你的自动化测试用例,然后测试用例会请求 Selenium 框架调用浏览器
  2. 浏览器的运行需要各自的 Driver 来驱动,Selenium 会借助每个浏览器的专属驱动来完成自动化
  3. 综上,Selenium 主要包括 Selenium Webdriver Client,也就是每种语言的客户端;以及 Selenium Driver 用于驱动各个浏览器。

相关文档

selenium 和 appium 架构对比

在之前的文章中介绍过appium环境的安装(win)appium环境的安装(mac)

  • Selenium IDE 等同于 Appium Desktop : 支持录制和导出脚本
  • Selenium Server 等同于 Appium Server:用于接受各种语言发送过来的请求,完成对浏览器或app的调用
  • Selenium Client 等同于 Appium Client :各个语言的客户端封装库,用于连接Server。Appium(即application + selenium移动端的selenium自动化工具) Client是在Selenium Client的基础上进行了简单的扩展,支持移动端的自动化测试
  • Selenium Grid:Selenium套件的一部分,用于实现分布式测试。利用Grid,可以很方便地同时在多台机器上(不同的浏览器、操作系统)和异构环境中并行运行多个测试事例

selenium环境安装

准确说是安装Selenium Client,Client 是每个语言的一个语言库,这里我以python为例,在项目对应的(虚拟)python环境执行如下命令即可

pip install selenium

chromedriver下载与配置

通过上面的介绍,还需要driver来驱动浏览器,这里以chrome浏览器对应的chromedriver为例

chromedriver下载

chromedriver配置

以下我还是以mac为例

  • 查看chrome版本如我的是87.0.4280.88,根据如上chrome版本与chromedriver对应关系可知我应该下载chromedriver版本是87.0.4280.88
  • 解压对应平台的chromedriver到指定目录,如我统一存放在~/Documents/chromedriver/对应的chrome版本号/chromedriver,则我当前版本的chrome版本存放在~/Documents/chromedriver/87.0.4280.88/chromedriver
  • 在$PATH的某个目录中创建chromedriver软链,如我的可执行如下命令
ln -s ~/Documents/chromedriver/87.0.4280.88/chromedriver /usr/local/bin/chromedriver

这样做的好处就是可以通过软链方便切换chromedriver版本,修改chromedriver软链指向的源文件即可
或者直接将对应版本的chrome路径添加到环境变量中(~/.zshrc): export PATH="$PATH:/xx chromedriver路径",如果修改的话直接修改这个环境变量

  • 测试chromedriver
$ which chromedrive
/usr/local/bin/chromedriver
$ chromedriver
Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

或执行如下python代码

from selenium  webdriver
browser = webdriver.Chrome()

如果弹出一个空白的 Chrome 浏览器,则证明所有的配置都没有问题

Selenium IDE

作为入门可使用录制工具来录制用例。官方提供了Selenium IDE,另外行业内还有一个比较常用的录制工具叫作 Katalon Studio。这里仅简单介绍Selenium IDE,可直接在chrome网上商店搜索并安装Selenium IDE扩展即可

  • 如我录制了一个访问百度搜索monkeyjerry的例子
  • 保存并导出python pytest即可得到如下脚本
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


class TestSearch():
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.vars = {}

    def teardown_method(self, method):
        self.driver.quit()

    def test_search(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.set_window_size(1440, 875)
        self.driver.find_element(By.ID, "kw").click()
        self.driver.find_element(By.ID, "kw").send_keys("monkeyjerry")
        self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)

录制的脚本严格意义并不是测试用例,只是步骤而已,并没有校验点。而且录制脚本并不便于维护,随着业务越来越复杂,单凭录制并不能很好的解决问题。这里并不详细介绍测试框架的封装,详细可参看基于Page Object的测试框架介绍