Screenshot with Katalon and Chrome headless mode

2019-07-23 07:57发布

I'm using the following code to take screenshots of warning and error messages in my Katalon Studio scripts:

import ru.yandex.qatools.ashot.AShot
import ru.yandex.qatools.ashot.Screenshot
import ru.yandex.qatools.ashot.coordinates.*
import ru.yandex.qatools.ashot.cropper.*

public class ScreenshotHelper {


  public void takeWebElementScreenshot(TestObject object) {
    WebElement element = WebUiCommonHelper.findWebElement(object, 20)
    WebDriver driver = DriverFactory.getWebDriver();
    String fileName = new SimpleDateFormat("yyyyMMddHHmmSSS").format(new Date())
    Screenshot screenshot = new AShot().takeScreenshot(driver, element)
    ImageIO.write(screenshot.getImage(),'PNG', new File(System.getProperty("user.dir")+"/ErrorScreenshots/ElementScreenshot"+"_"+fileName+".png"))
  }
}

This method gets called from another method of the same class:

public void catchNotyMessage(){

TestObject noty_warning = WebUI.modifyObjectProperty(findTestObject("DUMMY"), 'css', 'equals', 'div.noty_type_warning', true)
TestObject noty_error = WebUI.modifyObjectProperty(findTestObject("DUMMY"), 'css', 'equals', 'div.noty_type_error', true)

    if (WebUI.verifyElementPresent(noty_error, 1, FailureHandling.OPTIONAL)){
        this.takeWebElementScreenshot(noty_error)
    }
    else if (WebUI.verifyElementPresent(noty_warning, 1, FailureHandling.OPTIONAL)){
        this.takeWebElementScreenshot(noty_warning)
    }
}

And it works fine, the screenshot gets taken when using Katalon in normal mode.

However, when I run the script in headless mode, I get the following warning:

WARNING com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/DUMMY' located by 'By.cssSelector: div.noty_type_error' not found

even though the element should be present. And the test fails with the java.lang.NullPointerException.

Is is because of the headless execution? And how can I fix this?

2条回答
贼婆χ
2楼-- · 2019-07-23 08:37

After looking through https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html, java.io.FileNotFoundException: the system cannot find the file specified, Chrome Headless Doesn't work and Java "user.dir" property - what exactly does it mean?, i finally figured it out.

The problem is that System.getProperty("user.dir") changes when the test gets executed via command line in headless mode. So, this code works:

    public void takeWebElementScreenshot(TestObject object) {
    WebElement element = WebUiCommonHelper.findWebElement(object, 20)
    WebDriver driver = DriverFactory.getWebDriver();
    String fileName = new SimpleDateFormat("yyyyMMddHHmmSSS").format(new Date())
    Screenshot screenshot = new AShot().takeScreenshot(driver, element)
    try {
        if (DriverFactory.getExecutedBrowser().getName()=='HEADLESS_DRIVER'){
            ImageIO.write(screenshot.getImage(),'PNG', new File("C:/Users/path_to_working_directory/ErrorScreenshots/HeadlessElementScreenshot"+"_"+fileName+".png"))
        } else {
            ImageIO.write(screenshot.getImage(),'PNG', new File(System.getProperty("user.dir")+"/ErrorScreenshots/ElementScreenshot"+"_"+fileName+".png"))
        }

    } catch (Exception e) {
        e.printStackTrace()
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-23 08:52

You must set parameter to your headless chrome:

enter image description here

查看更多
登录 后发表回答