【问题标题】:How do i use both 'When' and 'And' for the same function without duplicating it我如何在不重复的情况下同时使用“When”和“And”来实现相同的功能
【发布时间】:2019-02-25 21:58:59
【问题描述】:

我正在重构我的脚本,目前正在执行以下操作;

When("I click the button {string}", (buttonID, next) => {

    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});

And("I click the button {string}", (buttonID, next) => {

    pageElement = driver.findElement(By.id(buttonID));
    driver.wait(until.elementIsVisible(pageElement), 10000);
    pageElement.click();
    next();
});

我这样做是出于可读性的目的,我有一个场景,其中“和”更具可读性,还有一个场景,“何时”更适用。

我已经看到了以下内容..

@Then("^(?:it's do something|it's do another thing)$");

这允许将多个场景名称用于同一功能,但遗憾的是,我正在寻找相反的名称。

任何帮助将不胜感激。

【问题讨论】:

    标签: selenium automation cucumber cucumberjs


    【解决方案1】:

    我们正在使用 Specflow,当我们需要使用与 WhenAnd 相同的步骤时,我们只需执行以下操作:

    [Given(@"I enter all required customer information")]
    [When(@"I enter all required customer information")]
    [Then(@"I enter all required customer information")]
    public void GivenIEnterAllRequiredCustomerInformation()
    {
       MyMethod();
    }
    

    因此,在您的情况下,请尝试以下操作:

    When("I click the button {string}", And("I click the button {string}", (buttonID, next) => {
        pageElement = driver.findElement(By.id(buttonID));
        driver.wait(until.elementIsVisible(pageElement), 10000);
        pageElement.click();
        next();
    });
    

    【讨论】:

      【解决方案2】:

      谢谢@IPoln​​ik,您的解决方案是正确的,除了一部分之外,您使用逗号分隔“时间”和“并且”,如下所示。

       When ("I click the button {string}", And ("i click the button {string}"), (buttonID, next) => {
      
          pageElement = driver.findElement(By.id(buttonID));
          driver.wait(until.elementIsVisible(pageElement), 10000);
          pageElement.click();
          next();
      });
      

      如果没有你的建议,我永远不会到达那里,所以非常感谢你,这也是我说你解决了我的问题的原因。

      祝你有美好的一天,杰克。

      编辑:我还发现这种语法也有效

         When ("I click the button {string}" | And ("i click the button {string}"), (buttonID, next) => {
      
          pageElement = driver.findElement(By.id(buttonID));
          driver.wait(until.elementIsVisible(pageElement), 10000);
          pageElement.click();
          next();
      });
      

      我相信这可能是更好的做法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 2012-02-10
        • 1970-01-01
        相关资源
        最近更新 更多