grid是进行分布式测试的工具,由一个hub主节点和若干个node代理节点组成 1.下载Selenium Standalone Server
下载地址:https://www.seleniumhq.org/download/
2.启动hub和节点 #hub默认端口是4444,node默认端口是5555 java -jar selenium-server-standalone-3.141.59.jar -role hub #本机注册节点 java -jar selenium-server-standalone-3.141.59.jar -role node #本机指定端口注册节点 java -jar selenium-server-standalone-3.141.59.jar -role node -port 5556 #远程机器注册节点 java -jar selenium-server-standalone-3.141.59.jar -role node -port 5557 -hub http://192.168.63.143:4444/grid/register
节点注册地址: http://192.168.63.143:4444/grid/register/ hub管理地址: http://192.168.63.143:4444/grid/console 3.查看后台http://192.168.63.143:4444/grid/console
4.实例1:编写自动化测试脚本 from selenium.webdriver import Remote driver = Remote(command_executor="http://127.0.0.1:4444/wd/hub", desired_capabilities={'platform':'ANY', 'browserName':'chrome', 'version':'', 'javascriptEnabled':True}) driver.get("http://www.baidu.com") driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys("hello") driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click() driver.quit()
修改脚本以在不同的浏览器上运行 from selenium.webdriver import Remote from time import sleep lists={'http://192.168.63.143:4444/wd/hub':'chrome', 'http://192.168.63.143:5555/wd/hub':'firefox'} for host,browser in lists.items(): print(host,browser) driver = Remote(command_executor=host, desired_capabilities={'browserName': browser, 'platform': 'ANY', 'version':'', 'javascriptEnabled':True } ) driver.get("http://www.baidu.com") 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() sleep(3) result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text assert "百度为您找到相关结果约" in result_text driver.quit()
5.浏览器内核 浏览器最重要或者最核心的部分是Rendering Engine,即渲染引擎,习惯成为渲染引擎。所谓的浏览器内核,决定了浏览器如何显示网页的内容以及页面的格式信息。