切换到新标签

时间:2017-12-29 11:52:14

标签: java google-chrome selenium headless-browser google-chrome-headless

我的配置如下:

  

Selenium 3.8.0

     

Java 8

     

Chrome浏览器60

     

Chromedriver v 2.31 64bit

我在chrome无头模式下运行我的测试。问题是,浏览器在切换到新选项卡并尝试捕获快照时无响应。

记录以下错误:

  

[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模式下运行相同,但一切正常。有人可以帮助解决这里的问题吗?

1 个答案:

答案 0 :(得分:1)

问题出在 switchWindow(WebDriver驱动程序)函数中,如下所示:

switchWindow(WebDriver驱动程序)中,您试图switchTo().window(winHandle)而未验证 winHandle ,如下所示:

for (String winHandle : driver.getWindowHandles()) 
    driver.switchTo().window(winHandle);

此处您尚未验证 winHandle 是否已选择 parentWindow windowHandle {{1} }

解决方案:

因此解决方案是验证 child window 不能是 winHandle ,如下所示:

parentWindow