Java 如何动态使用Selenium';s@FindBy?

Java 如何动态使用Selenium';s@FindBy?,java,selenium,webdriver,cucumber,Java,Selenium,Webdriver,Cucumber,我要把硒和黄瓜一起跑过去。下面是黄瓜的场景和概要 @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

我要把硒和黄瓜一起跑过去。下面是黄瓜的场景和概要

@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 |

我不知道如何使用部件设置
。问题是
Id
1到4不等

这些步骤将添加到definitions类中,您将在那里引用
ID
。由于您将四个值指定为
ID
,因此
@tag2
场景将运行四次,每次运行时,将相应地获取
ID
的值。即,第一次运行时,
ID
中的值将为1,第二次运行时,
ID
中的值将为2,依此类推。

无法执行此操作。
@FindBy
注释要求
using
为字符串常量,无法动态设置

为什么不在
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为标识符设置动态值,因为它将
常量字符串
作为值

但是,如果您真的要实现这一点,您可以在步骤中创建元素,并单击它,如下所述

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 |

那么,POM概念中的解决方案是什么呢?@Salman定义了所有四个复选框,只测试当前场景大纲中使用的一个复选框。也许可以,但应该有一个更简单的解决方案solution@Salman使用CheckBoxPage,您正在为静态页面建模,为什么您如此迫切地想要动态定义它?如果有人使用不存在的id更新您的方案,它还将呈现一个包含无法找到的复选框元素的页面。不建议在方案中包含id等技术详细信息。您最好使用该值,使其更清晰。使用driver.findElement并向其传递动态生成的xpath,而不是findby注释。。。
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();
    }