Tomar una captura de pantalla es muy rápido si el código se ejecuta en un servidor en localhost , menos de un segundo (alrededor de 400-500 ms):
private RemoteWebDriver driver; private DesiredCapabilities dc = new DesiredCapabilities(); @Before public void setUp() throws MalformedURLException { .... .... dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME); driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc); } @Test public void test() throws InterruptedException, IOException { driver.get("https://google.com"); driver.findElement(By.name("q")).sendKeys("automation test"); long before = System.currentTimeMillis(); //here is the problem File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); long after = System.currentTimeMillis(); System.out.println(after-before); FileUtils.copyFile(srcFile, new File("/Users/name/localpath/test.png")); } Pero si el servidor de destino se cambia a public ip que tiene instalado el servidor Selenium, tomar la captura de pantalla será más lento (alrededor de 5 segundos). Tal vez esto podría deberse a la distancia entre el cliente y el servidor, por lo que debe haber una diferencia.
¿Es posible reducir el tiempo de captura de pantalla? Estoy pensando en reducir la resolución de la imagen, pero ¿cómo la ajusto?
Una forma de obtener capturas de pantalla de menor tamaño (aparte de usar varios formatos de archivo) es cambiar el tamaño de la captura de pantalla: puede tomar una captura de pantalla de un elemento web (o una región de una página) que le interese especialmente.
Intente lo siguiente (necesitará usar la clase BufferedImage ):
@Test public void test() throws InterruptedException, IOException { driver.get("https://google.com"); driver.findElement(By.name("q")).sendKeys("automation test"); long before = System.currentTimeMillis(); File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); Point p = element.getLocation(); int width = element.getSize().getWidth(); int height = element.getSize().getHeight(); BufferedImage img = ImageIO.read(scrFile); BufferedImage elementScreenshot = img.getSubimage(p.getX(), p.getY(), width, height); //NOTE: the line above will crop the full page screenshot to element dimensions, change width and height if you wish to crop to region Image.write(elementScreenshot, "png", scrFile); FileUtils.copyFile(srcFile, new File("/Users/name/localpath/test.png")); long after = System.currentTimeMillis(); System.out.println(after-before); }Lo más probable es que el problema sea transferir el archivo por cable. También podría estar en la creación del archivo.
Sugeriría probar con la salida Base64 para ver si eso reduce el tiempo de transferencia:
String screenshotAsBase64String = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);