问题描述
我正在尝试使用JSR223 + groovy模拟来自移动设备的HTTP请求,以进行客户端性能测试,问题是我无法以这种方式执行,请单击页面上的“接受所有Cookie” span元素
响应:
响应消息:javax.script.ScriptException: javax.script.ScriptException:groovy.lang.MissingPropertyException:否 这样的属性:类Script7的ExpectedConditions
在脚本的“ desctop”部分中,xpath(通过WebDriver采样器实现)并单击此“接受所有Cookies” span元素可以100%正确地工作-因此,我知道xpath是正确的。
能否请您看一下问题并分享您的想法和任何提示,请问JSR223出现问题的原因在哪里以及如何解决?
代码在这里:
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();
使用JSR223在客户端上模拟移动设备时,如何执行相同的操作?
解决方法
在我看来像是打字错误或复制粘贴问题,您需要像这样更改:
import org.openqa.selenium.support.ui.ExpectedCondition;
此行:
import org.openqa.selenium.support.ui.ExpectedConditions;
因为它必须是完全合格的且正确的ExpectedConditions类名
也不要将JMeter函数或变量内联到JSR223脚本中,因为:
- 它们被缓存,并且在随后的迭代中,您将获得相同的值
- 他们可能会解决导致逻辑故障或编译失败的问题
- 它们与Groovy GString templates冲突
因此使用"${webdriver_path}"
代替vars.get("webdriver_path")
,以此类推,其中vars
是JMeterVariables类实例的简写。有关更多详细信息,请参见Top 8 JMeter Java Classes You Should Be Using with Groovy文章