I can not do and save a screenshot to a WebElement (image) using AShot

Hello!
On the page I have a picture. I need to save this image. For this I decided to use the method of creating the Ahot screenshots of a webelement.

—>For this we created the keyword.

public class takeScreenshot {

@Keyword

public void takeScreen(TestObject object){
	WebElement element = WebUiCommonHelper.findWebElement(object,20)
	WebDriver driver = DriverFactory.getWebDriver();	
	
	Screenshot screenshot = new AShot().takeScreenshot(driver,element);

	ImageIO.write(screenshot.getImage(),"PNG",new File("path\\to\\my\\directory\\image.png"))
}

}

–=>My test:

WebUI.openBrowser(‘URL’)

WebUI.maximizeWindow()

WebUI.scrollToElement(findTestObject(‘img_code’), 3)
CustomKeywords.‘screenshots.takeScreenshot.takeScreen’(findTestObject(‘img_code’))

—>And I get the error

java.lang.RuntimeException: org.openqa.selenium.JavascriptException: javascript error: $ is not defined
(Session info: chrome=76.0.3809.100)
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
System info: host: ‘DESKTOP-PS8G0QM’, ip: ‘192.168.56.1’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_181’
Driver info: com.kms.katalon.selenium.driver.CChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 76.0.3809.100, chrome: {chromedriverVersion: 76.0.3809.68 (420c9498db8ce…, userDataDir: C:\Users\Admin\AppData\Loca…}, goog:chromeOptions: {debuggerAddress: localhost:49678}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5821b158f744f8b9b07cc1ffb2f04e90
at ru.yandex.qatools.ashot.util.InnerScript.execute(InnerScript.java:32)
at ru.yandex.qatools.ashot.util.JsCoords.findCoordsWithJquery(JsCoords.java:24)
at ru.yandex.qatools.ashot.coordinates.JqueryCoordsProvider.ofElement(JqueryCoordsProvider.java:14)
at ru.yandex.qatools.ashot.coordinates.CoordsProvider.ofElements(CoordsProvider.java:21)
at ru.yandex.qatools.ashot.AShot.takeScreenshot(AShot.java:116)
at ru.yandex.qatools.ashot.AShot.takeScreenshot(AShot.java:134)
at ru.yandex.qatools.ashot.AShot$takeScreenshot.call(Unknown Source)
at screenshots.takeScreenshot.takeScreen(takeScreenshot.groovy:52)
at screenshots.takeScreenshot.invokeMethod(takeScreenshot.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at download_images.run(download_images:34)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1565416962945.run(TempTestCase1565416962945.groovy:21)
Caused by: org.openqa.selenium.JavascriptException: javascript error: $ is not defined
(Session info: chrome=76.0.3809.100)
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’
System info: host: ‘DESKTOP-PS8G0QM’, ip: ‘192.168.56.1’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_181’
Driver info: com.kms.katalon.selenium.driver.CChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 76.0.3809.100, chrome: {chromedriverVersion: 76.0.3809.68 (420c9498db8ce…, userDataDir: C:\Users\Admin\AppData\Loca…}, goog:chromeOptions: {debuggerAddress: localhost:49678}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 5821b158f744f8b9b07cc1ffb2f04e90
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at com.kms.katalon.selenium.driver.CChromeDriver.execute(CChromeDriver.java:20)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
at ru.yandex.qatools.ashot.util.InnerScript.execute(InnerScript.java:30)
… 21 more

—>Please tell me what I can do?

Hi Valeria,

I’m quite intrigued about this… Are you sure that this error message you have sent is related to the code you shared?? Because as you can see in the error message it finds '$'

Do you have that character in your code?? Maybe that error happens before it hits your screenshot code. Try to check…

Also, based on the link below. . . . I’m thinking that you have a ‘js.executescript()’ that doesn’t functioning well and that character ‘$’ is written inside your executeScript parameter.


I did some research about this and you have the same issue with the link below:


There are some replies there that might help you…

Hope that helps. . . :slight_smile: