在测试中处理selenium网格集线器和节点之间的连接问题

时间:2018-03-13 02:53:57

标签: selenium selenium-webdriver selenium-grid

我使用在远程docker网格上运行的webdriver运行测试。有时,虽然测试已经抓住了浏览器并且在运行过程中,但转发请求的连接失败(可能在集线器和节点之间),然后由于此而导致测试失败。有没有优雅的方式,我可以在测试中处理这样的错误并重试运行测试。我知道网格问题是这里的主要问题,但这个问题必须单独处理,但我很想知道如何在测试中处理这些错误。我已经提到了我的测试和驱动程序工厂的样子。任何帮助将不胜感激。我还在底部提到了网格中的错误,由于哪个测试失败了,但是这个错误我并不关心,因为我正在单独处理该网格问题。

@BeforeClass(alwaysRun = true)
public void setUpBeforeClass(){
instantiateDriverAndpages(browserName,browserVersion,platform) // grab remote webdriver instance
}

@Test
public void test1(retryAnalyzer = TestBase.class){
// all tests here
// in this part sometimes, test fails because of connection issue with grid
}

public void instantiateDriverAndpages(String browserName, String browserVersion, String platform) throws Exception {

    driverFactory = new DriverFactory();
    driver = driverFactory.get(browserName, browserVersion);

    if (driver == null) {

        throw new WebDriverException("Failed to load Remote WebDriver Instances");
    }
    dr = (RemoteWebDriver) driver;
   }

@AfterClass(alwaysRun = true)
public void afterClass() {
    if (driver != null) {
        driver.quit();
    }
    if (dr != null) {
        dr.quit();
    }
}

@AfterMethod(alwaysRun = true)
public void afterTest(ITestResult result) throws Exception {
    methodName = result.getName();

    if (!result.isSuccess()) {
        log.error("Test  Failed in method " + methodName);
    }

    log.info("After method  {}", methodName);
}   

在Driverfactory类中的某个地方

    public WebDriver get(String browserName, String browserVersion, String platform) throws MalformedURLException, InterruptedException {
        // Set common webdriver properties
        des = new DesiredCapabilities();
            threadDriver = new ThreadLocal<RemoteWebDriver>();    
            des.setPlatform(Platform.valueOf(platform));
            des.setVersion(browserVersion);
            log.info("Setting cross browser run");

            // set the desired capabilities per browser
            switch (BrowserName.valueOf(browserName)) {
                case FIREFOX:
                    //set firefox cap
                    log.info("Setting FIREFOX browser");
                    break;
                case IE:
                    des.setBrowserName(BrowserType.IE);
                    log.info("Setting IE browser");
                    break;
                case CHROME:
                    des.setBrowserName(BrowserType.CHROME);
                    log.info("Setting CHROME browser");
                    break;

            }

            String seleniumIP="someIp";
            remoteWebDriver = new RemoteWebDriver(new URL(seleniumIP), des);
            remoteWebDriver.setFileDetector(new LocalFileDetector());
            driver = remoteWebDriver;
            threadDriver.set((RemoteWebDriver) driver);
              return getDriver();
        }


    }

由于哪个测试失败导致网格出错:

Caused by: org.openqa.grid.common.exception.GridException: Session [ee1ad4f37] was terminated due to FORWARDING_TO_NODE_FAILED
        at org.openqa.grid.internal.ActiveTestSessions.getExistingSession(ActiveTestSessions.java:116)
        at org.openqa.grid.internal.DefaultGridRegistry.getExistingSession(DefaultGridRegistry.java:389)
        at org.openqa.grid.web.servlet.handler.RequestHandler.getSession(RequestHandler.java:237)
        at org.openqa.grid.web.servlet.handler.RequestHandler.process(RequestHandler.java:122)
        at org.openqa.grid.web.servlet.DriverServlet.process(DriverServlet.java:84)
        at org.openqa.grid.web.servlet.DriverServlet.doPost(DriverServlet.java:68)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at org.seleniumhq.jetty9.servlet.ServletHolder.handle(ServletHolder.java:841)
        at org.seleniumhq.jetty9.servlet.ServletHandler.doHandle(ServletHandler.java:535)
        at 

0 个答案:

没有答案