【发布时间】:2022-01-27 16:54:35
【问题描述】:
为了训练自己掌握上述技术,我创建了一个项目,其中包含一个测试,用于检查用户在 stackoverflow 网站主页上的导航。
硒网格的集线器和 3 个节点似乎配置正确 测试运行正常,我现在想在多个浏览器上同时运行此测试。
我的测试是这样写的: 黄瓜:
Feature: search the home page of stackoverflow
Scenario: Go to the site stackoverflow
Given I'm on google search page
When I enter the name of the site
Then I'm navigated on the home page of stackoverflow
硒:
public class StepGoStackoverflowChrome {
RemoteWebDriver driver;
String nodeUrl;
@Given("I'm on google search page")
public void i_m_on_google_search_page() {
try {
nodeUrl = "http://localhost:4444";
ChromeOptions options = new ChromeOptions();
// options.addArguments("--headless");
options.addArguments("start-maximized");
driver = new RemoteWebDriver(new URL(nodeUrl), options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.get("https://www.google.com");
} catch (MalformedURLException error) {
error.printStackTrace();
}
}
@When("I enter the name of the site")
public void i_enter_the_name_of_the_site() {
WebElement webElementList = driver.findElement(By.id("L2AGLb"));
webElementList.click();
driver.findElement(By.name("q")).sendKeys("stackoverflow", Keys.ENTER);
}
@Then("I'm navigated on the home page of stackoverflow")
public void i_m_navigated_on_the_home_page_of_stackoverflow() {
driver.findElement(By.xpath("//a[@href='https://stackoverflow.com/']")).click();
driver.close();
}
}
最后,docker 文件包含启动 3 个容器,其中包含 web 驱动程序 chrome、firefox 和 edge 的图像:
version: "3"
services:
chrome:
image: selenium/node-chrome:4.1.1-20211217
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_MAX_SESSIONS=2
edge:
image: selenium/node-edge:4.1.1-20211217
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_MAX_SESSIONS=3
firefox:
image: selenium/node-firefox:4.1.1-20211217
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_MAX_SESSIONS=3
selenium-hub:
image: selenium/hub:4.1.1-20211217
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
我终于用命令运行了 docker compose:
docker-compose -f docker-compose.yml up
我通过将ChromeOptions 的用法修改为FireFoxOption 和InterneExplorerOptions 来复制测试类,希望测试能够开始,但我有疑问。这会产生以下错误:
Exception in thread "main" io.cucumber.core.exception.CucumberException:
io.cucumber.core.runner.DuplicateStepDefinitionException: Duplicate step definitions in com.seleniumtest.formation.demo.cucumber.steps.StepGoStackoverflowChrome.i_m_on_google_search_page() and com.seleniumtest.formation.demo.cucumber.steps.StepGoStackoverflowFirefox.i_m_on_google_search_page()
我知道这会在 Cucumber 测试步骤的定义中造成语言上的歧义。你有教程或想法让我考虑。 谢谢
【问题讨论】:
标签: docker selenium docker-compose cucumber selenium-grid