JMeter + Webdriver : Java code is not taking screenshot and it is giving ERROR c.g.j.p.w.s.WebDriverSampler: Sourced file: inline evaluation of:



  • 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



  • The error is due to missing https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html of https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/TakesScreenshot.html 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 http://www.beanshell.org/ interpreter which is not 100% Java-compatible. Moreover https://www.blazemeter.com/blog/is-beanshell-dead/ so perhaps "we" should consider to move to Groovy instead.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2