[英]Unable to capture screenshot in Chrome Headless mode while switched in new tab
I have below Configuration :我有以下配置:
Selenium 3.8.0
硒 3.8.0
Java 8
爪哇 8
Chrome Browser 60
Chrome 浏览器 60
Chromedriver v 2.31 64bit
Chromedriver v 2.31 64 位
I'm running my test in chrome headless mode.我正在 Chrome 无头模式下运行我的测试。 The issues is, the browser get unresponsive while it switch to new tab and try to capture the snap.
问题是,浏览器在切换到新选项卡并尝试捕获快照时没有响应。
Following Error recorded :记录以下错误:
[727.930][SEVERE]: Timed out receiving message from renderer: 600.000
[727.930][SEVERE]:从渲染器接收消息超时:600.000
[727.934][WARNING]: screenshot failed, retrying
[727.934][警告]:截图失败,正在重试
The code where it causing the issue :导致问题的代码:
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
Method : 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
Method : 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();
}
}
While I'm running the same in GUI mode then all all works fine.虽然我在 GUI 模式下运行相同,但一切正常。 Can someone help what is wrong here ?
有人可以帮助这里有什么问题吗?
The issue is within your switchWindow(WebDriver driver) function as follows :问题出在您的switchWindow(WebDriver driver)函数中,如下所示:
Within switchWindow(WebDriver driver) you are trying to switchTo().window(winHandle)
without validating the winHandle
as follows :在switchWindow(WebDriver driver) 中,您尝试
switchTo().window(winHandle)
而不验证winHandle
,如下所示:
for (String winHandle : driver.getWindowHandles())
driver.switchTo().window(winHandle);
Here you are not validating whether winHandle
have picked up parentWindow
or windowHandle
of child window
.在这里,您没有验证
winHandle
是否已选择parentWindow
或windowHandle
的child window
。
So the solution will be to validate that the winHandle
must not be the parentWindow
as follows:因此,解决方案是验证
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.