相关文章推荐
严肃的跑步鞋  ·  Java代码实现 ...·  1 月前    · 
温暖的便当  ·  React es6 ...·  2 月前    · 
笑点低的荒野  ·  ORACLE ...·  1 年前    · 
备案 控制台
学习
实践
活动
专区
工具
TVP
写文章
专栏首页 用户3288143的专栏 【测试】Selenium Grid测试
3 0

海报分享

【测试】Selenium Grid测试

Selenium Webdriver API Selenium Grid 测试 专门用于在不同的浏览器、操作系统和机器上并行运行多个测试(兼容性测试)。 https://wangxiwei.gitbooks.io/webdriver-python/content/

配置环境

下载 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();
}

本地运行脚本测试

本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!
本文分享自作者个人站点/博客: https://blog.csdn.net/weixin_43469680 复制
如有侵权,请联系 cloudcommunity@tencent.com 删除。