标签:single follow machines play type ring chrome central debug
Original URL: https://www.guru99.com/introduction-to-selenium-grid.html
Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in parallel.
Selenium Grid has 2 versions - the older Grid 1 and the newer Grid 2. We will only focus on Grid 2 because Grid 1 is gradually being deprecated by the Selenium Team.
Selenium Grid uses a hub-node concept where you only run the test on a single machine called a hub, but the execution will be done by different machines called nodes.
You should use Selenium Grid when you want to do either one or both of following:
Following are the main differences between Selenium Grid 1 and 2.
Grid 1 |
Grid 2 |
---|---|
Selenium Grid 1 has its own remote control that is different from the Selenium RC server. They are two different programs. |
Selenium Grid 2 is now bundled with the Selenium Server jar file |
You need to install and configure Apache Ant first before you can use Grid 1. |
You do not need to install Apache Ant in Grid 2. |
Can only support Selenium RC commands/scripts. |
Can support both Selenium RC and WebDriver scripts. |
You can only automate one browser per remote control. |
One remote control can automate up to 5 browsers. |
Selenium Grid has a Hub and Node Architecture.
Selenium grid can be set up in two different ways; one through command line and the other through JSON config file.
In this section, you will use 2 machines. The first machine will be the system that will run the hub while the other machine will run a node. For simplicity, let us call the machine where the hub runs as "Machine A" while the machine where the node runs will be "Machine B." It is also important to note their IP addresses. Let us say that Machine A has an IP address of 192.168.1.3 while Machine B has an IP of 192.168.1.4.
Step 1
Download the Selenium Server from here.
Step 2
You can place the Selenium Server .jar file anywhere in your HardDrive. But for the purpose of this tutorial, place it on the C drive of both Machine A and Machine B. After doing this, you are now done installing Selenium Grid. The following steps will launch the hub and the node.
Step 3
Step 4
Another way to verify whether the hub is running is by using a browser. Selenium Grid, by default, uses Machine A‘s port 4444 for its web interface. Simply open up a browser and go to http://localhost:4444/grid/console
Also, you can check if Machine B can access the hub‘s web interface by launching a browser there and going to where "iporhostnameofmachineA" should be the IP address or the hostname of the machine where the hub is running. Since Machine A‘s IP address is 192.168.1.3, then on the browser on Machine B you should type http://192.168.1.3:4444/grid/console
Step 5
java -Dwebdriver.gecko.driver="C:\geckodriver.exe" -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub http://192.168.1.3:4444/grid/register -port 5566
Step 6
Go to the Selenium Grid web interface and refresh the page. You should see something like this.
At this point, you have already configured a simple grid. You are now ready to run a test remotely on Machine B.
To design test scripts that will run on the grid, we need to use DesiredCapabilites and the RemoteWebDriver objects.
To use the DesiredCapabilites object, you must first import this package
To use the RemoteWebDriver object, you must import these packages.
Go to the Grid‘s web interface and hover on an image of the browser that you want to automate. Take note of the platform, and the browserName showed by the tooltip.
In this case, the platform is "XP" and the browserName is "Firefox."
We will use the platform and the browserName in our WebDriver as shown below (of course you need to import the necessary packages first).
Import the necessary packages for RemoteWebDriver and then pass the DesiredCapabilities object that we created above as a parameter for the RemoteWebDriver object.
Below is a simple WebDriver Testng code that you can create in Eclipse on Machine A. Once you run it, automation will be performed on Machine B.
The test should pass.
JSON stands for Javascript Object Notation. It is a standard format used for interchange of data between browser and the web server. Selenium has an in built JSON config file that can be used to set up selenium grid.
Below are the steps to configure selenium grid using JSON config file.
Step 1) Download the code for JSON config file using the below path https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/DefaultHub.json
Here is the code
{ "port": 4444, "newSessionWaitTimeout": -1, "servlets" : [], "withoutServlets": [], "custom": {}, "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher", "registryClass": "org.openqa.grid.internal.DefaultGridRegistry", "throwOnCapabilityNotPresent": true, "cleanUpCycle": 5000, "role": "hub", "debug": false, "browserTimeout": 0, "timeout": 1800 }
Step 2) Copy and paste the code on a text editor such as Notepad with the extension ‘.json‘
Step 3)Launch the hub using the below command using command prompt
java -jar selenium-server-standalone-2.53.1.jar -role hub -hubConfig hubconfig.json
NOTE: Selenium stand alone jar file and the json file must be present on the same directory
Step 4) The below screen will appear which indicates the hub is set up successfully
Step 5) Open web browser and try connecting to the below URL
http://192.168.43.223:4444/grid/console
NOTE: The URL may vary from machine to machine. URL followed by ‘Nodes should register to‘ on the above screen must be used.
Step 6) Grid Console screen will appear which indicates the hub is up and running
Step 1) Download the sample node configuration file provided by Selenium using the below URL https://github.com/SeleniumHQ/selenium/blob/selenium-2.53.0/java/server/src/org/openqa/grid/common/defaults/DefaultNode.json
NOTE: IF YOU ARE USING SELENIUM 3.0 OR ABOVE, THE BELOW JSON FILE MUST BE USED
Step 2) Copy and paste the code into a text editor such as Notepad and save the file with the extension ‘.json’
Step 3)
Run the below command on command prompt
Java-Dwebdriver.chrome.driver="chromedriver.exe" -Dwebdriver.ie.driver="IEDriverServer.exe" -Dwebdriver.gecko.driver="geckodriver.exe" -jar selenium-server-standalone-2.53.1.jar -role node -nodeConfig node1.json
NOTE: Before running the command, please make sure the drivers for each of the browsers have been downloaded onto local machine directory
Step 4) The below screen will appear when enter key is pressed
Step 5) Navigate to the grid console to verify if the nodes have been successfully configured
The browsers are displayed on the grid console. Hence we can be sure that nodes are configured successfully.
Sample Test Cases On Selenium Grid:
package com.objectrepository.demo; import org.openqa.selenium.*; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.Assert; import org.testng.annotations.*; public class SeleniumGrid { WebDriver driver; String baseURL, nodeURL; @BeforeTest public void setUp() throws MalformedURLException { baseURL = "http://demo.guru99.com/test/guru99home/"; nodeURL = "http://192.168.43.223:4444/wd/hub"; DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setBrowserName("chrome"); capability.setPlatform(Platform.WIN10); driver = new RemoteWebDriver(new URL(nodeURL), capability); } @AfterTest public void afterTest() { driver.quit(); } @Test public void sampleTest() { driver.get(baseURL); driver.get(baseURL); if (driver.getPageSource().contains("MOBILE TESTING")) { Assert.assertTrue(true, "Mobile Testing Link Found"); } else { Assert.assertTrue(false, "Failed: Link not found"); } } }
The above code launches chrome browser and navigates to the URL specified in the ‘baseURL’ variable. It verified a link name ‘Mobile Testing‘ is displayed on the page
URL on the ‘nodeURL‘ variable can be modified to reflect the IP Address of the remote machine. Test result can be verified on the default TestNG report generated
[Selenium+Java] Selenium Grid Tutorial: Command Line and JSON Example
标签:single follow machines play type ring chrome central debug
原文地址:https://www.cnblogs.com/alicegu2009/p/9078216.html