Selenium2+python automation-gird distributed (reproduced)

This article is transferred from the blog: Shanghai - Little T

Original address: http://blog.csdn.net/real_tino/article/details/53467406

Selenium grid is a tool for distributed execution of test case scripts. For example, testers often need to test the compatibility of multiple browsers, then grid can be used. Here's how to run the same script on multiple browsers.

Files required to use grid: 1. Selenium server (ie selenium-server-standalone-x.xx.x.jar); 2. grid configuration file (this file is responsible for providing host and browser information); 3. Test script.

1. Let's first take a look at the content of the grid configuration file:

def grid():
    d={'http://127.0.0.1:4444/wd/hub' : 'firefox',
        'http://127.0.0.1:5555/wd/hub' : 'internet explorer',
        }
    return d

This file defines a method, which stores a dictionary, assigns 2 different ports to the local machine and specifies different browsers (4444 is the default port of the grid hub, 5555 is the port of a node, and later will be introduced).

2. Let's look at the test script again:

# encoding:utf-8
 
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time, os.path
import grid_module
 
for  host, browser in grid_module.grid().items():
    driver = webdriver.Remote(
        command_executor=host,
        desired_capabilities={
            'platform': 'ANY',
            'browserName': browser,
            'version': '',
            'javascriptEnabled': True
        }
    )
    driver.get("http://www.baidu.com")
    driver.find_element_by_id("kw").send_keys(u"中国")
    driver.find_element_by_id("su").click()
    time.sleep(3)
    if  driver.title == u"China_Baidu Search":
        print("title match!")
    else:
        print("title mismatch!")
    driver.close()
 

This script is a script that writes a Baidu search keyword and makes a simple assertion. The imported grid_module is the grid configuration file in the first step. The loop body writes the host name and browser name from the dictionary and assigns it to the following parameters, so that the test script will successively call and run the two specified local browsers.

3. Then start the server and download the corresponding version of the Selenium server  from http://selenium-release.storage.googleapis.com/index.html :

 

After downloading, open cmd and enter Java -jar selenium-server-standalone-x.xx.x.jar -role hub, this is the root of all evil, you can do the following things first by enabling it. After startup, open a cmd and enter java -jar selenium-server-standalone-x.xx.x.jar -role node -port 5555, this is the first node to start, and specify port 5555, which is consistent with the port written in the grid configuration file.

After the hub and node are started, we enter http://127.0.0.1:4444/grid/console in the browser to open the grid console: 

we can see that a node with port 5555 has been started, here The IP is the local IP.

4. Finally, we run the test script, and the two browsers start up obediently~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325453333&siteId=291194637