Centos7.x 使用 selenium + python + jenkins 做UI自动化
一、基础环境准备
1.Chrome + Chrome Driver
https://www.cnblogs.com/TSmagic/p/15671533.html(此篇文章已经介绍)
2.Selenium + Python
pip install selenium
pip show selenium
Python环境安装就不多做介绍啦!
3.Jenkins
https://www.cnblogs.com/TSmagic/p/15080407.html(此篇文章已经介绍)
4.测试代码
# coding=utf-8 # lm-ui—test—demo-博客园 from ddt import ddt from selenium.webdriver.common.by import By from selenium import webdriver import time import unittest # 也可以使用ddt创建模拟数据 # @ddt class daTestCase(unittest.TestCase): # 登录元素 user_name_loc = (By.XPATH, "//input[@placeholder='请输入账号']") pass_word_loc = (By.XPATH, "//input[@placeholder='请输入密码']") login_button_loc = (By.XPATH, "//*[@id=\"app\"]/div/div[5]/div/div/div/div[6]/button") # 登录账号信息 account = ["daui", "Qwe123456"] def setUp(self): global driver chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument('--headless') # 浏览器无窗口加载 chromeOptions.add_argument('--disable-gpu') # 不开启GPU加速 """ 解决报错: selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (unknown error: DevToolsActivePort file doesn't exist) """ chromeOptions.add_argument('--disable-dev-shm-usage') # 禁止 chromeOptions.add_argument('--no-sandbox') # 以根用户打身份运行Chrome,使用-no-sandbox标记重新运行Chrome # 其它设置(可选): chromeOptions.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面 chromeOptions.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度 # 创建driver对象 # chrome_options=chromeOptions加载设置 # executable_path="/usr/bin/chromedriver"指定webdriver路径(可选) driver = webdriver.Chrome(chrome_options=chromeOptions, executable_path="/usr/bin/chromedriver") driver.get("你的测试地址") time.sleep(2) # self.driver.maximize_window() # 浏览器全屏显示 def test_login1(self): try: driver.find_element(*self.user_name_loc).send_keys(self.account[0]) driver.find_element(*self.pass_word_loc).send_keys(self.account[1]) driver.find_element(*self.login_button_loc).click() # self.driver.save_screenshot('D:/1.png') # 截图 except: print("登录失败") else: print("登录成功") def tearDown(self): print("用例执行完成..") driver.quit() # 退出浏览器 if __name__ == '__main__': unittest.main()
5.运行效果
1.png
2.png
to be continued...