美文网首页
Linux下Selenium+Jenkins测试环境搭建

Linux下Selenium+Jenkins测试环境搭建

作者: 含辞未吐气若幽兰 | 来源:发表于2023-08-13 14:39 被阅读0次

1 install java

# yum search java-11-openjdk

# yum install java-11-openjdk.x86_64

2 install chrome for linux

# yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

2 view chrome version, # yum list |grep chrome

3 install chromedriver for linux

down chromedriver for linux

  # chmod 755 chromedriver_linux64.zip

  # unzip chromedriver_linux64.zip

  # chmod 777 chromedriver

  # cp chromedriver /usr/bin/chromedriver

4 install python3.7.7

# yum install epel-release -y

# cd /opt

# wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tar.xz

# yum install -y python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make

# tar -xvf Python-3.7.7.tar.xz

# cd Python-3.7.7

# ./configure --with-ssl --prefix=/usr/local/python3

# make && make install

# ln -s /usr/local/python3/bin/python3 /usr/bin/python3

# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

4 生成python虚拟环境

# python3 -m venv venv

进入虚拟环境

# source venv/bin/activate

查看安装包

# pip freeze

5 安装selenium

# pip3 install selenium==3.141.0 -i https://pypi.douban.com/simple

# pip3 install urllib3==1.26.2  重装低版本

# pip3 install yagmail

6 编写demo.py


from selenium import webdriver

from selenium.webdriver.chrome.options import Options

from unittest import mock

import time

def autodemo():

    # 实例化参数方法

    # chrome_options = Options()

    # 设置浏览器的无头浏览器, 无界面, 浏览器将不提供界面, Linux操作系统无界面下就可以运行

    chrome_options = webdriver.ChromeOptions()

    chrome_options.add_argument('--headless')

    chrome_options.add_argument('--no-sandbox')

    chrome_options.add_argument('--disable-gpu')

    chrome_options.add_argument('--disable-dev-shm-usage')

    #chrome_options.add_argument('--disable-extensions')

    # 实例化chrome, 导入设置项

    # test_webdriver = webdriver.Chrome('/opt/google/chrome/chromedriver', options=chrome_options)

    test_webdriver = webdriver.Chrome(options=chrome_options)

    # test_webdriver = webdriver.Chrome('/opt/chromedriver/chromedriver', options=chrome_options)

    test_webdriver.minimize_window()

    test_webdriver.get("https://www.baidu.com")

    test_webdriver.find_element_by_xpath("//input[@id='kw']").send_keys("python")

    test_webdriver.find_element_by_xpath("//input[@id='su']").click()

    time.sleep(2)

    print(test_webdriver.title)

    test_webdriver.quit()

autodemo()

将demo.py 放/data/auto下面,执行测试:

# python3 demo.py


7 install jenkins

# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo --no-check-certificate

# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

# yum upgrade

# yum install java-17-openjdk    if jdk has been installed correctly, do not run this

# yum install jenkins -y

# systemctl daemon-reload

# systemctl start jenkins  or #cd /etc/init.d    #./jenkins start

–no-sandbox –headless

创建自由风格项目,build step 加入:

cd /opt/auto

python3 demo.py

8 禁用Chrome自动更新

chrome版本更新,可能会导致驱动不兼容,可以考虑禁用chrome自动更新

# vim /usr/share/applications/google-chrome.desktop

找到Exec行,修改为:Exec=/usr/bin/google-chrome-stable --disable-component-update

相关文章

网友评论

      本文标题:Linux下Selenium+Jenkins测试环境搭建

      本文链接:https://www.haomeiwen.com/subject/clvopdtx.html