简体   繁体   中英

Is there a way, to concatenate a variable with By. for Selenium Webdriver In Java

I am looking around for a solution in Java - Selenium Webdriver.. I created a function

public WebElement waitforElementCss(String locator)
{
    WebDriverWait wait = new WebDriverWait(driver,10);

    return wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locator)));
}

What I want to do is, make it dynamic like this

public WebElement waitforElementCss(String type, String locator)
{
    WebDriverWait wait = new WebDriverWait(driver,10);

    return wait.until(ExpectedConditions.elementToBeClickable(By.type(locator)));
}

So instead of calling everytime By.CssSelector, Xpath.... I want it to get the parameter whereever I call from...I had did that in python but for some reason In Java I am not able to do it..

You don't need to be using string 's for this:

public WebElement waitforElement(By locator)
{
    WebDriverWait wait = new WebDriverWait(driver,10);

    return wait.until(ExpectedConditions.elementToBeClickable(locator));
}   

By is already an abstraction over locating mechanisms. You do not need to be making them even more generic by using string 's, using the above method like so:

waitForElement(By.cssSelector("something"));
waitForElement(By.id("something"));
waitForElement(By.xpath("something"));

instead of:

waitForElement("xpath", "something");
waitForElement("id", "something");
waitForElement("css", "something");

Better, no? Less likely you'll get manual mistakes if I misspell "xpath", for instance. Also uses inbuilt framework classes, so you aren't duplicating work.

With some reflection you could try something like :

public static <E> WebElement waitForElement(Class<E> byClass, String locator) 
        throws Exception {
    WebDriverWait wait = new WebDriverWait(driver,10);
    By byObject = (By) byClass.getConstructor(String.class).newInstance(locator);
    return wait.until(ExpectedConditions.elementToBeClickable(byObject);
}

Then call it :

WebElement cssElem = waitForElement(By.ByCssSelector.class, "something");
WebElement otherElem = waitForElement(By.ById.class, "someId");

As first parameter, you could use subclasses of By .

I haven't tried it, but it should work.

Why not use a simple if else or switch ?

public WebElement waitforElementCss(String type, String locator) {
    WebDriverWait wait = new WebDriverWait(driver,10);
    WebElement element = null;
    if (type.equals("id")) {
        wait.until(ExpectedConditions.elementToBeClickable(By.id(locator)));
    } else if (type.equals("name")) {
        wait.until(ExpectedConditions.elementToBeClickable(By.name(locator)));
    } else if (type.equals("css")) {
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locator)));
    }
    return element;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM