0
votes

Scenario:

  1. open main page and click on "Accept All Cookies" (JSR223 Sampler1 in Once Only controller);
  2. open pages from the set of parametrized urls (JSR223 Sampler2 in another controller).

JSR223 Sampler1 code for main page:

import org.apache.jmeter.samplers.SampleResult; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.concurrent.TimeUnit;

System.setProperty("webdriver.chrome.driver", "vars.get("webdriver_path")");

Map<String, Object> mobileEmulation = new HashMap<>(); mobileEmulation.put("userAgent", "vars.get("userAgent")"); Map<String, Object> chromeOptions = new HashMap<>(); chromeOptions.put("mobileEmulation", mobileEmulation); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("mobileEmulation", mobileEmulation); ChromeDriver driver = new ChromeDriver(options);

driver.get("https://vars.get("main_page")"); WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath"))); driver.findElement(By.xpath("xpath")).click(); log.info(driver.getTitle());

JSR223 Sampler2 code for any page from the set of urls:

driver.get("https://${url}");

Error message: Response message:javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: driver for class

Problem: If I just copy all code from JSR223 Sampler1 to JSR223 Sampler2 and change destination url, urls are opening, but in unproper way - launching each time new browser instance, and I can't have realistic response time (for driver.get("url") only), because result provides time of Sampler work, which includes driver initialization, new browser instance start and it takes several seconds...

Could you please propose any ideas, how is possible to resolve this problem? To get all requests in 1 browser instance and to have realistic response time for all requests in JSR223 Sampler2 for browser.get("url") only? Will appreciate for any help.

1

1 Answers

1
votes
  1. In the first JSR223 Sampler you need to store your driver instance into JMeter Variables like:

    vars.putObject("driver", driver)
    

    it should be the last line of your script

  2. In the second JSR223 Sampler you need to get the driver instance from JMeter Variables like:

    driver = vars.getObject("driver")
    

    it should be the first line of your script

vars is the shorthand for JMeterVariables class instance, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for JSR223 Test Elements

P.S. the same approach with vars you should follow when executing driver.get() function like:

driver.get("https://" + vars.get("url"))