selenium+python自动化92-多线程启动多个不同浏览器

Posted jason89

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium+python自动化92-多线程启动多个不同浏览器相关的知识,希望对你有一定的参考价值。

前言

如果想用多个浏览器跑同一套测试代码,driver=webdriver.Firefox()这里的driver就不能写死了,可以把浏览器名称参数化。
后续如果想实现多线程同时启动浏览器执行用例,用前面讲的tomorrow模块,设置下线程数套用下就可以了

启动浏览器

1.为了实现多个浏览器的灵活切换,可以把启动浏览器写一个函数,参数用浏览器名称就行了

def startBrowser(name):
    """
    打开浏览器函数,"firefox"、"chrome"、"ie"、"phantomjs"
    """
    try:
        if name == "firefox" or name == "Firefox" or name == "ff":
            print("start browser name :Firefox")
            driver = webdriver.Firefox()
            return driver
        elif name == "chrome" or name == "Chrome":
            print("start browser name :Chrome")
            driver = webdriver.Chrome()
            return driver
        elif name == "ie" or name == "Ie":
            print("start browser name :Ie")
            driver = webdriver.Ie()
            return driver
        elif name == "phantomjs" or name == "Phantomjs":
            print("start browser name :phantomjs")
            driver = webdriver.PhantomJS()
            return driver
        else:
            print("Not found this browser,You can use ‘firefox‘, ‘chrome‘, ‘ie‘ or ‘phantomjs‘")
    except Exception as msg:
        print("启动浏览器出现异常:%s" % str(msg))
        

 

多线程启动不同浏览器

1.代码参考:

# coding:utf-8
from selenium import webdriver
import time
from tomorrow import threads

def startBrowser(name):
    """
    打开浏览器函数,"firefox"、"chrome"、"ie"、"phantomjs"
    """
    try:
        if name == "firefox" or name == "Firefox" or name == "ff":
            print("start browser name :Firefox")
            driver = webdriver.Firefox()
            return driver
        elif name == "chrome" or name == "Chrome":
            print("start browser name :Chrome")
            driver = webdriver.Chrome()
            return driver
        elif name == "ie" or name == "Ie":
            print("start browser name :Ie")
            driver = webdriver.Ie()
            return driver
        elif name == "phantomjs" or name == "Phantomjs":
            print("start browser name :phantomjs")
            driver = webdriver.PhantomJS()
            return driver
        else:
            print("Not found this browser,You can use ‘firefox‘, ‘chrome‘, ‘ie‘ or ‘phantomjs‘")
    except Exception as msg:
        print("启动浏览器出现异常:%s" % str(msg))

@threads(5)
def run_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"]
    for i in names:
        run_case(i)

 

2.运行结果:

技术分享图片

 
 
标签: selenium

以上是关于selenium+python自动化92-多线程启动多个不同浏览器的主要内容,如果未能解决你的问题,请参考以下文章

selenium+python自动化91-unittest多线程生成报告(BeautifulReport)

selenium+python自动化91-unittest多线程生成报告(BeautifulReport)

selenium+python自动化90-unittest多线程执行用例

Python多线程Selenium跨浏览器测试

Selenium3+python自动化016-多线程

Python多线程Selenium跨浏览器测试