【问题标题】:Is there a way to pass a webelement as a parameter?有没有办法将 web 元素作为参数传递?
【发布时间】:2022-01-07 06:42:32
【问题描述】:

我正在使用包含三个 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 上搜索过,但没有找到明确的答案。

【问题讨论】:

  • WebElement yes 的范围是私有的,其他类或包不能使用它们。如果SomeClassSomeTest 都在类似的包中,请使用受保护的范围。
  • 我做出了改变。
  • 两个类在同一个包还是不同的包?
  • 它们在同一个包中。
  • 好的,可以分享一下clickButtons方法代码吗?

标签: java selenium parameters findby


【解决方案1】:

这个

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);

【讨论】:

    【解决方案2】:

    我在 Selenium 测试工具手册第 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();
                    }
                }
            }   
        }
    

    【讨论】:

      猜你喜欢
      • 2017-07-10
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多