java selenium grid实践

  1. 下载jar包:selenium-server-standalone-3.141.59.jar和驱动,执行机上必须安装java环境和浏览器驱动,客户端作为控制器如果不执行测试,则不必安装驱动

  2. 启动hub:
    java -jar selenium-server-standalone-3.141.59.jar -role hub -port 8888 -maxSession 10

  3. 启动node:(需要保持双方能够相互通信)
    java -Dwebdriver.chrome.driver="输入驱动的确定路径" -jar selenium-server-standalone-3.141.59.jar -role node -hub "hub地址:8888/grid/register/" -port 18881 -browser "browserName=chrome,maxInstances=2,version=92,platform=MAC"

  4. 测试代码:
    `public class TestMain {
    private static WebDriver driver;
    @Test
    public static void test() throws InterruptedException, MalformedURLException {
    String url1 = "http://nodeurl1:18881/wd/hub";
    String url2 = "http://nodeurl2:18882/wd/hub";
    Thread thread1 = new Thread(() -> {
    try {
    invokeBrowser(url1);
    System.out.println(Thread.currentThread().getName());

         } catch (MalformedURLException | InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println(Thread.currentThread().getName());
     });
     Thread thread2 = new Thread(() -> {
         try {
             invokeBrowser(url2);
             System.out.println(Thread.currentThread().getName());
         } catch (MalformedURLException | InterruptedException e) {
             e.printStackTrace();
         }
     });
    
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
    

    }

    public static void invokeBrowser(String url3) throws MalformedURLException, InterruptedException {
    // 期望能力对象
    DesiredCapabilities capabilities = new DesiredCapabilities();
    //配置测试的浏览器,使用chrome浏览器
    capabilities.setBrowserName(BrowserType.CHROME);
    capabilities.setPlatform(Platform.MAC);
    // hub节点
    String url = url3;
    //和hub建立通讯,把相应配置传给hub,hub会根据配置选择注册的node节点,打开相应的浏览器进行测试
    WebDriver driver = new RemoteWebDriver(new URL(url), capabilities);
    driver.get("https://www.baidu.com/");
    driver.findElement(By.id("kw")).sendKeys("selenium");
    Thread.sleep(5000);
    driver.quit();
    }
    }
    `

posted @ 2021-09-12 17:29  L-robot  阅读(221)  评论(0)    收藏  举报