簡體   English   中英

在新標簽頁中切換時無法在 Chrome Headless 模式下捕獲屏幕截圖

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

我有以下配置:

硒 3.8.0

爪哇 8

Chrome 瀏覽器 60

Chromedriver v 2.31 64 位

我正在 Chrome 無頭模式下運行我的測試。 問題是,瀏覽器在切換到新選項卡並嘗試捕獲快照時沒有響應。

記錄以下錯誤:

[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 driver)函數中,如下所示:

switchWindow(WebDriver driver) 中,您嘗試switchTo().window(winHandle)而不驗證winHandle ,如下所示:

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

在這里,您沒有驗證winHandle是否已選擇parentWindowwindowHandlechild 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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM