WDS.sampleResult.sampleStart() для семплера JSR223
Я использую JSR223 Sampler, и я хочу начать вычислять время после загрузки URL-адреса, чтобы мой код был следующим:
**
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();
**
1 ответ
JSR223 Sampler автоматически рассчитывает его продолжительность в зависимости от содержимого вашего скрипта, поэтому, если вы хотите измерить время, необходимое для поиска ввода, у вас есть параметры:
Создайте еще один JSR223 Sampler, который откроет требуемую страницу и сохранит экземпляр WebDriver в JMeterVariables, например:
Первый семплер JSR223:
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)
Второй семплер JSR223:
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']")));
Используйте функцию SampleResult.addSubResult(), чтобы создать "дочерний" результат выборки, который будет измерять время, необходимое для обнаружения элемента:
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)
в этом случае вы получите что-то вроде:
Ознакомьтесь с 8 лучшими Java-классами JMeter, которые следует использовать с Groovy, чтобы узнать о них больше.
vars
и
SampleResult
сокращения