1

I am using JSR223 Sampler and I want to start calculating time after url load so my code as below :

**

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
System.setProperty("webdriver.gecko.driver","/Users/geckodriver");
FirefoxOptions options = new FirefoxOptions().setAcceptInsecureCerts(true);
options.addArguments("--headless");
WebDriver driver = new FirefoxDriver(options);
def wait = new WebDriverWait(driver, 20);
driver.get('https://google.com/');
WDS.sampleResult.sampleStart();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@name='q']")));
WDS.sampleResult.sampleEnd();

**

alqoush
  • 21
  • 1
  • 7
  • I basically need to start calculating time after driver.get() .i used function sampleStart();/sampleEnd(); ended up with below error : 2020-10-07 16:08:11,096 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.sampleStart() is applicable for argument types: () values: [] – alqoush Oct 07 '20 at 05:21

1 Answers1

1

JSR223 Sampler automatically calculates its duration depending on your script contents so if you want to measure the time required to find an input you have to options:

  1. Create another JSR223 Sampler which will open the required page and store the WebDriver instance into JMeterVariables like:

    • First JSR223 Sampler:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.firefox.FirefoxOptions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      System.setProperty("webdriver.gecko.driver","/Users/geckodriver");
      FirefoxOptions options = new FirefoxOptions().setAcceptInsecureCerts(true);
      options.addArguments("--headless");
      WebDriver driver = new FirefoxDriver(options);
      def wait = new WebDriverWait(driver, 20);
      driver.get('https://google.com/');
      
      vars.putObject('driver', driver)
      vars.putObject('wait', wait)
      
    • Second JSR223 Sampler:

       import org.openqa.selenium.By;
       import org.openqa.selenium.support.ui.ExpectedConditions;
      
       def driver = vars.getObject('driver')
       def wait = vars.getObject('wait')
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@name='q']")));
      
  2. Use SampleResult.addSubResult() function to create a "child" sample result which will measure the time required to locate the element:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.By;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    System.setProperty("webdriver.gecko.driver","/Users/geckodriver");
    FirefoxOptions options = new FirefoxOptions().setAcceptInsecureCerts(true);
    options.addArguments("--headless");
    WebDriver driver = new FirefoxDriver(options);
    def wait = new WebDriverWait(driver, 20);
    driver.get('https://google.com/');
    
    def myResult = new org.apache.jmeter.samplers.SampleResult()
    myResult.setSampleLabel('Locating element')
    myResult.sampleStart()
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@name='q']")));
    myResult.setResponseCodeOK()
    myResult.setSuccessful(true)
    myResult.sampleEnd()
    
    SampleResult.addSubResult(myResult,false)
    

    in this case you will get something like:

    enter image description here

Check out Top 8 JMeter Java Classes You Should Be Using with Groovy to learn more about these vars and SampleResult shorthands

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • thanks, @Dmitri,I believe option 2 will suit me more with my current requirement but its throwing same error again. – alqoush Oct 07 '20 at 08:00
  • 2020-10-07 18:58:21,636 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started 2020-10-07 18:58:21,636 INFO o.a.j.t.JMeterThread: Thread started: FirefoxeBrowser 1-1 2020-10-07 18:58:25,427 ERROR o.a.j.s.SampleResult: sampleEnd called twice java.lang.Throwable: Invalid call sequence at org.apache.jmeter.samplers.SampleResult.sampleEnd(SampleResult.java:1145) [ApacheJMeter_core.jar:5.3] at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:82) – alqoush Oct 07 '20 at 08:02
  • I copied the above script a ran it. – alqoush Oct 07 '20 at 08:03
  • I switched to Optioned 2 , thanks for the help, it resolves my issue. – alqoush Oct 08 '20 at 04:48