当前位置: 首页 > 知识库问答 >
问题:

JUnit采样器无法正常使用CSV数据集配置多个线程Jeter

柳胜
2023-03-14

我尝试使用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个答案

孟韬
2023-03-14

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

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

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

JUnit sampler sampler = new JUnit sampler();

所以有一个概念问题。

如果您想使用CSV,那么创建一个使用Webdriver采样器的测试计划,并使用JMeter断言进行检查。

请看这里的例子:

    < Li > http://www . ubik-ingenierie . com/blog/leverage-your-load-testing-using-jmeter-and-selenium-web driver/
郁明诚
2023-03-14

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

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

参考资料:

  • JMeterContextService
  • JMeterContext
  • JMeter变量

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

  1. Web驱动程序采样器 - 附带Selenium客户端库和配置元素来设置浏览器实例。自动处理多线程执行
  2. JSR223 采样器 - 您可以使用它来代替 JUnit,这样您就不必重新编译代码来进行更改。假设你选择了格鲁维语言,那么性能将与Java几乎相同
 类似资料:
  • 我试图在JMeter中加载我的CSV文件,但我一直得到下面的错误。我将文件结构设置为 c: \claimstest\claimsload.jmx C:\索赔stest\policies.csv

  • 如果我在IntelliJ IDE中测试这一点,只需将writeLine方法包装到main方法中,就可以正常工作 我的问题是:groovy sampler是否理解Java(在我看到的一篇文章中提到groovy理解99%的Java语法)。曾经有人成功地使用JMeter将数据写入csv文件吗?

  • 我将有n个(不是固定的文件数)数量的csv文件,这些文件应该作为输入传递给http采样器请求。计划是所有线程都应该从csv file1和file2读取数据...直到文件。不应为任何线程读取任何重复的数据。 目前,我有一个jmeter脚本可以从一个csv文件中读取数据,它工作得很好,所以必须将其扩展到从多个csv文件中读取数据。

  • 我想将JMeter的CSV数据集配置中提到的CSV文件中的数据使用到我的JUnit测试用例程序中,并将该数据传递给测试方法。 我知道如何从 HTTP 请求的 CSV 数据集配置中获取数据,但找不到有关 JUnit 请求的任何帮助。 我的测试方法是: 我不熟悉JUnit和JMeter。对此,任何帮助都将不胜感激。谢了。

  • 我想知道,如何在groovy脚本中对a做出反应。我正在使用While控制器迭代CSV中的所有行,并在实际测试计划之前生成JMeter变量。对于不同的CSV文件,我需要多次执行此操作,因此我不想在While控制器中停止线程。 我想象了这样的事情: CSV_VALUE1是JMeter变量的值,CSV_VALUE2是变量的名称。 测试计划 我也很欣赏更好的解决方案,它遍历CSV文件的每一行,并根据我的约

  • < li >我正在对多个并发用户执行登录测试。 < ol start="2 " > < li >我创建了一个JDBC请求,以从患者表中获取用户名和密码。然后创建了一个测试。借助BeanShell断言使用相同数据的CSV文件。 < li >现在在CSV数据集configTest.csv中传递此文件名。 < li >我可以同时登录多个用户,但面临一个问题:当我第一次运行测试时。该文件在同一位置不可用,