SELENIUM2 Python Automated Test Study notes (vii)

Source: Internet
Author: User
Tags selenium grid

Selenium Grid2

The grid allows you to establish the primary node (hub) and branch node (nodes) on different hosts. The test cases on the master node are run on different branch nodes, so that a different environment can be set up to allow a test case to complete validation in different environments. The Selenium Grid2 has been integrated into Selenium server (Selenium-server-stanalon-xxx.jar package).

9.1 Selenium2 Working principle

Selenium2 because of the use of Webdriver, this technology is not by JS driver, but directly invoke the browser's original eco-interface driven. So there is no homologous problem, there is no need to use RC to execute the local script (the downside is that not all browsers provide good driver support, but JavaScript is common to all browsers). So the local script is executed in Selenium2 by invoking the local browser interface directly via the local webdriver driver.

9.2Selenium Server Environment Configuration

The first step is to configure the Java environment: Java:http://www.java.com/zh_CN/download/manual.jsp Double-click the downloaded JDK to set the installation path. Here we choose the default installation in C:\Java\jdk1.7.0_45\

Directory. Set environment variables below: "My Computer" right-click menu---> Properties---> Advanced---> Environment variables---> System variables-NEW:

Variable name:java_home

Variable Value: C:\Java\jdk1.7.0_45\

Variable name: Calss_path

Variable value:.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;

Locate the PATH variable name, "edit" to add:

Variable name: PATH

Variable value:%java_home%\bin;%java_home%\jre\bin;

Verify that Java is successful at the Windows command prompt: Java–version

The second step, download run Selenium server::http://www.seleniumhq.org/download/ Find Selenium-server-standalone-xxx.jar in the list on the left of the page to download. Download complete can be placed in any location, directly from the command prompt to start selenium Server:

C:\selenium> Java-jarselenium-server-standalone-xxx.jar

Executing the se_rc.py script, Selenium server will act as a proxy, executing client and server-side requests and return information.

se_rc.py

#coding =utf-8

From selenium import Selenium

Import UnitTest, time, re

Classserc (unittest. TestCase):

def setUp (self):

Self.verificationerrors = []

Self.selenium = Selenium ("localhost", 4444, "*chrome", "http://www.baidu.com/")

Self.selenium.start ()

def TEST_SERC (self):

sel = Self.selenium

Sel.open ("/")

Sel.type ("id=kw", "Selenium Grid")

Sel.click ("Id=su")

Sel.wait_for_page_to_load ("30000")

def tearDown (self):

Self.selenium.stop ()

Self.assertequal ([],self.verificationerrors)

if __name__ = = "__main__":

Unittest.main ()


9.3 Seleniumgrid Working principle


Selenium Grid is a tool for distributed testing, and its overall structure is composed of a hub node and several node broker nodes. The hub is used to manage the registration and collision information of each agent node, and to accept requests from the remote customer service, and then forward the requested command to each agent node for execution. The code to execute the tests remotely using the selenium grid is the same as calling Selenium-server directly (except that the environment starts differently and requires a hub and at least one node):

> Java-jarselenium-server-standalone-x.xx.x.jar-role Hub

> Java-jarselenium-server-standalone-x.xx.x.jar-role Node

Above is to start a hub and a node,hub default port number of 4444,node default port number is 5555; If the same machine is to start multiple node, pay attention to the port allocation problem, you can start multiple node:

> Java-jar selenium-server-standalone-x.xx.x.jar-role node-port 5555

> Java-jarselenium-server-standalone-x.xx.x.jar-role node-port 5556

> Java-jarselenium-server-standalone-x.xx.x.jar-role node-port 5557

The basic structure of calling the selenium Grid is as follows:

The above is a common way to use selenium Grid, using only its supported distributed execution functions, that is, when you need more test cases at the same time, you can run these use cases in parallel to shorten the overall test time; about concurrent technology requires multi-threading techniques in programming languages, We'll learn more about Python's multithreading techniques in the sections that follow. In addition to this, Selenium Grid supports a more user-friendly function that forwards the use case to a test agent that matches the matching requirements based on the type of test that is launched in your use case. For example, if your use case specifies that you want to test the 17 version of Firefox on Liunux, the selenium Grid automatically matches the proxy node that has the FireFox17 installed with the registration information, and if the match succeeds, forwards the test request and, if it fails, rejects the request. The code for the remote compatibility test using Seleniumgrid is the same as above. The basic structure of its invocation is shown below:


Here's a demonstration of starting a hub master node and two node branch nodes.


Access Gird's console through a browser:http://127.0.0.1:4444/grid/console

To view the starting section information from the console:


Node (5555) configuration information:

port:5555

Servlets:[]

host:192.168.5.57

cleanupcycle:5000

browsertimeout:0

hubhost:192.168.5.57

registercycle:5000

CapabilityMatcher:org.openqa.grid.internal.utils.DefaultCapabilityMatcher

Newsessionwaittimeout:-1

url:http://192.168.5.57:5555

remotehost:http://192.168.5.57:5555

Prioritizer:null

Register:true

Throwoncapabilitynotpresent:true

nodepolling:5000

Proxy:org.openqa.grid.selenium.proxy.DefaultRemoteProxy

Maxsession:5

Role:node

Jettymaxthreads:-1

hubport:4444

timeout:300000

9.4selenium Grid Application

Previously written scripts can only be executed on a fixed browser, since it is necessary to specify a browser driver (Webdriver) before writing the test case. Firefox, Webdriver. Chrome), once you have determined that the browser driver cannot be changed. We hope that the written test cases can be freely switched to different browsers to execute.



9.4.1 Know remote


Webdriver provides remote server control browser that can send commands to the terminal. First, let's get to know the format of remote. remote_ts.py:

From Selenium.webdriver import Remote

Fromselenium.webdriver.common.desired_capabilities Import Desiredcapabilities

Driver = Remote (command_executor= ' Http://127.0.0.1:4444/wd/hub ',

Desired_capabilities=desiredcapabilities.chrome

)

Driver.get (' http://www.baidu.com ')

driver.find_element_by_id ("kw"). Send_keys ("Selenium")

driver.find_element_by_id ("su"). Click ()

Driver.quit ()

Command_executor is a command executor that specifies the host and port that the script executes.

>>> from selenium.webdriver.common.desired_capabilities import desiredcapabilities

>>> A=desiredcapabilities.chrome

>>> Print a

{' Platform ': ' Any ', ' browsername ': ' Chrome ', ' Version ': ', ' javascriptenabled ':

True}

Desired capabilities is essentially an KeyValue object that tells the selenium Server script to perform the basic operating environment:

' platform ': ' any ' platform default can be any (windows,mac,android).

' browsername ': The ' Chrome ' browser name is Chrome.

' version ': The version of browser is empty by default.

' javascriptenabled ': True JavaScript startup status is True

remote_ts.py:

From Selenium.webdriver import Remote

Fromselenium.webdriver.common.desired_capabilities Import Desiredcapabilities

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_id ("kw"). Send_keys ("Selenium")

driver.find_element_by_id ("su"). Click ()

Driver.quit ()

The Webdriver API provides the parameters for different platforms and browsers:

android ={' platform ': ' Android ', ' browsername ': ' Android ', ' Version ': ',

' Javascriptenabled ': True}

Chrome ={' platform ': ' Any ', ' browsername ': ' Chrome ', ' Version ': ',

' Javascriptenabled ': True}

Firefox = {' Platform ': ' Any ', ' browsername ': ' Firefox ', ' Version ': ',

' Javascriptenabled ': True}

htmlunit ={' platform ': ' Any ', ' browsername ': ' Htmlunit ', ' Version ': '}

Htmlunitwithjs = {' Platform ': ' Any ', ' browsername ': ' Htmlunit ', ' Version ':

' Firefox ', ' javascriptenabled ': True}

InternetExplorer = {' Platform ': ' WINDOWS ', ' browsername ': ' Internet

Explorer ', ' Version ': ', ' javascriptenabled ': True}

ipad = {' Platform ': ' MAC ', ' browsername ': ' ipad ', ' Version ': ',

' Javascriptenabled ': True}

iphone = {' Platform ': ' MAC ', ' browsername ': ' iphone ', ' Version ': ', ' javascriptenabled ': True}

Safari = {' Platform ': ' Any ', ' browsername ': ' Safari ', ' Version ': ', ' javascriptenabled ': True}

Phantomjs = {' Platform ': ' Any ', ' browsername ': ' Phantomjs ', ' Version ': ',

' Javascriptenabled ': True}

Opera = {' Platform ': ' Any ', ' browsername ': ' Opera ', ' Version ': ', ' javascriptenabled ': True}


9.4.2 Parameterized Browser Execution cases


By decomposing the desired capabilities, the browser configuration is specified by the corresponding value of Browsername, so the parameterization of the browser will be very simple.

Before running the script we need to start selenium Server, because only the browser is parameterized, so start a node: > Java-jarselenium-server-standalone-2.39.0.jar

browsers.py

#coding =utf-8

Fromselenium.webdriver Import Remote

Fromselenium.webdriver.common.desired_capabilities Import Desiredcapabilities

lists=[' Chrome ', ' Firefox ', ' InternetExplorer '

For browser inlists:

Print browser

Driver = Remote (

Command_executor= ' Http://127.0.0.1:4444/wd/hub ',

desired_capabilities={' platform ': ' Any ',

' Browsername ': Browser,

' Version ': ',

' javascriptenabled ': True

})

Driver.get ("http://www.baidu.com")

driver.find_element_by_id ("kw"). Send_keys (browser)

driver.find_element_by_id ("su"). Click ()

Driver.close ()

Create a lists array, list different browsers, and use the For loop to read the browser in the array, and pass the parameters to run the test case as browsername as the currently executing browser.


9.4.3 Multi-node execution cases


In the section of the selenium Grid principle, we demonstrate how to start multiple nodes, and then start multiple nodes so that the same script executes on different nodes.

Open two command prompt windows on this machine to start a hub separately:

C:\selenium>java-jarselenium-server-standalone-2.39.0.jar-role Hub

Start two node (nodes):

C:\selenium>java-jar selenium-server-standalone-2.39.0.jar-role Node-port 5555

C:\selenium>java-jar selenium-server-standalone-2.39.0.jar-role Node-port 5556

The following changes the script to run on different nodes and browsers: host.py:

#coding =utf-8

Fromselenium.webdriver Import Remote

Fromselenium.webdriver.common.desired_capabilities Import Desiredcapabilities

lists={' Http://127.0.0.1:4444/wd/hub ': ' Chrome ',

' Http://127.0.0.1:5555/wd/hub ': ' Firefox ',

' Http://127.0.0.1:5556/wd/hub ': ' InternetExplorer '

}

For Host,browserin Lists.items ():

Print Host,browser

Driver = 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 (browser)

driver.find_element_by_id ("su"). Click ()

Driver.close ()

Create a lists dictionary, define different host port numbers and browsers, and use a for loop to read the dictionary host and browser to remote, so that the script executes when invoking different hosts and browsers.

Start Remote Node

The Hub and node we are currently launching are on a single host. To start node on another host, you must meet the following requirements:

L can ping each other between the local hub host and the remote node host.

The remote host must install the running environment (Python, Selenium, browser, and browser driver) that runs the script.

The remote host must have a Java environment installed because it needs to run Seleniumserver.

The procedure is as follows:

Start the local hub host ( Local host IP : 172.16.10.66):

C:\selenium>java-jar Selenium-server-standalone-2.39.0.jar-role Hub

Start the remote host ( operating system: Ubuntu , IP Address: 172.16.10.34):

The remote host starts node in the following way:

$ Java-jar selenium-server-standalone-2.39.0ar-role node-port 5555

-hub Http://172.16.10.66:4444/grid/register

(The port number set is: 5555, the hub IP point is 172.16.10.66)

Modify the IP address and port number of the remote host to run the script under Firefox browsing above it. host.py:

#coding =utf-8

Fromselenium.webdriver Import Remote

Fromselenium.webdriver.common.desired_capabilities Import Desiredcapabilities

lists={' Http://127.0.0.1:4444/wd/hub ': ' Chrome ',

'//172.16.10.34: 5555/wd/hub ': ' Firefox ',

' Http://127.0.0.1:5556/wd/hub ': ' InternetExplorer '

}

For Host,browserin Lists.items ():

Print Host,browser

Driver = 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 (browser)

driver.find_element_by_id ("su"). Click ()

Driver.close ()

Now run the script again and you will see the script executed on the 172.16.10.34 host.

Tips:

Since the use of selenium server to execute automation scripts, it is often caused by forgetting to start selenium server causing the script to run an error. We actually execute the cmd command in the script. As in the all_test.py file we created, add:

Import OS

Os.chdir (' E:\\selenium ')

Os.system (' Java-jar Selenium-server-standalone-2.39.0.jar ')

This will never cause the test script to fail because you forgot to start selenium Server.


9.5 Webdriver Drive


Drivers supported by the Webdriver:

SELENIUM2 Python Automated Test Study notes (vii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.