【开源自动化测试疑难FAQ】【WebDriver】WebDriver启动时白屏挂起问题解决方法(一)
2013-01-20 17:20
369 查看
WebDriver启动的时候很容易无限挂起,直到外围框架设定的超时时间达到而退出运行,给测试运行带来很大的困扰。而实际上WebDriver有一组timeout的设置方法,启动时的挂起属于页面加载的范畴,所以可以考虑用timeouts().pageLoadTimeout()来重新启动一个有效的实例来执行测试。
/** * Description: catch page load timeout Exception and restart a new session.</BR> * 内容描述:通过页面跳转是否超时来测试WebDriver启动时是否发生挂死异常。 * * @param driver RemoteWebDriver object. * @param browser the browser mode. * @param testUrl the url used to navigate by the driver.get method. * @throws Exception */ private boolean hasLoadPageSucceeded(RemoteWebDriver driver, String browser, String testUrl) throws Exception { try { driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS); driver.get(testUrl); return true; } catch (TimeoutException te) { LOG.error("******************本次启动WebDriver异常挂起******************"); setBuildEnvChoice(browser); driverObjectInitalize(); return false; } } /** * Description: catch page load timeout Exception and restart a new session.</BR> * 内容描述:循环一定次数测试WebDriver启动是否挂死。 * * @param driver RemoteWebDriver object. * @param browser the browser mode. * @param testUrl the url used to navigate by the driver.get method. * @param repeatTimes retry times. * @throws Exception */ private void driverStatusTest(RemoteWebDriver driver, String browser, String testUrl, int repeatTimes) throws Exception { int index = 0; boolean suspended = true; while (index < repeatTimes && suspended){ suspended = !hasLoadPageSucceeded(driver, browser, testUrl); index ++; } if (index == repeatTimes && suspended){ throw new RuntimeException("can not start webdriver successfully, it's suspended!"); } } /** * Description: start webdirver after capability settings completed.</BR> * 内容描述:在做好配置之后创建WebDriver实例。 * * @throws Exception */ private String driverObjectInitalize() throws Exception{ if (USE_DRIVERSERVER){//是否使用IEDirverServer driver = new RemoteWebDriver(service.getUrl(), capability); return service.getUrl().toString(); }else{ URL url = new URL("http://localhost:" + server.getPort() + "/wd/hub"); driver = new RemoteWebDriver(url, capability); return "http://localhost:" + server.getPort() + "/wd/hub"; } } /** * Description: start webdirver</BR> * 内容描述:启动WebDriver实例。 * * @param browser the browser mode * @throws RuntimeException */ protected void startWebDriver(String browser) { try { setBrowserMode(browser); String url = driverObjectInitalize();//about:blank is useless on some machines. driverStatusTest(driver, browser, url, 2); driver.manage().timeouts().implicitlyWait(maxWaitfor, TimeUnit.SECONDS); driver.manage().timeouts().setScriptTimeout(maxWaitfor, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(maxLoadTime, TimeUnit.SECONDS); } catch (Exception e) { LOG.error(e); throw new RuntimeException(e); } } /** * Description: set browser mode on local machines: do not close browsers already opened.</BR> * 内容描述:选择在本机执行,有人工干预,无需杀掉浏览器进程。 * * @throws Exception */ private void setBrowserMode(String browser) throws Exception{ if (browser.toLowerCase().contains("ie") || browser.toLowerCase().contains("internetexplorer")) { capability = DesiredCapabilities.internetExplorer(); } else if (browser.toLowerCase().contains("ff") || browser.toLowerCase().contains("firefox")) { capability = DesiredCapabilities.firefox(); } else if (browser.toLowerCase().contains("chrome")) { capability = DesiredCapabilities.chrome(); } else if (browser.toLowerCase().contains("safari")) { capability = DesiredCapabilities.safari(); } else if (browser.toLowerCase().contains("opera")) { capability = DesiredCapabilities.opera(); } else if (browser.toLowerCase().contains("htmlunit")) { capability = DesiredCapabilities.htmlUnit(); } else { throw new IllegalArgumentException("you are using wrong mode of browser paltform!"); } }
相关文章推荐
- 【开源自动化测试疑难FAQ】【WebDriver】WebDriver启动时白屏挂起问题解决方法(二)
- 【开源自动化测试疑难FAQ】【WebDriver】封装GET方法来解决页面跳转不稳定的问题
- 【开源自动化测试疑难FAQ】【WebDriver】RemoteWebDriver日志问题处理过程
- 【开源自动化测试疑难FAQ】【WebDriver】几种类型的组件的判断
- 【开源自动化测试疑难FAQ】【WebDriver】WebDriver对SWFUpload的无奈之举
- 【开源自动化测试疑难FAQ】【WebDriver】不可编辑域和日历控件域的输入
- 记录一下解决webdriver启动浏览器报“Unable to find a free port”问题的方法
- [bigdata] 启动CM出现 “JDBC Driver class not found: com.mysql.jdbc.Driver” 以及“Error creating bean with name 'serverLogFetcherImpl'”问题的解决方法
- WebDriver打开firefox浏览器崩溃问题解决方法
- 解决Android应用冷启动时出现的白屏问题的方法
- WebDriver封装GET方法来解决页面跳转不稳定的问题
- 访问 IIS 元数据库失败 或 W3SVC 的访问权问题解决方法 或 无法在Web服务器上启动调试
- Android编程中activity启动时出现白屏、黑屏问题的解决方法
- 解决Selenium Webdriver执行测试时,每个测试方法都打开一个浏览器窗口的问题
- 转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题
- Android中Splash应用启动白屏问题的解决方法
- Microsoft Office每次启动自动配置问题解决方法
- Win10系统安全中心服务启动不了问题的解决方法
- 【分享】VS200X水晶报表边框效果在WEB页面不连续显示问题解决方法
- hive启动问题解决方法