【问题标题】:JUnit sampler not working properly with CSV data set Config for multiple Threads JmeterJUnit 采样器无法与多线程 Jmeter 的 CSV 数据集配置一起正常工作
【发布时间】:2018-04-10 16:47:16
【问题描述】:

我尝试使用 Junit Request Sampler 通过使用 CSV 数据集配置登录到多个用户的测试应用程序。例如:我将线程数设置为 2,并在 .csv 文件中设置两个用户登录详细信息,然后运行测试。结果是打开了两个firefox浏览器,一个浏览器登录成功,另一个没有在登录页面的用户名和密码字段中输入用户名和密码。这是我的硒脚本代码。请任何人都可以提出这个问题的原因吗?

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);
    }
}

【问题讨论】:

  • 你能分享一下你遇到了什么错误吗?
  • 我使用了两个 junit 请求采样器。在第一个 junit 请求采样器中,将类名称设置为 testClass,将 setUpBeforeClass 方法设置为测试方法。在第二个 junit 请求采样器中,将类名称设置为 testClass,将登录方法设置为测试方法,然后我运行线程组。结果是打开了两个firefox浏览器并成功加载了url。但是只有一个浏览器完全登录到应用程序成功,而另一个浏览器由于登录数据未插入用户名和密码字段而无法登录。这就是问题所在。

标签: java selenium jmeter


【解决方案1】:

您的问题是您正在初始化用户名和密码一次:

String userName = sampler.getThreadContext().getVariables().get("username");
String password = sampler.getThreadContext().getVariables().get("password");
String Empnamecsv = sampler.getThreadContext().getVariables().get("Empname");

在 JMeter 中,您可以测试 JUnit 类,但在这里您似乎是在 JUnit 测试类中创建它:

JUnitSampler 采样器 = new JUnitSampler();

所以有受孕问题。

如果您想使用 CSV,请创建一个使用 Webdriver Sampler 的测试计划并使用 JMeter assertions 进行检查。

在此处查看示例:

【讨论】:

  • 非常感谢您的回答。
  • 如果答案是好的,你应该接受它并投票。谢谢
【解决方案2】:

尝试将您的“登录”方法更新为如下所示:

@Test
public void login() throws InterruptedException {
    org.apache.jmeter.threads.JMeterVariables vars = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables();
    driver.findElement(By.id("txtusername")).sendKeys(vars.get(userName));
    driver.findElement(By.id("txtpassword")).sendKeys(vars.get(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, vars.get("Empnamecsv"));
}

参考资料:


还要注意 JMeter 和 Selenium 集成的替代方法:

  1. WebDriver Sampler - 带有 Selenium 客户端库和配置元素来设置浏览器实例。自动处理多线程执行
  2. JSR223 Sampler - 您可以使用它来代替 JUnit,这样您就不必重新编译代码来进行更改。假设您选择 Groovy 语言,性能将与 Java 几乎相同

【讨论】:

  • 您好,感谢您的回答。我尝试通过更新提到的“登录”方法。但是同样的问题又出现了。有时我看到 CSV 文件中的不同用户名同时粘贴在同一个打开的浏览器中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
  • 2013-07-12
  • 2011-10-29
相关资源
最近更新 更多