繁体   English   中英

如何动态使用Selenium的@FindBy?

[英]How do I dynamically use Selenium's @FindBy?

我要在此页面上用硒和黄瓜做硒。 这是黄瓜方案和大纲

@tag
Feature: Check the checkbox
  Webdriver should be able to check the checkbox lists

  @tag1
  Scenario: Check the checkbox
    Given I am in checkbox task page 
    When I can check the hobbies from the checkbox
    And I click on "Next Task"
    Then It navigates to the next task

  @tag2
  Scenario Outline: Title of your scenario outline
    Given I am in "http://suvian.in/selenium/1.6checkbox.html"
    When I can check the <id> from the checkbox
    And I click on "Next Task"
    Then It navigates to the "http://suvian.in/selenium/1.7button.html"

    Examples: 
       | id |
       |  1 |
       |  2 |
       |  3 |
       |  4 |

然后在POM(页面对象模型)中,复选框的ID为checkId 我有以下几点:

public class CheckBoxPage {

    @FindBy(how=How.ID, using="????")
    public WebElement checkId;
}

我不知道该如何设置using部分。 问题是Id1 to 4不等。

不能做 @FindBy批注期望using的是String常量,无法动态设置。

为什么不只在CheckBoxPage定义所有四个复选框? 似乎页面上的复选框数量不会改变。

public class CheckBoxPage {

    @FindBy(how= How.ID, using="1")
    private WebElement singingCheckbox;

    @FindBy(how= How.ID, using="2")
    private WebElement dancingCheckbox;

    @FindBy(how= How.ID, using="3")
    private WebElement sportsCheckbox;

    @FindBy(how= How.ID, using="4")
    private WebElement gamingCheckbox;
}

您无法使用PageFactory为标识符设置动态值,因为它将constant String作为值

但是,如果确实要实现此目的,则可以在步骤内创建元素,然后按如下所述单击它。

Feature: Check the checkbox
  Scenario Outline: Click all Check Boxes
  Given I am in "http://suvian.in/selenium/1.6checkbox.html"
  Then It should click <id> checkbox
    Examples:
    | id |
    |  1 |
    |  2 |
    |  3 |
    |  4 |

步骤定义

   @Given("^I am in \"([^\"]*)\"$")
    public void i_am_in(String arg1) throws Throwable {
        driver.get(arg1);
    }
    @Then("^It should click (\\d+) checkbox$")
    public void it_should_click_checkbox(int arg1) throws Throwable {
        driver.findElement(By.id(Integer.toString(arg1))).click();
    }

这些步骤将添加到定义类中,并且您将在其中引用ID 由于您将四个值指定为ID ,因此@tag2方案将运行四次,并且每次运行时都会相应地获取ID的值。 即,第一次运行时, ID的值将为1,其次ID值将为2,依此类推。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM