How to run Selenium tests with a standalone / containerized server
This question regards how to start a standalone Selenium server - currently it appears as though my junit test will start up the Selenium server for me, and I am looking to do that separately.
// selenium test script using junit:
package suman;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumTest {
private static FirefoxDriver driver;
private WebElement element;
@BeforeClass
public static void openBrowser(){
System.setProperty("webdriver.gecko.driver", "/home/oleg/Desktop/geckodriver");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void valid_UserCredential(){
System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
driver.get("http://www.store.demoqa.com");
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys("testuser_3");
driver.findElement(By.id("pwd")).sendKeys("Test@123");
driver.findElement(By.id("login")).click();
try{
element = driver.findElement (By.xpath(".//*[@id='account_logout']/a"));
}catch (Exception e){
}
Assert.assertNotNull(element);
System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName());
}
@Test
public void inValid_UserCredential()
{
System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
driver.get("http://www.store.demoqa.com");
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys("testuser");
driver.findElement(By.id("pwd")).sendKeys("Test@123");
driver.findElement(By.id("login")).click();
try{
element = driver.findElement (By.xpath(".//*[@id='account_logout']/a"));
}catch (Exception e){
}
Assert.assertNotNull(element);
System.out.println("Ending test " + new Object(){}.getClass().getEnclosingMethod().getName());
}
@AfterClass
public static void closeBrowser(){
driver.quit();
}
}
// pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>suman</groupId>
<artifactId>suman</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>suman</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project>
when I run:
mvn clean test
it will start a selenium server in the background, and the test will use that selenium server. My question is - how can I configure my project so that my test scripts use an external/containerize/standalone Selenium server? I don't want my test process to start the Selenium server - I want to start that separately on my own.
Download selenium-server-standalone.jar
from Selenium Official Site.
Then, run this jar with parameters start java -jar "C:mypathtofileselenium-server-standalone-3.7.1.jar" -role hub -port 4445
This will start a HUB which takes your tests and distributes it to nodes. Nodes are machines (can be the same as the hub) where Browser will physically start its process.
To create a node, we'll use the very same jar file.
To start a node: start java -Dwebdriver.chrome.driver="C:pathtochromedriverchromedriver.exe" -jar "C:pathtoseleniumjarselenium-server-standalone-3.7.1.jar" -role webdriver -browser "browserName=chrome,version=64.0,maxInstances=3" -hub http://localhost:4445/grid/register -port 5580
The above will allow you to create a node with 3 chrome instances. You can of course use any other browser.
When You finally set up your Selenium Grid (hub + nodes) you want to distribute tests to it.
Without Selenium Grid, test are running always on your local machine and browsers are created by new ChromeDriver()
or new FirefoxDriver()
classes.
There is one class which runs the tests against provided URL (hub's url). It's RemoteWebDriver
.
Example of how to use: WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4445/wd/hub"), DesiredCapabilities.chrome());
Now every test will be run on your custom Selenium Server. You might use it to run tests against 10 or more instances, depends on your machine. You can even use few different computers if on the same network.
链接地址: http://www.djcxy.com/p/50724.html