java参数化selenium junit中的一个变量,以加载测试在jmeter上运行的网站
我需要加载测试一个执行XHR对象的网站。 正常的负载测试在这种情况下不适用,因为负载测试寻找的是服务器端,而我对客户端感兴趣
我唯一需要参数化的是用户名,以使脚本同时运行
我确实使用了Katalon Recorder以JUnit的形式获取自动化代码,然后将其输入selenium,然后将其导出为jar文件,并在Jmeter中使用
该代码适用于单个用户,但我想不出一种方法来让脚本同时运行
包装仪表板
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Testpage {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "Path/To/chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testDashboard() throws Exception {
driver.get("https://test.com/newlogin");
driver.findElement(By.id("UserName")).clear();
driver.findElement(By.id("UserName")).sendKeys("Username");
driver.findElement(By.id("Password")).clear();
driver.findElement(By.id("Password")).sendKeys("Password!");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='LOGIN'])[1]/following::input[3]")).click();
driver.get("https://test.com");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Italy'])[3]/following::td[4]"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.get("https://test.com/1");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Guest_Arrivals_CY'])[2]/following::span[1]"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.get("https://test.com/2");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Nov 18'])[1]/following::div[4]"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.get("https://test.com/3");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.cssSelector("#kSXbjj > svg"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.get("https://test.com/4");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.cssSelector("#tGPUB > svg > g > text"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}`enter code here`
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
在当前状态下,页面会为单个用户保守地加载,一旦页面中的元素被加载,它就会移动到下一个页面,以此类推
# 1 楼答案
您需要将脚本链接到ApacheJMeter_core library。这将允许您访问JMeterContext类,该类通过JMeterVariables类提供对JMeter Variables的读/写访问
相关代码如下所示:
请参阅How to Use JUnit With JMeter文章,了解更多使用JUnit Request采样器的提示和技巧