Learn some Selenium and play something new ~, let distributed testing have more ways to play

 Foreword

We all know that Selenium is an automated testing tool used in the field of Web application testing, and Selenium Grid is a major component of Selenium, through which distributed testing can be realized, which can help teams to test their in different environments quickly and easily. web application.

Distributed execution testing is actually not a very difficult concept to understand. Simply put, it is the ability to execute tests in different environments at the same time. Through Selenium Grid, we can execute the WebDriver script on the remote end by sending commands from the client to the browser instance on the remote end, and run tests in parallel on multiple remote machines.

Introduction to new features of Selenium Grid 4

In previous versions of Selenium Grid (V1-V3), its architecture was relatively simple. In the new Selenium Grid 4 version its architecture has become a bit more complex, but also more flexible and powerful.

Feature 1: Hub and Node use the same jar service

In the past, when we wanted to configure Selenium Grid, we needed to start Hub and Node nodes separately. In the new Selenium Grid 4 version, both Hub and Node use the same jar file. Once the service is started, it will act as both Hub and Node.

Feature 2: Architecture Optimization

In previous versions of Selenium, Selenium Grid consisted of Hub and Node. Hub, as the master control center, was responsible for Node node registration and forwarding requests, and Node node was responsible for requests from Hub and execution of test scripts. For details, refer to my previous article Selenium Grid- Make automated distributed execution possible

Article link: http://testingpai.com/article/1596527701728

picture

If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the first interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and various Lu Dashen Technical Exchange: 798478386      

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. [Interface Automation] The current market situation of software testing and the ability standards of testers. , 2. [Interface Automation] Fully skilled in the Requests library and the underlying method call logic, 3. [Interface Automation] interface automation combat and the application of regular expressions and JsonPath extractors, etc. For more exciting videos, please pay attention to the UP account. https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337&vd_source=488d25e59e6c5b111f7a1a1a16ecbe9a  is divided into components in the new architecture of Selenium Grid 4 version: Router, Distributor, Node, Session Map, Session Queue 、Event Bus

  • Router - Listen for new session requests

  • Distributor - choose the appropriate Node to execute the test

  • Node - Execute tests on the corresponding host

  • Session Map - mark the session ID for the Node node

  • Session Queue - maintains all session queues

  • Event Bus - as a communication bridge between Grid components

picture

 

picture

Feature 3: Different Grid operation modes

In Selenium Grid 3 and previous versions, only the Hub and Node working modes can be used. In addition to providing support for the classic Hub and Node modes, Selenium Grid 4 also introduces two other new modes: standalone and fully distributed.

  • Standalone mode

The standalone mode can execute complete distributed functions on one machine, and is the simplest mode of Selenium Grid. By default, the service will listen at http://localhost:4444 address, and we need to point to this address through RemoteWebDriver.

  • Classical mode

Classical mode can also be called hub and node mode. This mode is more suitable for small and medium-sized distributed execution requirements. Set up a service node (hub) and multiple proxy nodes (node) as needed.

  • Fully Distributed mode

For large-scale distributed requirements, you can use the fully distributed mode, which is the most advanced mode in Selenium Grid 4. In this mode, each component needs to be started by itself.

Standalone mode use

In standalone mode, both Node and Hub run in the same Selenium Grid service

Step1: Prepare the required environment

Because the downloaded Selenium components are jar files, it is necessary to prepare the Java environment to run them. If you do not have a Java environment installed, you can go to https://www.oracle.com/java/technologies/downloads/ to download and install JDK and configure environment variables. After the configuration is complete, enter the command line terminal and enter the following command to detect:

java -version

If the Java environment configuration is successful, you will see the relevant Java version information.

Step2: Download the browser driver

In the next step, you need to choose to download the appropriate driver according to your current operating system and browser.

  • Chrome Driver Address: https://npm.taobao.org/mirrors/chromedriver

  • Firefox Driver Address: https://npm.taobao.org/mirrors/geckodriver

After the driver is downloaded, place it in a certain directory, and add the directory to the environment variable of the system. It will be automatically recognized when the Selenium Grid service starts. Without doing this Selenium Grid will not be able to perform tests on these browsers.

picture

Step3: Create Selenium Grid

In this step, we need to download the corresponding address of Selenium Server: https://www.selenium.dev/downloads/

After the download is complete, place it in any directory, and then run the following command on the command line terminal to start Selenium Grid

java -jar selenium-server-4.1.1.jar standalone

 You will see the following information output, we can see that both Chrome and Firefox are registered in the Grid

D:\Selenium Grid>java -jar selenium-server-4.1.1.jar standalone
16:59:31.406 INFO [LogManager$RootLogger.log] - Using the system default encoding
16:59:31.409 INFO [OpenTelemetryTracer.createTracer] - Using OpenTelemetry for tracing
16:59:37.876 INFO [NodeOptions.getSessionFactories] - Detected 8 available processors
16:59:38.046 INFO [NodeOptions.discoverDrivers] - Discovered 2 driver(s)
16:59:38.071 INFO [NodeOptions.report] - Adding Firefox for {"browserName": "firefox"} 8 times
16:59:38.072 INFO [NodeOptions.report] - Adding Chrome for {"browserName": "chrome"} 8 times

We can see the current status by visiting http://localhost:4444/status


{
  "value": {
    "ready": true,
    "message": "Selenium Grid ready.",
    "nodes": [
      {
        "id": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
        "uri": "http:\u002f\u002f192.168.56.1:4444",
        "maxSessions": 8,
        "osInfo": {
          "arch": "amd64",
          "name": "Windows 10",
          "version": "10.0"
        },
        "heartbeatPeriod": 60000,
        "availability": "UP",
        "version": "4.1.1 (revision e8fcc2cecf)",
        "slots": [
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "b8a3c42e-c8fb-493c-bb3c-7b6deaa2a02a"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "0eaffe11-62a1-4593-a94f-80151a69c24b"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "7c7211bf-58a7-4ddb-9246-effecbf910ce"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "437be3d5-bdf0-44aa-b401-51f7b9136b15"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "f799da78-4ac8-4799-8d4b-50314a954c01"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "10bd31b1-acf6-4006-af7c-47ea448a6a33"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "31c91763-4aef-4379-910c-647266806fce"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "20caece4-cb26-4914-8dfa-80fffd37191f"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "53a12fa7-63a4-474d-bab4-5e6fc6976ff1"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "7d1bb113-1050-482f-bd5d-939362270fdf"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "c4993662-f2f1-46e6-ab6c-6eae7727dd68"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "d9fcf1d0-cba0-4a53-bba4-934f35be8ccb"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "8b8bdd06-4c6f-4344-812a-3429b5d013c7"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "f7ca236a-7c1f-48ba-93e1-25745ed18493"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "chrome",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "3116789e-3fab-4bcc-89fe-9c0b788bb29f"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          },
          {
            "id": {
              "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",
              "id": "3abf1ce7-cd89-48ae-a33d-252abd9a29c5"
            },
            "lastStarted": "1970-01-01T00:00:00Z",
            "session": null,
            "stereotype": {
              "browserName": "firefox",
              "platformName": "WIN10"
            }
          }
        ]
      }
    ]
  }
}

 We can also view the Grid console by entering http://localhost:4444/grid/console on the browser side, and we can also see that in the standalone mode, there are corresponding Chrome browser and Firefox browser in the node node.

picture

Step4: Execute the test using Selenium Grid

In the above steps, we have configured the hub and node through the simplest standalone mode, and next we will execute our automated tests.

When writing test scripts, we only need to point to http://localhost:4444/ through RemoteWebDriver

@Test
public void testChrome() throws MalformedURLException {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(CapabilityType.BROWSER_NAME,"chrome");
    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), caps);
    driver.manage().window().maximize();
    driver.get("http://testingpai.com/");
}

@Test
public void testFirefox() throws MalformedURLException {
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(CapabilityType.BROWSER_NAME,"firefox");
    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), caps);
    driver.manage().window().maximize();
    driver.get("http://testingpai.com/");
}

 The above sample client script and Grid service are both on the local machine. Of course, in actual application, we can deploy the Grid service and the script on two hosts respectively. At this time, the address pointed to by the script is: http://localhost:4444/ Just replace it with the external IP address of the host where the Grid service is located.

Guess you like

Origin blog.csdn.net/m0_73409141/article/details/132261135