【问题标题】:pass an element name to another classes method将元素名称传递给另一个类方法
【发布时间】:2020-02-12 23:53:44
【问题描述】:

我有一个功能文件,我在其中将选项卡名称(如 Actions、Workorders、notifications 选项卡)传递给定义选项卡元素及其操作的 pageobjects 类。 如果该选项卡名称不可见,我们必须单击箭头按钮并单击该项目。我将该项目命名为 tabNameItem。不清楚如何在此方法中添加选项卡项名称。以下方法引发错误“然后从选项卡用户单击“accountWorkOrdersTab”#AccountsPageSteps.from_the_tabs_User_clicks_on_tab(WebElement) cucumber.runtime.CucumberException:不知道如何将“accountWorkOrdersTab”转换为 org.openqa.selenium.WebElement。尝试编写自己的转换器:" 调用方式:

@Then("^from the tabs User clicks on \"([^\"]*)\"$")
public void from_the_tabs_User_clicks_on_tab(WebElement tabname) throws Throwable {
    serviceCloudAccountsPageObject.openAccountsTabs(tabname);
}

动作类:

@FindBy(xpath = "//a[@text()='Work Order')]")
public WebElement accountWorkOrdersTab;

@FindBy(xpath = "//span[contains(.,'Work Order')]")
public WebElement accountWorkOrdersTabItem;

public void openAccountsTabs(WebElement tabName) {

    if(verificationHelper.isDisplayed(tabName)){
        tabName.click();
        System.out.println(tabName.getText() +" tab displayed & is clicked");
    }
    else {

        moreTab.click();
        accountWorkOrdersTabItem.click();
        System.out.println("WorkOrder tab not displayed & is clicked from More tab");

    }
}

}

【问题讨论】:

    标签: java selenium cucumber


    【解决方案1】:

    不要在功能文件中传递 WebElement,只需传递字符串。 from_the_tabs_User_clicks_on_tab(字符串标签名)

    基于此字符串值,您可以使用 case 语句并返回 webElement。

    public WebElement returnElement(String tabname) {
        WebElement returnElement = null;
    
    
        switch (elementName) {
        case "tabname1":
            returnElement = driver.findElement(By.xpath("//a[@text()='Work Order')]"));
            break;
        case "tabname2":
            returnElement = driver.findElement(By.xpath("//span[contains(.,'Wor Order')]"));;
            break;
    default:
            break;
        }
    
        return returnElement;
    }
    

    使用上面的代码,你应该得到所需的 web 元素,你可以在返回元素上执行你的操作。

    希望你明白了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多