【问题标题】:selenium grid/cucumber and docker compose: run a parallel test on several nodesselenium grid/cucumber 和 docker compose:在多个节点上运行并行测试
【发布时间】: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 的用法修改为FireFoxOptionInterneExplorerOptions 来复制测试类,希望测试能够开始,但我有疑问。这会产生以下错误:

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


    【解决方案1】:

    我在 Java + testng + cucumber 的帮助下完成了类似的事情。 testng.xml 带有 3 个 <test> 标签和 <parameter> 并并行运行测试。例如,

    <suite name="parallel" parallel="tests" thread-count="5">
      <test name="chrome>
        <parameter name="browser" value="chrome"/>
        <classes>
          <class name="YOUR.CLASS.PATH"/>
        </classes>
      </test>
      <test name="firefox>
        <parameter name="browser" value="firefox"/>
        <classes>
          <class name="YOUR.CLASS.PATH"/>
        </classes>
      </test>
      <test name="edge>
        <parameter name="browser" value="edge"/>
        <classes>
          <class name="YOUR.CLASS.PATH"/>
        </classes>
      </test>
    

    根据browser 的值,您可以设置功能

    @Parameter("browser")
    @BeforeMethod
    public void beforeMethod(String browser) {
      if (browser.equals("chrome))
         // driver for chrome
      // and so on
    }
    

    【讨论】:

    • 非常感谢您的回答。同时我也使用了testNG,我现在正在尝试了解具有并行属性的线程设置。使用黄瓜/硒实现胶水代码时,您是否为每个类创建一个测试?
    • 不,实际上我只是通过适当的线程处理来管理它。仅当驱动程序的条件您还可以检查 Java 中的 ThreadLocal 概念来管理线程此播放列表清除了我对自动化线程的所有疑虑和困惑youtube.com/playlist?list=PL9ok7C7Yn9A_JZFMrhrgEwfqQGiuyvSkB
    • 谢谢,链接很棒
    • 没问题。很高兴为您提供帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    相关资源
    最近更新 更多