Java 如何通过;由「;硒测试方法中的字符串?

Java 如何通过;由「;硒测试方法中的字符串?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,所以我想为我的方法传递两件事,String-这将是一个属性名,By-这将允许选择应该搜索哪个条件元素。简单的例子: public static WebElement getElement(By by) { return driver.findElement(by); } 但这会迫使我这样使用它: element = getElement(By.className(properties.getProperty("class"))); 我想这样使用它: element = getElem

所以我想为我的方法传递两件事,String-这将是一个属性名,By-这将允许选择应该搜索哪个条件元素。简单的例子:

public static WebElement getElement(By by) {
    return driver.findElement(by);
}
但这会迫使我这样使用它:

element = getElement(By.className(properties.getProperty("class")));
我想这样使用它:

element = getElement(By.className, "class");
我该怎么做?我原以为这样简单的代码可以工作,但不幸的是,它返回了错误

“by(字符串)未定义”

编辑: 我决定使用:

public static String useProperty(String propertyName) {
    return properties.getProperty(propertyName);
}
这并不是我想要的处理方式,但它确实允许简化和提高代码的可读性。

这是不可能的,它是一个接收
字符串的方法。此
字符串
用于初始化内部类,这是
返回的

您可以构建
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);
}
这是不可能的,因为这是一个接收
字符串的方法。此
字符串
用于初始化内部类,这是
返回的

您可以构建
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);
}
“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);
    }
“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);
    }

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

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

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

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

简单实现:

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

简单实现:

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

@拉特米拉萨诺夫,谢谢。我看到了编辑,
开关
需要在
字符串
上,而不是
定位器
上。是的,对不起,这是我的错。谢谢,这是一个解决方案,但我想这不是最好的处理方法。“我想我会决定用其他方法来缩短它。”拉特米拉萨诺夫,谢谢。我看到了编辑,
开关
需要在
字符串
上,而不是
定位器
上。是的,对不起,这是我的错。谢谢,这是一个解决方案,但我想这不是最好的处理方法。我想我会决定用额外的方法来缩短它。