1.本机上同时执行多个浏览器
import threading,sys
from time import sleep,ctime
from selenium import webdriver
path_list=sys.path[0].split("/")
index = path_list.index("SeleniumOfJenkins")+1
driver_path="/".join(path_list[:index])+"/driver/"
def BaiduSearch(browser,search):
print(browser,search)
base_url="http://www.baidu.com"
if browser=="chrome":
driver=webdriver.Chrome(executable_path=driver_path+"chromedriver")
elif browser=="firefox":
driver=webdriver.Firefox(executable_path=driver_path+"geckodriver")
elif browser == "safari":
driver=webdriver.Safari()
driver.implicitly_wait(10)
driver.get(base_url+"/")
driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys(search)
driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
assert "百度为您找到相关结果约" in result_text
sleep(3)
driver.quit()
if __name__=="__main__":
lists = {"chrome": "谷歌", "firefox": "火狐", "safari": "苹果"}
file = range(len(lists))
threads = []
for b, s in lists.items():
t = threading.Thread(target=BaiduSearch, args=(b, s))
threads.append(t)
for i in file:
threads[i].start()
print("线程%s 启动 %s"%(i,ctime()))
for i in file:
threads[i].join()
print("线程%s 运行 %s"%(i,ctime()))
2.分布式执行测试用例
import threading,sys
from time import sleep,ctime
from selenium import webdriver
path_list=sys.path[0].split("/")
index = path_list.index("SeleniumOfJenkins")+1
driver_path="/".join(path_list[:index])+"/driver/"
def BaiduSearch(host,browser):
print(host,browser)
base_url="http://www.baidu.com"
dc={"browserName":browser}
driver = webdriver.Remote(command_executor=host,desired_capabilities=dc)
driver.implicitly_wait(10)
driver.get(base_url+"/")
driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys(browser)
driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
assert "百度为您找到相关结果约" in result_text
sleep(3)
driver.quit()
if __name__=="__main__":
lists = {"http://192.168.63.143:5555/wd/hub":"chrome", "http://127.0.0.1:4444/wd/hub":"firefox", "http://127.0.0.1:5555/wd/hub":"safari"}
file = range(len(lists))
threads = []
for b, s in lists.items():
t = threading.Thread(target=BaiduSearch, args=(b, s))
threads.append(t)
for i in file:
threads[i].start()
print("线程%s 启动%s"% (i,ctime()))
for i in file:
threads[i].join()
print("线程%s 运行%s" % (i,ctime()))
print("全部结束",ctime())
3FAQ
3.1.分布式执行时,写入了3个浏览器,但只执行了2个
lists = {"http://127.0.0.1:5555/wd/hub":"chrome", "http://127.0.0.1:4444/wd/hub":"firefox", "http://127.0.0.1:5555/wd/hub":"safari"}
print(len(lists))
for i,j in lists.items():
print(i,j)
原因:字典中key必须唯一。否则后面传入键值对会替换掉重名的键值对。