我尝试使用Junit Request Sampler通过使用CSV数据集配置登录我的多个用户的测试应用程序 . 例如:我将线程数设置为2,并在.csv文件中设置两个用户登录详细信息,然后运行测试 . 结果打开了两个firefox浏览器,一个浏览器成功登录,另一个没有获取用户名和密码到登录页面的用户名和密码字段 . 这是我的selenium脚本代码 . 请有人能说出这个问题的原因吗?
import org.apache.jmeter.protocol.java.sampler.JUnitSampler;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class testClass {
static WebDriver driver;
JUnitSampler sampler = new JUnitSampler();
String userName = sampler.getThreadContext().getVariables().get("username");
String password = sampler.getThreadContext().getVariables().get("password");
String Empnamecsv = sampler.getThreadContext().getVariables().get("Empname");
@BeforeClass
public static void setUpBeforeClass() throws Exception
{
System.setProperty("webdriver.gecko.driver", "D:\\Automation\\Geckodriver\\V0.19.0\\geckodriver.exe");
driver = new FirefoxDriver();
}
@Test
public void loadHomePage() throws InterruptedException
{
driver.get("http://localhost/testWeb");
Thread.sleep(1000);
}
@Test
public void login() throws InterruptedException
{
driver.findElement(By.id("txtusername")).sendKeys(userName);
driver.findElement(By.id("txtpassword")).sendKeys(password);
driver.findElement(By.id("btnsubmit")).click();
Thread.sleep(1000);
String name = driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[3]/span[1]/span[1]")).getText();
Assert.assertEquals(name,namecsv);
}
}
2 回答
尝试更新“登录”方法,如下所示:
参考文献:
JMeterContextService
JMeterContext
JMeterVariables
还要注意JMeter和Selenium集成的替代方法:
WebDriver Sampler - 附带Selenium客户端库和配置元素以设置浏览器实例 . 自动处理多线程执行
JSR223 Sampler - 您可以使用它而不是JUnit,这样您就不必重新编译代码来进行更改 . 假设您选择Groovy语言,性能几乎与Java相同
您的问题是您正在初始化userName和密码一次:
在JMeter中,您可以测试JUnit类,但在这里,您似乎是在JUnit测试类中创建它:
所以有一个概念问题 .
如果要使用CSV,请创建使用Webdriver Sampler的测试计划并使用JMeter assertions进行检查 .
在这里查看示例: