0

I am new to JMeter + WebDriver + Java. Javascript code is working as expected for screenshot. Due to recent upgrade of Java, we are moving from js to Java Below is the code. Can you please help on it

import org.openqa.selenium.By;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui;
import java.io.File;
import org.apache.commons.io.FileUtils;
import Java.awt.Robot;
import java.awt.image.BufferedImage;

WDS.sampleResult.sampleStart();

WDS.browser.get("https://www.google.com/");
Thread.sleep(10);

TakesScreenshot ts = (TakesScreenshot)WDS.browser;
        File srcFile = ts.getScreenshotAs(OutputType.FILE);
        File destFile = new File("/Users/****/desktop/screenshot.png");
        try{
            FileUtils.copyFile(srcFile, destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

WDS.sampleResult.sampleEnd();

Also I tried below code

 File mFile = ((TakesScreenshot)WDS.browser).getScreenshotAs(OutputType.FILE);
 FileUtils.copyFile(mFile, new File("output/" + WDS.name + "-" + WDS.parameters + ".png"));

Error message: 2022-05-03 11:28:51,641 ERROR c.g.j.p.w.s.WebDriverSampler: Sourced file: inline evaluation of: import org.openqa.selenium.By; import org.openqa.selenium.support.events.EventFi . . . '' : Typed variable declaration : Class: TakesScreenshot not found in namespace : at Line: 14 : in file: inline evaluation of: import org.openqa.selenium.By; import org.openqa.selenium.support.events.EventFi . . . '' : TakesScreenshot in inline evaluation of: ``import org.openqa.selenium.By; import org.openqa.selenium.support.events.EventFi . . . '' at line number 14

enter image description here

1 Answer 1

0

The error is due to missing import of TakesScreenshot class

You need to add the next line:

import org.openqa.selenium.TakesScreenshot

to the beginning of your script.

Also with regards to "moving from js to Java", where you are moving is not "Java", it's Beanshell interpreter which is not 100% Java-compatible. Moreover using Beanshell is some form of a performance anti-pattern so perhaps "we" should consider to move to Groovy instead.

1
  • Thank you for your reply. Added the import. Now error is coming for next line. File srcFile = ts.getScreenshotAs(OutputType.FILE); I already added import java.io.File;
    – Pavan
    May 3 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.