这里简单的列举下如何启动多个浏览器,这里先把思路进行列出来,根据思路进行一步步写
1、先把浏览器驱动单独封装起来,调用时,直接选取对应的驱动
2、把想要执行的用例进行也单独封装起来。
3、通过多线程进行调用封装的用例,每启动1次线程,就运行一次用例。
4、把多个浏览器的名称列举出来,通过循环的方式把浏览器参数传入到用例中。从而进行执行用例
代码如下:
from selenium import webdriver
import threading
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
# 封装浏览器驱动
def Browser(browser):
driver = None
if browser == "ie":
driver = webdriver.Ie()
elif browser == "firefox":
binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
# driver = webdriver.Firefox()
elif browser == "chrome":
driver = webdriver.Chrome()
else:
print('输入的正确的浏览器信息')
return driver
# 封装用例
def test_baidu(browser_name):
driver = Browser(browser_name)
driver.get('https://www.baidu.com')
driver.find_element_by_id('kw').send_keys('yx_test')
driver.find_element_by_id('su').click()
print(driver.title)
print('通过浏览器:', browser_name)
time.sleep(2)
driver.quit()
# 封装多线程
def run_case(name):
thread_list = []
for i in range(1):
appium_server = threading.Thread(target=test_baidu, args=(name,))
thread_list.append(appium_server)
for j in thread_list:
j.start()
if __name__ == '__main__':
a = ('chrome', 'firefox')
for i in a:
run_case(i)
运行结果:
分别打开谷歌和火狐浏览器,均输入内容进行搜索
遇到的问题及解决方法: