如何在硒测试方法中为其传递“按”和“字符串”?

时间:2018-11-21 08:36:34

标签: java selenium selenium-webdriver

因此,我想为我的方法传递两件事,字符串-将是属性名称,而By-将允许选择要搜索的条件元素。简单的例子:

public static WebElement getElement(By by) {
    return driver.findElement(by);
}

但这会迫使我以这种方式使用它:

element = getElement(By.className(properties.getProperty("class")));

虽然我想这样使用它:

element = getElement(By.className, "class");

我该怎么做?我以为这样的简单代码可以工作,但是不幸的是它返回错误

  

“ by(String)未定义”

public static WebElement getElement(By by, String string) {
    return driver.findElement(by(properties.getProperty(string));
}

编辑: 我决定使用:

public static String useProperty(String propertyName) {
    return properties.getProperty(propertyName);
}

并不是我想要的那样,但是它确实可以简化和提高代码的可读性。

4 个答案:

答案 0 :(得分:6)

这是不可能的,By.className()是一种接收String参数的方法。该String用于初始化内部类By.ByClassName,这是返回的By

您可以构建switch case来处理此问题,例如

public static WebElement getElement(String string) {
    By by = null;
    String locator = properties.getProperty(string);

    switch (string) {
        case "class":
            by = By.className(locator);
            break;
        case "id":
            by = By.id(locator);
            break;
    }

    return driver.findElement(by);
}

答案 1 :(得分:0)

“ By”不能表示为字符串。还有另一个例子:

public void Test_xy() throws InterruptedException {     

        String[] by = {"id", "xpath"};
        String[] property_name = {"value", "title"};

        driver.get("https://www.example.com");
        Thread.sleep(5000);
        WebElement more_info = getElement(by[1], "/html/body/div/p[2]/a");
        String element_title = getProperty(more_info, property_name[1]);

    }

    public WebElement getElement(String by, String by_value) {
        if (by == "id") {WebElement element = driver.findElement(By.id(by_value));return element;}
        else if (by == "xpath") {WebElement element = driver.findElement(By.xpath(by_value));return element;}
        // else if (by == different locator) ...
        else {return null;}
    }

    public String getProperty(WebElement element, String chosen_property) {
        return element.getAttribute(chosen_property);
    }

答案 2 :(得分:0)

一种方法可能是使用泛型:

private <T extends By> WebElement getElement(Class<T> byType, String prop) {

    if (byType.equals(ByClassName.class))
        return getDriver().findElement(By.className(prop));
    else if (byType.equals(ByCssSelector.class))
        return getDriver().findElement(By.cssSelector(prop));
    else if (byType.equals(ById.class))
        return getDriver().findElement(By.id(prop));
    else if (byType.equals(ByLinkText.class))
        return getDriver().findElement(By.linkText(prop));
    else if (byType.equals(ByName.class))
        return getDriver().findElement(By.name(prop));
    else if (byType.equals(ByPartialLinkText.class))
        return getDriver().findElement(By.partialLinkText(prop));
    else if (byType.equals(ByTagName.class))
        return getDriver().findElement(By.tagName(prop));
    else if (byType.equals(ByXPath.class))
        return getDriver().findElement(By.xpath(prop));
    else
        throw new UnsupportedOperationException();
}

您将像这样使用它:

@Test
public void myWebElementTest() {
    WebElement div1 = getElement(By.ByName.class, "div1");
    WebElement id1 = getElement(By.ById.class, "id1");
}

答案 3 :(得分:0)

简单实现:

public WebElement getLocator(String locatorType, String locatorValue ){

    switch(locatorType.toUpperCase()) {
    case "ID":
        return driver.findElement(By.id(locatorValue ));
    case "CSSSELECTOR":
        return driver.findElement(By.cssSelector(locatorValue));
    case "XPATH":
        return driver.findElement(By.xpath(locatorValue));
    case "NAME":
        return driver.findElement(By.name(locatorValue));
    case "LINKTEXT":
        return driver.findElement(By.linkText(locatorValue));
    case "PARTIALLINKTEXT":
        return driver.findElement(By.partialLinkText(locatorValue);
    case "TAGNAME":
        return driver.findElement(By.tagName(locatorValue));
    case "CLASSNAME":
        return driver.findElement(By.className(locatorValue));
    default:
        LOGGER.info("Incorrect locator or Type" +  locatorType);
    }
}