当前位置: 首页 > 知识库问答 >
问题:

带有不同按钮的cucumber场景大纲

卢树
2023-03-14
// --- feature file ---
Scenario Outline: click any button other than button1 and display label1
    When i click "<buttontype>"
    Then i am presented with label1
      Examples:
        | buttontype        |
        | button2           |
        | button3           |
        | button4           |

// --- Page Object ---
public class ButtonsObjects {

    public ButtonsObjects(AppiumDriver driver) {
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }
    // ------------------ locators ------------------

    @AndroidFindBy(id = "btn1")
    public MobileElement button1;

    @AndroidFindBy(id = "btn2")
    public MobileElement button2;

    @AndroidFindBy(id = "btn3")
    public MobileElement button3;

    @AndroidFindBy(id = "btn4")
    public MobileElement button4;

    // ------------------ actions ------------------
    public void clickButton1() {
      button1.click;
    }

    public void clickButton2() {
      button2.click;
    }

    public void clickButton3() {
      button3.click;
    }

    public void clickButton4() {
      button4.click;
    }

}

// --- step definition ---
@When("^i click \"([^\"]*)\"$")
    public void i_click(String arg0) {
        // How do I handle this here?
    }

共有1个答案

仲孙俊贤
2023-03-14

使用一个方法来丰富您的PageObject,该方法将按钮名称作为输入并单击相应的按钮怎么样?

public void clickOnButton(String buttonName) {
  // Implement as you wish (if/switch/reflection...)
}

然后在你的cucumber步骤中,你可以依靠以下方法

@When("i click {string}")
public void i_click(String buttonName) {
  pageObject.clickOnButton(buttonName);
}

注意:我在步骤注释中使用了Cucumber表达式,这更容易(从Cucumber5.x开始使用)

 类似资料: