Selenium v​​>=3.4.0 上的整页截图?

Full-page screenshot on Selenium v>=3.4.0?

Selenium 开发者决定停止整页截图功能: https://github.com/SeleniumHQ/selenium/issues/3912

未来需要什么代码才能在应用程序测试脚本中模拟此功能?

我尝试降级到 Selenium 3.3.1,但对驱动程序 and/or 浏览器版本有一些依赖性,该版本不向前兼容。

试试这个。

import unittest
from selenium import webdriver
import datetime

class Tests(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(desired_capabilities={'browserName': 'chrome', 'chromeOptions': {'args': ['--headless'], 'binary': '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'}})
        self.driver.get('http://google.com/')

def tearDown(self):
    self.driver.quit()

def test_screenshot(self):
    width = self.driver.execute_script(
        "return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
    height = self.driver.execute_script(
        "return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")
    self.driver.set_window_size(width+100, height+100)
    self.driver.save_screenshot('screenshot ' + str(datetime.datetime.now().date()) + ' ' + str(datetime.datetime.now().time()) + '.png')
if __name__ == '__main__':
    unittest.main()

在一个不起眼的开源库中找到的解决方案:

pom.xml

    <dependency>
        <groupId>ru.yandex.qatools.ashot</groupId>
        <artifactId>ashot</artifactId>
        <version>1.5.4</version>
    </dependency>

SomeJavaTest.java

private void screenshot(File out) throws IOException, InterruptedException, AWTException {

    final Screenshot screenshot = new AShot()
            .shootingStrategy(ShootingStrategies.viewportPasting(100))
            .takeScreenshot(driver);

    final BufferedImage image = screenshot.getImage();
    ImageIO.write(image, "PNG", out);

}