首页 > 解决方案 > 如何一次使用 5 个登录来执行登录功能的负载测试

问题描述

public class Login {

public WebDriver driver ;

@Test(invocationCount = 20, threadPoolSize = 5)
public void GmailLogin() throws InterruptedException {
WebDriver driver = LoadTest.getInstance().getDriver();  
driver.get("https://tst-oec-ebooking.azurewebsites.net/");    
driver.findElement(By.xpath("//html/body/div/div/div[2]/div/form/div/input")).sendKeys("mad@dayrep.com");
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys("Pass@123");
driver.findElement(By.xpath("//*[@id=\"login_submit\"]")).click();
Thread.sleep(1500);
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);

}

@BeforeMethod
public void setup(){
  System.setProperty("webdriver.gecko.driver", "D:\\CIPL0564\\D Drive\\Software\\geckodriver-v0.20.1-win64\\geckodriver.exe");

}

@AfterMethod
public void tearDown(){
LoadTest.getInstance().removeDriver();
}
}

通过执行上述代码,应用程序以相同的登录名运行 20 次。但我每次需要使用 5 个登录凭据运行应用程序,如果可能的话,还需要使用不同的浏览器。请建议我需要做的修改。

标签: javaseleniumload-testing

解决方案


您可以尝试实现一个静态方法来获取用户名/密码(这只是pseudo code

public class CredentialHelper {
private static int counter = 0;
private static List<MyGmailCredential> myGmailCredentials = [5 credentials];
public static synchronized MyGmailCredential getCredential() {
    MyGmailCredential currentCred = CredentialHelper.myGmailCredentials[counter%5];
    CredentialHelper.counter++;
    return currenCred;
}
}

在你的课堂上使用

public class Login {

public WebDriver driver ;

@Test(invocationCount = 20, threadPoolSize = 5)
public void GmailLogin() throws InterruptedException {
WebDriver driver = LoadTest.getInstance().getDriver();  
driver.get("https://tst-oec-ebooking.azurewebsites.net/");    
MyGmailCredential myGmailCredential = CredentialHelper.getCredential();
driver.findElement(By.xpath("//html/body/div/div/div[2]/div/form/div/input")).sendKeys(myGmailCredential.getUserName());
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\"Password\"]")).sendKeys(myGmailCredential.getPassword());
driver.findElement(By.xpath("//*[@id=\"login_submit\"]")).click();
Thread.sleep(1500);
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);

}

@BeforeMethod
public void setup(){
  System.setProperty("webdriver.gecko.driver", "D:\\CIPL0564\\D Drive\\Software\\geckodriver-v0.20.1-win64\\geckodriver.exe");

}

@AfterMethod
public void tearDown(){
LoadTest.getInstance().removeDriver();
}
}

推荐阅读