我有以下配置:
硒3.8.0
Java 8
Chrome浏览器60
Chromedriver v 2.31 64bit
我在镀铬无头模式下运行我的测试。问题是,浏览器在切换到新选项卡并尝试捕获快照时无响应。
记录以下错误:
[727.930] [严重]:超时从渲染器接收消息:600.000
[727.934] [警告]:屏幕截图失败,正在重试
导致问题的代码:
if(myprofile.postalAddress.size()>0)
{
myprofile.getGetAddressMapIcon().get(0).click();
LogWriter.logger.info("Address Clicked");
CommonMethods.switchWindow(driver);
TakeScreenshot.passedScreenShot("GoogleMap");
driver.close();
CommonMethods.switchToParentWindow(driver);
LogWriter.logger.info("Map Window closed");
}
SwitchWindow
方法:
public static void switchWindow(WebDriver driver) throws IOException
{
parentWindow = driver.getWindowHandle();
for (String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
LogWriter.logger.info("-----");
}
LogWriter.logger.info("Window Title : " + driver.getTitle());
}
TakeScreenshot
方法:
public static void passedScreenShot(String testname) throws IOException
{
File sourceFile = ((TakesScreenshot) DriverSetup.driver).getScreenshotAs(OutputType.FILE);
DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy__hh_mm_ssaa");
destDir = System.getProperty("user.home")+"/AutomationTemp/Reports/Screenshots/PassedTest";
new File(destDir).mkdirs();
String destFile = dateFormat.format(new Date())+"RCON" + "_" + testname +".png";
try
{
Files.copy(sourceFile, new File(destDir + "/" + destFile));
}
catch (IOException e)
{
e.printStackTrace();
}
}
虽然我在GUI模式下运行相同,但一切正常。有人可以帮助解决这里的错误吗?
问题出在switchWindow(WebDriver驱动程序)函数中,如下所示:
在switchWindow(WebDriver驱动程序)中,您尝试switchTo().window(winHandle)
而不验证winHandle
如下:
for (String winHandle : driver.getWindowHandles())
driver.switchTo().window(winHandle);
在这里,你没有验证winHandle
是否已经获得了parentWindow
或windowHandle
的child window
。
所以解决方案是验证winHandle
不能是parentWindow
如下:
public static void switchWindow(WebDriver driver) throws IOException
{
parentWindow = driver.getWindowHandle();
new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
for (String winHandle : driver.getWindowHandles())
{
if (!parentWindow.equalsIgnoreCase(winHandle))
driver.switchTo().window(winHandle);
//you can do anything on the new tab/window
}
}