有没有办法将网络元素作为参数传递?

Is there a way to pass a webelement as a parameter?

我正在使用包含三个 ID 定位器的页面对象构建三个测试用例。我想知道是否可以将定位器传递给所有三种情况的单一方法。

页面对象:

public class SomeClass {

    @FindBy(how=How.CSS, using="label[for='yes']")
    private WebElement yes;

    @FindBy(how=How.CSS, using="label[for='maybe']")
    private WebElement maybe;

    @FindBy(how=How.CSS, using="label[for='no']")
    private WebElement no;

    public SomeClass(WebDriver driver) {
        super(driver);
    }

    public SomeClass clicksButtons() {
        *some locator*.click();
        return new someClass(this.driver);
    }
}

测试用例:

public class SomeTest {
    
    @Test
    public void willClickAButton() {
        
        SomeClass someClass = new SomeClass(this.getDriver());

        SomeClass.clicksButtons();

        Assert.assertTrue(true);
    }

我想将一个参数(是、可能或否)传递给 clicksButtons 方法,这样我就可以在其他两个测试用例中重用该方法,而不必对其进行硬编码。我搜索了 Google,但找不到明确的答案。

这个

public SomeClass clicksButtons() {
        *some locator*.click();
        return new someClass(this.driver);
    }

应该是:

public SomeClass clicksButtons(WebElemenet ele) {
        ele.click();
        return new someClass(this.driver);
    }

并像这样调用此方法:

SomeClass.clicksButtons(pass a webelement here);

我在 Selenium Testing Tools Cookbook 第 2 版中找到了答案。

    public void select(String label) {

//I found the buttons by class and stored them in a List<>. In my case there were 3.
        List<WebElement> like = this.driver.findElements(By.className("custom-control-label"));
        
//iterate the buttons contained in the List<>
        for(WebElement button : like) 
        {
//if the button's "for" attribute contains the given label...
            if (button.getAttribute("for").equals(label)) 
            {
//...and if it is not selected...
                if (!button.isSelected()) 
                {
//click the button, according to the label
                    button.click();
                }
            }
        }   
    }