我有以下配置:
硒 3.8.0
爪哇 8
铬浏览器 60
铬驱动程序 v 2.31 64 位
我正在铬无头模式下运行我的测试。问题是,浏览器在切换到新选项卡并尝试捕获快照时无响应。
记录了以下错误:
[727.930][SEVERE]:从渲染器接收消息超时: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 驱动程序(中,您尝试在不验证winHandle
的情况下switchTo().window(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
}
}