Many people have reported that the screenshots taken by my sample code shows only 1/2 or 1/3 of the original screen image.
I have found out my fault, and fixed it.
Device-Pixcel-Ratio (DPR for short) matters.
What is DPR? See
DPR tells how many of the screen’s actual pixels should be used to draw a single CSS pixel.
On my MacBook Air with Retina display 13inch (2560x1600), DPR is 2.0f.
On your mobile device, possibly DPR is 3.0f. Therefore the screenshot image you got looks to be 1/3 width of the original.
AShot does supports DPR in its API. In the source code of pazone.ashot.ShootingStrategies, you can find several occurence of dpr
literal.
If you use AShot on a device with DPR>1.0f, then you should consider using API specifying the appropriate value of DPR as parameter.
I revised my ksbackyard project to version 0.41.0 where DPR is supported.
com.kazurayam.ksbackyard.ScreenshotDriver takeEntirePageScreenshot()
now takes DPR into account:
static BufferedImage takeEntirePageImage(WebDriver webDriver, Options options) {
...
float dpr = options.getDevicePixelRatio()
AShot aShot = new AShot().
coordsProvider(new WebDriverCoordsProvider()).
shootingStrategy(
ShootingStrategies.viewportPasting(ShootingStrategies.scaling(dpr), timeout))
You need to find out the value of Device-Pixel-Ration of your platform runtime. But how? ---- JavaScript on browser can tell it to you. So I added a groovy class com.kazurayam.ksbackyard.DevicePixcelRationResolver
, which makes a query to browser via WebDriver, execute a javascript window.devicePixcelRatio
, return the dpr value to the caller Groovy.
ksbackyard Test Cases/main/demo_savingEntirePageImage shows a runnable example of taking entire page Screenshot with DPR considered:
// Test Cases/main/demo_savingEntirePageImage
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import org.openqa.selenium.WebDriver
import com.kazurayam.ksbackyard.DevicePixelRatioResolver
import com.kazurayam.ksbackyard.ScreenshotDriver
import com.kazurayam.ksbackyard.ScreenshotDriver.Options
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path outDir = projectDir.resolve("tmp/main")
Files.createDirectories(outDir)
WebUI.openBrowser("")
WebUI.setViewPortSize(400, 640)
WebUI.navigateToUrl("http://demoaut.katalon.com/")
WebDriver driver = DriverFactory.getWebDriver()
File outFile = outDir.resolve("screenshot.png").toFile()
float dpr = DevicePixelRatioResolver.resolveDPR(driver)
Options options = new Options.Builder()
.timeout(100)
.devicePixelRatio(dpr)
.build()
ScreenshotDriver.saveEntirePageImage(driver, outFile, options)
WebUI.delay(1)
WebUI.closeBrowser()