简体   繁体   中英

Unable to capture screenshot in Chrome Headless mode while switched in new tab

I have below Configuration :

Selenium 3.8.0

Java 8

Chrome Browser 60

Chromedriver v 2.31 64bit

I'm running my test in chrome headless mode. 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.934][WARNING]: screenshot failed, retrying

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 :

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 :

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. Can someone help what is wrong here ?

The issue is within your switchWindow(WebDriver driver) function as follows :

Within switchWindow(WebDriver driver) you are trying to switchTo().window(winHandle) without validating the winHandle as follows :

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 .

Solution :

So the solution will be to validate that the winHandle must not be the parentWindow as follows:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM