自动化测试析疑——WebDriver启动时白屏挂起问题解决方法

发表于:2013-4-08 13:33

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:刘毅    来源:51Testing软件测试网采编

  WebDriver启动的时候很容易无限挂起,直到外围框架设定的超时时间达到而退出运行,给测试运行带来很大的困扰。而实际上WebDriver有一组timeout的设置方法,启动时的挂起属于页面加载的范畴,所以可以考虑用timeouts().pageLoadTimeout()来重新启动一个有效的实例来执行测试。

  Java代码:

* Description: catch page load timeout Exception and restart a new session.
* 内容描述:通过页面跳转是否超时来测试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.
* 内容描述:循环一定次数测试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.
* 内容描述:在做好配置之后创建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
* 内容描述:启动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.
* 内容描述:选择在本机执行,有人工干预,无需杀掉浏览器进程。
*
* @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!");
}
}

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号