Search code examples
jmeteremulationclient-sidejsr223mobile-devices

JMeter - mobile devices requests emulation with JSR223 & groovy - No such property: ExpectedConditions error


I'm trying to emulate https requests from mobile devices for client-side performance testing, using JSR223 + groovy, and the problem is that I couldn't perform in this way click on "Accept All Cookies" span element on page.

Response:

Response message:javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: ExpectedConditions for class: Script7

In 'desctop' part of script, xpath (implemented via WebDriver sampler) and click on this "Accept All Cookies" span element works 100% correctly - and by this reason I know, that xpath is correct.

Could you please take a look at the problem and share your thought and any tips, where could be the reason of problem in JSR223 and how is possible to solve it?

Code is here:

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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

System.setProperty("webdriver.chrome.driver", "${webdriver_path}");

Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", ${width});
deviceMetrics.put("height", ${height});
deviceMetrics.put("pixelRatio", ${pixelRatio});
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "${userAgent}");
Map<String, Object> chromeOptions = new HashMap<>();
chromeOptions.put("mobileEmulation", mobileEmulation);
//DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("mobileEmulation", mobileEmulation);
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://${HOST_MAIN}");

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(), 'Accept All Cookies')]")));
driver.findElement(By.xpath("//span[contains(text(), 'Accept All Cookies')]")).click();

//this way doesn't work too:
//WebDriverWait(driver, 20).until(EC.element_to_be_clickable(By.xpath("//span[contains(text(), 'Accept All Cookies')]"))).click();

log.info(driver.getTitle());
driver.quit();

How can I implement the same action when emulating mobile devices on client-side using JSR223?


Solution

  • Looks like a typo or a copy-paste issue to me, you need to change this like:

    import org.openqa.selenium.support.ui.ExpectedCondition;
    

    to this line:

    import org.openqa.selenium.support.ui.ExpectedConditions;
    

    as it has to be fully qualified and correct ExpectedConditions class name

    Also don't inline JMeter Functions or Variables into JSR223 scripts because:

    1. They are cached and on subsequent iterations you will get the same value
    2. They may resolve into something causing logic malfunction or compilation failure
    3. They conflict with Groovy GString templates

    so instead of "${webdriver_path}" use vars.get("webdriver_path") and so on where vars is the shorthand for JMeterVariables class instance. See Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details