Combined with selenium grid and testng for concurrent execution of Automatic Web Testing

Source: Internet
Author: User
Tags testng selenium grid

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
  1. <Target name = "Run-demo-for-multiple-environments"
  2. Description = "Run Selenium tests in parallel for multiple environments">
  3. <Java classpathref = "demo. classpath" classname = "org. testng. testng" failonerror = "true">
  4. Omitted]
  5. <Arg value = "-d"/>
  6. <Arg value = "$ {basedir}/target/reports"/>
  7. <Arg value = "-suitename"/>
  8. <Arg value = "Selenium grid demo in parallel for multiple environments"/>
  9. [Here]
  10. <Arg value = "-parallel"/>
  11. <Arg value = "methods"/>
  12. <Arg value = "-threadcount"/>
  13. <Arg value = "10"/>
  14. <Arg value = "-testclass"/>
  15. <Arg value = "com. thoughtworks. Selenium. Grid. Demo. webtestinvolvingmultienvironments"/>
  16. </Java>
  17. </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
  1. Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. closeseleniumsession;
  2. Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. Session;
  3. Import static com. thoughtworks. Selenium. Grid. Tools. threadsafeseleniumsessionstorage. startseleniumsession;
  4. Import org. testng. Annotations. aftermethod;
  5. Import org. testng. Annotations. beforemethod;
  6. Import org. testng. Annotations. parameters;
  7. Import org. testng. Annotations. test;
  8. Import com. thoughtworks. Selenium. selenium;
  9. /**
  10. * Tests for the footer of a page. This footer always contains a Copyright string and
  11. * Timestamp for the time the target was generated.
  12. * @ Author pmorales
  13. */
  14. Public class sample {
  15. Private selenium;
  16. @ Beforemethod
  17. @ Parameters ({"seleniumhost", "seleniumport", "Browser", "website "})
  18. Protected void startsession (string seleniumhost, int seleniumport, string browser, string website) throws exception {
  19. Startseleniumsession (seleniumhost, seleniumport, browser, website );
  20. Selenium = SESSION ();
  21. Selenium. setTimeout ("120000 ");
  22. }
  23. @ Aftermethod
  24. Protected void closesession () throws exception {
  25. Closeseleniumsession ();
  26. }
  27. @ Test
  28. Public void test1 (){
  29. Selenium. Open ("/");
  30. Selenium. Type ("Q", "test1 ");
  31. Selenium. Click ("btng ");
  32. Selenium. waitforpagetoload ("30000 ");
  33. }
  34. [Omitted]
  35. }
  36. 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.

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.