selenium python环境下的部署以及selenium grid的部署
安装教程
selenium的安装
https://selenium-python-zh.readthedocs.io/en/latest/installation.htmlwebdriver的安装
selenium3 以后需要装webdriver
方法:
driver下载地址:
https://www.cnblogs.com/nancyzhu/p/8589764.html
mac的安装方法:
https://www.jianshu.com/p/9015db3fcd80
selenium grid
一、Selenium Grid解决什么问题
可以分布式运行自动化测试用例,提高执行效率和解决兼容性测试的要求。二、运行机制selenium Grid使用Hub和Node模式,一台计算机作为Hub(管理中心)管理其他多个Node(节点)计算机。Hub负责将测试用例分发给多台Node计算机执行,并收集多台Node计算机执行结果的报告,汇总后提交一份总的测试报告。
有两种角色hub 和 node
Hub:在分布式测试模式中,只能有一台作为Hub的计算机。Hub负责管理测试脚本, 并负责发送脚本给其他Node节点。所有的Node节点计算机会在Hub计算机中先进行注册,注册成功后再和Hub计算机通信,Node节点计算机会告之Hub自己的相关信息。例如,Node节点的浏览器相关信息、最多并发数等。Hub计算机可以给自己分配执行测试用例的任务。Hub计算机分发的测试用例任务会在各个Node节点计算机执行。
Node:在分布式测试模式中,可以有一个或者多个Node节点。Node节点会打开本地的浏览器完成测试任务并返回测试结果给Hub。Node节点的操作系统和浏览器版本无需和Hub保持一致。在Node节点上可以同时打开多个浏览器并行执行测试任务
- Hub端(计算机A)的配置
打开http://selenium-release.storage.googleapis.com/index.html?path=2.51/地址,下载selenium-server-standalone- 2.53.1.jar,
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参数:hub表示作为管理中心
port参数:hub服务器的端口号
hubConfig:hub各种配置的json文件路径。
运行后,会出现
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地址,在浏览器中打开网址:
http://10.32.59.169:4444/grid/console/
可以看到Grid Console vxxx.xx.x以及下面的超链接 view config,但此时并没有任何节点信息,至此,hub端的配置已经完成。
- Node端的配置
在计算机B中运行
java -jar selenium-server-standalone- 2.53.1.jar -role webdriver -nodeConfig node.json
role参数:webdriver表示Node节点的名字
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已经Configuration。可以看到刚才我们配置的节点信息。
编写分布式脚本
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()
# 远程服务器的webdriver
self.driver = webdriver.Remote(
command_executor='http://10.32.59.169:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)
# 使用selenium grid执行测试
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()