Testng can be set as a concurrent execution test case. Selenium grid can forward test cases to different remote control/browser pairs through grid hub, and these remote control/browser pairs can be located on different machines, in this way, the two can be combined to implement scalable automatic web testing.
1. testng concurrent execution of Test Cases
In test. xml where testng is configured, concurrent execution can be specified through the suit tag attribute. For example:
<Suite name = "My suite" parallel = "methods" thread-count = "5">
Specifies that each test method uses a separate thread. The total number of threads is 5.
Parallel can be:
Methods: each method uses a thread.
Tests: each method in the <Test> label uses one thread.
Classes: each class uses a thread.
See here: http://testng.org/doc/documentation-main.html#parallel-running
If you use ant to start the test, you can also specify this parameter in the ant script. You can refer to the code of build. XML in selenium grid:
XML Code
- <Target name = "Run-demo-for-multiple-environments"
- Description = "Run Selenium tests in parallel for multiple environments">
- <Java classpathref = "demo. classpath" classname = "org. testng. testng" failonerror = "true">
- Omitted]
- <Arg value = "-d"/>
- <Arg value = "$ {basedir}/target/reports"/>
- <Arg value = "-suitename"/>
- <Arg value = "Selenium grid demo in parallel for multiple environments"/>
- [Here]
- <Arg value = "-parallel"/>
- <Arg value = "methods"/>
- <Arg value = "-threadcount"/>
- <Arg value = "10"/>
- <Arg value = "-testclass"/>
- <Arg value = "com. thoughtworks. Selenium. Grid. Demo. webtestinvolvingmultienvironments"/>
- </Java>
- </Target>
<Target name = "Run-demo-for-multiple-environments" Description = "Run Selenium tests in parallel for multiple environments"> <Java classpathref = "demo. classpath "classname =" org. testng. testng "failonerror =" true "> [omitted] <Arg value ="-d "/> <Arg value =" $ {basedir}/target/reports "/> <Arg Value = "-suitename"/> <Arg value = "Selenium grid demo in parallel for multiple environments"/> [here] <Arg value = "-parallel"/> <Arg Value = "methods"/> <Arg value = "-threadcount"/> <Arg value = "10"/> <Arg value = "-testclass"/> <Arg value =" com. thoughtworks. selenium. grid. demo. webtestinvolvingmultienvironments "/> </Java> </Target>
2 Use selenium Grid
The mechanism of grid is to start a hub, Start Multiple Remote Control, and notify the hub location when the remote control is started, so that these RC can be registered on the hub, the test program communicates with the hub. when the test is concurrently sent to the hub, the hub automatically distributes the test commands to the registered RC. After the RC receives the command, it performs the test.
Here D:/opensource/selenium-grid-1.0.4/doc/website/download.html the latest version of selenium grid, which contains detailed usage instructions, the statement is as follows:
Selenium grid requires JDK and ant installation. The build. xml file is in the directory, and the main target defined in the file is as follows:
Check Configuration: ant sanity-Check
Start hub: ant launch-Hub
The Hub configuration is in the grid_configuration.yml file, which must be in the root of classpath.
After startup, you can view the current hub status at http: // host: Port/console.
Start RC (by default): ant run-demo-in-Sequence
Start RC (specified parameter): ant-dport = 5555-dhost = 192.168.1.16-dhuburl = http: // 192.168.1.1: 4444 launch-remote-control
The host and port are the RC addresses, and the huburl is the hub address.
After normal startup, you can view the currently registered RC in the hub at http: // host: Port/console.
3. Writing Test code
The test code is in grid tool.
Threadsafeseleniumsessionstorage instantiates a selenium (actually defaultselenium) before each use case, and closes it after the use case. The initialization of selenium is the same as the general initialization of defaultselenium. Specify host, port, browser, and indexpage. The host and port here are not the server in RC, but the hub.
The following is the sample code:
Java code
- Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. closeseleniumsession;
- Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. Session;
- Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. startseleniumsession;
- Import org. testng. Annotations. aftermethod;
- Import org. testng. Annotations. beforemethod;
- Import org. testng. Annotations. parameters;
- Import org. testng. Annotations. test;
- Import com. thoughtworks. Selenium. selenium;
- /**
- * Tests for the footer of a page. This footer always contains a Copyright string and
- * Timestamp for the time the target was generated.
- * @ Author pmorales
- */
- Public class sample {
- Private selenium;
- @ Beforemethod
- @ Parameters ({"seleniumhost", "seleniumport", "Browser", "website "})
- Protected void startsession (string seleniumhost, int seleniumport, string browser, string website) throws exception {
- Startseleniumsession (seleniumhost, seleniumport, browser, website );
- Selenium = SESSION ();
- Selenium. setTimeout ("120000 ");
- }
- @ Aftermethod
- Protected void closesession () throws exception {
- Closeseleniumsession ();
- }
- @ Test
- Public void test1 (){
- Selenium. Open ("/");
- Selenium. Type ("Q", "test1 ");
- Selenium. Click ("btng ");
- Selenium. waitforpagetoload ("30000 ");
- }
- [Omitted]
- }
Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. closeseleniumsession;
Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. Session;
Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. startseleniumsession;
Import org. testng. Annotations. aftermethod;
Import org. testng. Annotations. beforemethod;
Import org. testng. Annotations. parameters;
Import org. testng. Annotations. test;
Import com. thoughtworks. Selenium. selenium;
/**
* Tests for the footer of a page. This footer always contains a Copyright string and
* Timestamp for the time the target was generated.
* @ Author pmorales
*/
Public class sample {
Private selenium;
@ Beforemethod
@ Parameters ({"seleniumhost", "seleniumport", "Browser", "website "})
Protected void startsession (string seleniumhost, int seleniumport, string browser, string website) throws exception {
Startseleniumsession (seleniumhost, seleniumport, browser, website );
Selenium = SESSION ();
Selenium. setTimeout ("120000 ");
}
@ Aftermethod
Protected void closesession () throws exception {
Closeseleniumsession ();
}
@ Test
Public void test1 (){
Selenium. Open ("/");
Selenium. Type ("Q", "test1 ");
Selenium. Click ("btng ");
Selenium. waitforpagetoload ("30000 ");
}
[Omitted]
}
In the testng configuration file test. seleniumhost, seleniumport, browser, website, and other parameters are specified in XML. When multiple test cases and multiple RC instances run the test, the test runs concurrently on multiple RC instances.