Objective
If you want to run the same set of test code with multiple browsers, Driver=webdriver. Firefox () driver here can't be written dead, you can parameterize the name of the browser.
Follow-up if you want to implement multi-threaded and start the browser to execute the use case, with the Tomorrow module, set the number of threads to apply it.
Launch browser
1. In order to achieve the flexible switching of multiple browsers, you can start the browser to write a function, parameters with the browser name on the line
defStartbrowser (name):"""Open Browser functions, "Firefox", "Chrome", "ie", "PHANTOMJS"""" Try: ifName = ="Firefox" orName = ="Firefox" orName = ="FF": Print("Start Browser Name:firefox") Driver=Webdriver. Firefox ()returnDriverelifName = ="Chrome" orName = ="Chrome": Print("Start Browser Name:chrome") Driver=Webdriver. Chrome ()returnDriverelifName = ="IE" orName = ="Ie": Print("Start Browser Name:ie") Driver=Webdriver. Ie ()returnDriverelifName = ="Phantomjs" orName = ="Phantomjs": Print("Start Browser name:p Hantomjs") Driver=Webdriver. PHANTOMJS ()returnDriverElse: Print("Not found this browser,you can use ' Firefox ', ' Chrome ', ' ie ' or ' phantomjs '") exceptException as msg:Print("The exception occurred when starting the browser:%s"%str (msg))
Multithreading launches different browsers
1. Code reference:
#Coding:utf-8 fromSeleniumImportWebdriverImport Time fromTomorrowImportThreadsdefStartbrowser (name):"""Open Browser functions, "Firefox", "Chrome", "ie", "PHANTOMJS"""" Try: ifName = ="Firefox" orName = ="Firefox" orName = ="FF": Print("Start Browser Name:firefox") Driver=Webdriver. Firefox ()returnDriverelifName = ="Chrome" orName = ="Chrome": Print("Start Browser Name:chrome") Driver=Webdriver. Chrome ()returnDriverelifName = ="IE" orName = ="Ie": Print("Start Browser Name:ie") Driver=Webdriver. Ie ()returnDriverelifName = ="Phantomjs" orName = ="Phantomjs": Print("Start Browser name:p Hantomjs") Driver=Webdriver. PHANTOMJS ()returnDriverElse: Print("Not found this browser,you can use ' Firefox ', ' Chrome ', ' ie ' or ' phantomjs '") exceptException as msg:Print("The exception occurred when starting the browser:%s"%str (msg)) @threads (5)defrun_case (name): Driver=startbrowser (name) driver.get ("https://www.cnblogs.com/yoyoketang/") Time.sleep (3) Print(Driver.title) driver.quit ()if __name__=="__main__": Names= ["Chrome","FF","IE"] forIinchnames:run_case (i)
2. Operation Result:
Tags: selenium
Selenium+python Automation 92-Multithreading launches multiple different browsers