一、基础环境准备
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 selenium import webdriver
import time
import unittest class daTestCase(unittest.TestCase):
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")
print(u"加载驱动完成..")
driver.get("你的测试地址")
print(u"加载页面完成..")
time.sleep(2)
#self.driver.maximize_window() # 浏览器全屏显示 def test_login1(self):
global driver
driver.find_element_by_xpath("//input[@placeholder='请输入账号']").send_keys("daui")
driver.find_element_by_xpath("//input[@placeholder='请输入密码']").send_keys("Qwe123456")
driver.find_element_by_xpath("//*[@id=\"app\"]/div/div[5]/div/div/div/div[6]/button").click()
#self.driver.save_screenshot('D:/1.png') # 截图
print(u"case1:正确账号密码登录成功") def test_login2(self):
global driver
driver.find_element_by_xpath("//input[@placeholder='请输入账号']").send_keys("111st")
driver.find_element_by_xpath("//input[@placeholder='请输入密码']").send_keys("Qwe123456")
driver.find_element_by_xpath("//*[@id=\"app\"]/div/div[5]/div/div/div/div[6]/button").click()
#self.driver.save_screenshot('D:/2.png') # 截图
print(u"case2:账号错误登录失败") def test_login3(self):
global driver
driver.find_element_by_xpath("//input[@placeholder='请输入账号']").send_keys("daui")
driver.find_element_by_xpath("//input[@placeholder='请输入密码']").send_keys("1qwe123")
driver.find_element_by_xpath("//*[@id=\"app\"]/div/div[5]/div/div/div/div[6]/button").click()
#self.driver.save_screenshot('D:/3.png') # 截图
print(u"case3:密码错误登录失败") def tearDown(self):
global driver
print(u'用例执行完成..')
print(u"")
driver.quit() # 退出浏览器 if __name__ == '__main__':
unittest.main()
5.运行效果
1.png
2.png
to be continued...