我尝试运行相同的end2end测试并行(负载测试,JMeter与JUnitSampler)。 如果我用真正的chromeBrowser运行并行测试,它的工作正常。如果我使用chromeOption“--headless”运行测试,则不会运行任何测试。真正的chromeBrowser和无头chromeBrowser有什么区别? 使用headlessBrowser(非并行)进行的一项测试也运行良好。
Step1 - JUnit测试:
public class AppTest extends TestCase{
private Logger logger;
private WebDriver driver;
private String user;
private String passwd;
@Before
@Override
public void setUp() throws MalformedURLException {
logger = LoggerFactory.getLogger(AppTest.class);
user = “user”;
passwd = “12345”;
DesiredCapabilities caps = DesiredCapabilities.chrome();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments(“--headless”);
chromeOptions.addArguments(“--incognito”);
caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4444/wd/hub”), caps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test
public void workFlow() throws InterruptedException {
driver.get("https://somesite.de");
try {
WebElement input = driver.findElement(By.id(“login-form-username”));
input.click();
input.clear();
input.sendKeys(user);
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
Thread.sleep(1000);
WebElement myButton = driver.findElement(By.className("my-button”));
myButton.click();
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
Thread.sleep(2000);
WebElement input = driver.findElement(By.id(“password-form-password”));
input.click();
input.sendKeys(passwd);
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
WebElement myButton = driver.findElement(By.className(“my-button”));
myButton.click();
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
try {
Thread.sleep(2000);
List<WebElement> displayBoxHeaders = driver.findElements(By.className(“box-complete-header”));
assertThat(displayBoxHeaders.get(1).getText()).contains(user);
WebElement logo = displayBoxHeaders.get(0);
String svg = logo.findElement(By.tagName(“svg”)).findElement(By.tagName(“g”)).findElement(By.tagName(“path”)).getAttribute(“class”);
assertThat(svg).isNotNull().isNotEmpty();
} catch (NoSuchElementException e) {
fail();
}
try {
WebElement displayText = driver.findElement(By.id(“ui-form-text”));
String text = displayText.getText();
assertThat(text).isNotNull().isNotEmpty();
} catch (Exception e) {
fail();
}
try {
WebElement myButton = driver.findElement(By.className(my-button”));
myButton.click();
} catch (NoSuchElementException e) {
fail(“Element not found”);
}
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
Step2 - JMeter中的JUnitSampler,上面有JUnitTestClass。
Step3 - Docker中的SeleniumGrid
git clone https://github.com/SeleniumHQ/docker-selenium.git
cd docker-selenium
docker run -d -p 4444:4444 --name selenium-hub selenium/hub:3.5.3-boron
docker run -d --link selenium-hub:hub -e NODE_MAX_INSTANCES=100 -e NODE_MAX_SESSION=100 selenium/node-chrome:3.5.3-boron
Step3 - 在JMeter中运行单个线程: 测试成功运行。
Step4 - 在JMeter中并行运行5个线程:
第一次测试成功,其他四次没有。
错误:每次测试运行失败都是ElementNotFound
对于测试而言,浏览器实例没有相同的会话非常重要。