午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

selenium+python自動化91-多線程啟動多個不同瀏覽器

 上海悠悠 2021-05-27

一、前言

    如果想用多個瀏覽器跑同一套測試代碼,driver=webdriver.Firefox()這里的driver就不能寫死了,可以把瀏覽器名稱參數(shù)化。

    后續(xù)如果想實現(xiàn)多線程同時啟動瀏覽器執(zhí)行用例,用前面講的tomorrow模塊,設(shè)置下線程數(shù)套用下就可以了

二、啟動瀏覽器

1.為了實現(xiàn)多個瀏覽器的靈活切換,可以把啟動瀏覽器寫一個函數(shù),參數(shù)用瀏覽器名稱就行了

```

def startBrowser(name):

    """

    打開瀏覽器函數(shù),name參數(shù):"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("啟動瀏覽器出現(xiàn)異常:%s" % str(msg))

```

三、 多線程啟動不同瀏覽器

1.代碼參考:

```

# coding:utf-8

from selenium import webdriver

import time

from tomorrow import threads

def startBrowser(name):

    """

    打開瀏覽器函數(shù),"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("啟動瀏覽器出現(xiàn)異常:%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.運行結(jié)果:

    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多