前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >【测试】Selenium Grid测试

【测试】Selenium Grid测试

作者头像
瑞新
发布2020-07-07 18:42:37
发布2020-07-07 18:42:37
1.1K00
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
复制
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的值

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
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代码分析

脚本录制

代码语言:javascript
代码运行次数:0
运行
复制
// 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脚本

代码语言:javascript
代码运行次数:0
运行
复制
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();
        }
    }



}

本地运行脚本测试

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置环境
  • 启动Hub
  • 验证Hub控制台
  • 启动节点1
  • 节点2
  • Remote测试(Java)
  • Remote测试(python)
  • DesiredCapabilities
  • pytest代码分析
  • 脚本录制
    • Java脚本
  • 本地运行脚本测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档