Linux 下如何利用無(wú)頭瀏覽器執(zhí)行web自動(dòng)化的用例?本文以centos 7為例,安裝chrome、利用Python + conda +selenium簡(jiǎn)單演示執(zhí)行自動(dòng)化。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-606135.html
- 安裝chrome瀏覽器
- 安裝chromedriver
- Linux安裝好Python環(huán)境-conda
- 設(shè)置Chrome為無(wú)頭瀏覽模式(只在后臺(tái)內(nèi)存中執(zhí)行、無(wú)瀏覽器頁(yè)面)
- 準(zhǔn)備測(cè)試用例
- 執(zhí)行用例
1、Chrome瀏覽器安裝
- 安裝必要依賴
yum install liberation-fonts -y
yum install vulkan-1.1.97.0-1.el7.x86_64 -y
- 下載安裝Chrome瀏覽器
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
rpm -ivh google-chrome-stable_current_x86_64.rpm
2、chromedriver配置
- chromedriver下載地址: https://registry.npmmirror.com/binary.html?path=chromedriver/
# 查看chrome版本
google-chrome --version
# 根據(jù)版本下載對(duì)應(yīng)chromedriver
wget https://registry.npmmirror.com/-/binary/chromedriver/104.0.5112.79/chromedriver_linux64.zip
# 解壓
unzip chromedriver_linux64.zip
# 將解壓的文件放到 /usr/bin/ 文件下
mv chromedriver /usr/bin
# 添加執(zhí)行權(quán)限
chmod +x /usr/bin/chromedriver
3、Linux的Python環(huán)境-conda
- 因?yàn)锳naconda3過(guò)大,現(xiàn)使用更小的miniconda,下載地址:https://docs.conda.io/en/latest/miniconda.html
# 下載
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# 執(zhí)行安裝腳本,一路enter,提示yes則yes,直到提示:Thank you for installing Miniconda3!
sh Miniconda3-latest-Linux-x86_64.sh
# 安裝完成,配置環(huán)境變量(如已存在則直接跳過(guò))
vim ~/.bashrc
export PATH="/root/miniconda3/bin:$PATH"
# 如果提示沒(méi)有命令,需刷新bashrc
source ~/.bashrc
- conda使用,更多使用方法參考官方文檔:https://docs.conda.io/projects/conda/en/latest/
# 安裝完成查看版本
conda -V
# 查看當(dāng)前有哪些環(huán)境
conda env list
# base 為默認(rèn)環(huán)境(盡量不用)
# 退出默認(rèn)的環(huán)境
source activate
conda deactivate
# 創(chuàng)建conda環(huán)境
conda create -n wulitou_py3.9 python==3.9 pytest selenium
# -n 后為環(huán)境名稱
# python==3.9 指定Python版本
# pytest selenium 為需要添加的依賴(可省略暫不添加,后續(xù)在添加)
# 激活環(huán)境(使用環(huán)境)
conda activate wulitou_py3.9
# 如在創(chuàng)建時(shí)未添加依賴,可添加(yes直接確認(rèn))
conda install pytest -y
# 查看已安裝的依賴
conda list
4、無(wú)頭瀏覽器設(shè)置
chrome_options = webdriver.ChromeOptions()
# 設(shè)置driver以無(wú)頭瀏覽的模式運(yùn)行
chrome_options.add_argument('-headless')
# Linux上需要禁用sandbox
chrome_options.add_argument('-no-sandbox')
# 禁用GPU(可選)
chrome_options.add_argument('-disable-gpu')
self.driver= webdriver.Chrome(options=self.chrome_options)
5、準(zhǔn)備測(cè)試用例
- 因利用pytest、selenium,因此conda 需要提前添加依賴
conda install pytest -y
conda install selenium -y
- 完整test_ui.py
# -*-coding:utf-8 -*-
"""
@File : test_ui.py
"""
import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestSahi:
def setup_class(self):
self.chrome_options = webdriver.ChromeOptions()
# 設(shè)置driver以無(wú)頭瀏覽的模式運(yùn)行
self.chrome_options.add_argument('-headless')
# Linux上需要禁用sandbox
self.chrome_options.add_argument('-no-sandbox')
# 禁用GPU(可選)
self.chrome_options.add_argument('-disable-gpu')
self.driver= webdriver.Chrome(options=self.chrome_options)
# Linux需要手動(dòng)設(shè)置大小,不使用最大化
self.driver.set_window_size(1366, 768)
self.driver.implicitly_wait(3)
def teardown_class(self):
time.sleep(2)
self.driver.quit()
# 業(yè)務(wù)代碼
def testAlter_01(self):
self.driver.get("https://sahitest.com/demo/clicks.htm")
# 雖無(wú)界面,依舊可截圖查看
self.driver.save_screenshot("sahitest-1.png")
self.driver.find_element(By.XPATH, '//input[@value="click me"]').click()
self.driver.find_element(By.XPATH, '//input[@value="click me"]').click()
def testAlter_02(self):
self.driver.get("https://sahitest.com/demo/clicks.htm")
self.driver.find_element(By.XPATH, '//input[@value="click me"]').click()
self.driver.save_screenshot("sahitest-2.png")
self.driver.find_element(By.XPATH, '//input[@value="click me"]').click()
self.driver.find_element(By.XPATH, '//input[@value="click me"]').click()
self.driver.find_element(By.XPATH, '//input[@value="click me"]').click()
self.driver.save_screenshot("sahitest-3.png")
if __name__ == '__main__':
pytest.main()
# pytest -vs test_ui.py
6、執(zhí)行測(cè)試用例
pytest -vs test_ui.py
執(zhí)行結(jié)束后,當(dāng)前位置下生成對(duì)應(yīng)截圖文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-606135.html
到了這里,關(guān)于Linux無(wú)頭瀏覽器的自動(dòng)化測(cè)試實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!