selenium python環(huán)境下的部署以及selenium grid的部署

selenium python環(huán)境下的部署以及selenium grid的部署

安裝教程

selenium grid

一、Selenium Grid解決什么問題

可以分布式運行自動化測試用例,提高執(zhí)行效率和解決兼容性測試的要求。二、運行機制selenium Grid使用Hub和Node模式,一臺計算機作為Hub(管理中心)管理其他多個Node(節(jié)點)計算機。Hub負責(zé)將測試用例分發(fā)給多臺Node計算機執(zhí)行,并收集多臺Node計算機執(zhí)行結(jié)果的報告,匯總后提交一份總的測試報告。

有兩種角色hubnode
Hub:在分布式測試模式中,只能有一臺作為Hub的計算機。Hub負責(zé)管理測試腳本, 并負責(zé)發(fā)送腳本給其他Node節(jié)點。所有的Node節(jié)點計算機會在Hub計算機中先進行注冊,注冊成功后再和Hub計算機通信,Node節(jié)點計算機會告之Hub自己的相關(guān)信息。例如,Node節(jié)點的瀏覽器相關(guān)信息、最多并發(fā)數(shù)等。Hub計算機可以給自己分配執(zhí)行測試用例的任務(wù)。Hub計算機分發(fā)的測試用例任務(wù)會在各個Node節(jié)點計算機執(zhí)行。
Node:在分布式測試模式中,可以有一個或者多個Node節(jié)點。Node節(jié)點會打開本地的瀏覽器完成測試任務(wù)并返回測試結(jié)果給Hub。Node節(jié)點的操作系統(tǒng)和瀏覽器版本無需和Hub保持一致。在Node節(jié)點上可以同時打開多個瀏覽器并行執(zhí)行測試任務(wù)

java -jar selenium-server-standalone-   2.53.1.jar -role hub

或者

 java -jar selenium-server-standalone-  2.53.1.jar -role hub -hubConfig hub.json

role參數(shù):hub表示作為管理中心
port參數(shù):hub服務(wù)器的端口號
hubConfig:hub各種配置的json文件路徑。

運行后,會出現(xiàn)

2019-05-06 10:28:44.370:INFO::main: Logging initialized @670ms
10:28:44.380 INFO - Will listen on 4444
10:28:44.412 INFO - Will listen on 4444
2019-05-06 10:28:44.414:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT
2019-05-06 10:28:44.434:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@6dde5c8c{/,null,AVAILABLE}
2019-05-06 10:28:44.450:INFO:osjs.ServerConnector:main: Started ServerConnector@7dc222ae{HTTP/1.1}{0.0.0.0:4444}
2019-05-06 10:28:44.450:INFO:osjs.Server:main: Started @751ms
10:28:44.450 INFO - Nodes should register to http://10.32.59.169:4444/grid/register/
10:28:44.450 INFO - Selenium Grid hub is up and running

其中,10:28:44.450為本機的ip地址,在瀏覽器中打開網(wǎng)址:
http://10.32.59.169:4444/grid/console/
可以看到Grid Console vxxx.xx.x以及下面的超鏈接 view config,但此時并沒有任何節(jié)點信息,至此,hub端的配置已經(jīng)完成。

  • Node端的配置
    在計算機B中運行
java -jar selenium-server-standalone-   2.53.1.jar -role webdriver -nodeConfig node.json

role參數(shù):webdriver表示Node節(jié)點的名字
nodeConfig :node各種配置的json文件路徑

運行后,有如下cosole信息:


11:04:49.446 INFO - Launching a Selenium Grid node
11:04:49.773 INFO - Java: Oracle Corporation 25.211-b12
11:04:49.773 INFO - OS: Mac OS X 10.14.4 x86_64
11:04:49.776 INFO - v2.51.0, with Core v2.51.0. Built from revision 1af067d
11:04:49.804 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC
11:04:49.804 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC
11:04:49.804 INFO - Driver class not found: com.opera.core.systems.OperaDriver
11:04:49.804 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
11:04:49.834 INFO - Selenium Grid node is up and ready to register to the hub
11:04:49.854 INFO - Starting auto registration thread. Will try to register every 5000 ms.
11:04:49.855 INFO - Registering the node to the hub: http://10.32.59.169:4444/grid/register
11:04:49.869 INFO - The node is registered to the hub and ready to use


這是后再次訪問
http://10.32.59.169:4444/grid/console/
可以看到頁面下多了兩個tab,Browsers已經(jīng)Configuration??梢钥吹絼偛盼覀兣渲玫墓?jié)點信息。

編寫分布式腳本

import unittest
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.keys import Keys


class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        # 使用本地webdrvier
        # self.driver = webdriver.Firefox()

        # 遠程服務(wù)器的webdriver
        self.driver = webdriver.Remote(
            command_executor='http://10.32.59.169:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)
        # 使用selenium grid執(zhí)行測試

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_id("id-search-field")
        elem.send_keys("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def test_get_cookis(self):
        driver = self.driver
        driver.get("http://www.python.org")
        driver.add_cookie({'name': 'forTest', 'value': 'dddd'})
        print(driver.get_cookies())

    def tearDown(self):
        # self.driver.close()
        self.driver.quit()


if __name__ == "__main__":
    unittest.main()


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容