配置环境
下载
http://selenium-release.storage.googleapis.com/index.html
启动Hub
-role hub表示启动运行hub;
-port是设置端口号,hub的默认端口是4444
-maxSession为最大会话请求,这个参数主要要用并发执行测试用例,默认是1
java -jar c:/third/selenium-server-standalone-3.5.3.jar -role hub -maxSession 10 -port 4444
验证Hub控制台
http://192.168.43.58:4444/
Nodes should register to http://192.168.43.58:4444/grid/register/
启动节点1
-role node:表示启动的是node节点
-port 5555:指定node节点端口
-hub http://localhost:4444/grid/register:表示hub机地址
-maxSession 5:node节点最大会话请求
-browser browserName=firefox,seleniumProtocol=WebDriver,maxInstances=5,platform=WINDOWS 设置浏览器的参数
maxInstances表示最大实例,最多可运行的浏览器数,不能大于前面maxSession的值
java -jar c:/third/selenium-server-standalone-3.5.3.jar -role node -port 5555 -hub http://localhost:4444/grid/register -maxSession 5 -browser browserName=firefox,seleniumProtocol=WebDriver,maxInstances=5
主节点显示
节点2
java -jar c:/third/selenium-server-standalone-3.5.3.jar -role node -port 6666 -hub http://localhost:4444/grid/register -maxSession 5 -browser browserName=chrome,seleniumProtocol=WebDriver,maxInstances=5
Remote测试(Java)
Remote测试(python)
DesiredCapabilities
pytest代码分析
脚本录制
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class T1Test {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
@After
public void tearDown() {
driver.quit();
public String waitForWindow(int timeout) {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
Set<String> whNow = driver.getWindowHandles();
Set<String> whThen = (Set<String>) vars.get("window_handles");
if (whNow.size() > whThen.size()) {
whNow.removeAll(whThen);
return whNow.iterator().next();
@Test
public void t1() {
driver.get("https://www.baidu.com/");
driver.manage().window().setSize(new Dimension(881, 694));
driver.findElement(By.id("kw")).sendKeys("hadoop");
driver.findElement(By.id("kw")).sendKeys(Keys.ENTER);
vars.put("window_handles", driver.getWindowHandles());
driver.findElement(By.linkText("Hadoop_百度百科")).click();
vars.put("win3844", waitForWindow(2000));
driver.switchTo().window(vars.get("win3844").toString());
WebElement element = driver.findElement(By.cssSelector(".summary-pic img"));
Actions builder = new Actions(driver);
builder.moveToElement(element).perform();
WebElement element = driver.findElement(By.tagName("body"));
Actions builder = new Actions(driver);
builder.moveToElement(element, 0, 0).perform();
}
Java脚本
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
testBaidu("firefox");
// testBaidu("chrome");
public static void testBaidu(String browserName){
DesiredCapabilities capability = new DesiredCapabilities();
capability.setBrowserName(browserName);
capability.setPlatform(Platform.WINDOWS);
try {
//List list =new ArrayList<>()
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.get("http://www.baidu.com");
//输入 hadoop查询
driver.findElement(By.cssSelector("#kw")).sendKeys("hadoop" + Keys.ENTER);
//获得当前窗口url
String curUrl = driver.getCurrentUrl();
System.out.println(" browser:"+browserName+" url:"+curUrl);
//使用close()方法关闭浏览器后,并不会清除临时文件中的webdriver临时文件
//driver.close();
//使用quit()关闭浏览器后,会自动删除临时文件夹
//driver.quit();
} catch (
MalformedURLException e) {
e.printStackTrace();
}
本地运行脚本测试