0
votes

I tried to use Junit Request Sampler for login to my test application with multiple users by using CSV data set config. Ex : I set number of thread count as 2 and set two user login details in .csv file and i run the test. The result was open the two firefox browsers and one browser logged successfully and other one is not get username and password into username and password fields in login page. This is my selenium script code. Please anyone can suggest the reason for this issue ?

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
Can you please share what error ur getting ? - Ankur Singh
I used two junit request samplers. in first junit request sampler set class Name as testClass and setUpBeforeClass method as Test Method. In second junit request sampler set class Name as testClass and login method as the Test Method and i run the thread group. The result was open two firefox browsers and load the url successfully. But only one browser logged to application success fully and other one fail to login due to login data not inserted into username and password field. That's the issue. - kanchana thisaru

2 Answers

0
votes

Try updating your "login" method to look like:

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

References:


Also be aware of alternative ways of JMeter and Selenium integration:

  1. WebDriver Sampler - comes with Selenium client libraries and Configuration Elements to set up the browser instance. Automatically handles multi-threaded execution
  2. JSR223 Sampler - you can use it instead of JUnit, this way you won't have to recompile the code to bring changes. Assuming you choose Groovy language the performance will be nearly the same as for Java
0
votes

Your issue is that you're initializing userName and password once:

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

In JMeter you can test JUnit classes but here it seems you're creating it in JUnit test class:

JUnitSampler sampler = new JUnitSampler();

So there is a conception problem.

If you want to use a CSV, then create a Test plan that uses a Webdriver Sampler and use JMeter assertions for your checks.

See an example here: