有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java参数化selenium junit中的一个变量,以加载测试在jmeter上运行的网站

我需要加载测试一个执行XHR对象的网站。 正常的负载测试在这种情况下不适用,因为负载测试寻找的是服务器端,而我对客户端感兴趣

我唯一需要参数化的是用户名,以使脚本同时运行

我确实使用了Katalon Recorder以JUnit的形式获取自动化代码,然后将其输入selenium,然后将其导出为jar文件,并在Jmeter中使用

该代码适用于单个用户,但我想不出一种方法来让脚本同时运行

包装仪表板

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Testpage {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
        System.setProperty("webdriver.chrome.driver", "Path/To/chromedriver.exe");

      WebDriver driver = new ChromeDriver(options);


    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testDashboard() throws Exception {
    driver.get("https://test.com/newlogin");
    driver.findElement(By.id("UserName")).clear();
    driver.findElement(By.id("UserName")).sendKeys("Username");
    driver.findElement(By.id("Password")).clear();
    driver.findElement(By.id("Password")).sendKeys("Password!");
    driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='LOGIN'])[1]/following::input[3]")).click();
    driver.get("https://test.com");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Italy'])[3]/following::td[4]"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.get("https://test.com/1");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Guest_Arrivals_CY'])[2]/following::span[1]"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.get("https://test.com/2");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Nov 18'])[1]/following::div[4]"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.get("https://test.com/3");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.cssSelector("#kSXbjj > svg"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

    driver.get("https://test.com/4");
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.cssSelector("#tGPUB > svg > g > text"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }

  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }`enter code here`
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

在当前状态下,页面会为单个用户保守地加载,一旦页面中的元素被加载,它就会移动到下一个页面,以此类推


共 (1) 个答案