开发者

waitForCondition gives missing ; before statement error

I've looked at all the examples and I am still have a problem using waitForCondition. Here is my code.

WebDriverBackedSelenium seleniumWD = new WebDriverBackedSelenium(driver, "http://www.x.com");

seleniumWD.waitForCondition("seleniumWD.isElementPresent(\"fullname\");", "5000");

I get the error: seleniumWD is not defined. So I changed it to:

WebDriverBackedSelenium seleniumWD = new WebDriverBackedSelenium(driver, "http://www.开发者_JAVA技巧x.com");

seleniumWD.waitForCondition("boolean ok = seleniumWD.isElementPresent(\"fullname\");", "5000");

And now I get the error: missing ; before statement


It seems that you are using the Selenium JS object from the Selenium 2/ WebDriver based test. Instead of using WebDriverBackedSelenium you should use ExpectedCondition and Wait classes provided by WebDriver. In your case, assuming that the fullname is the id of the element for which you are waiting, your code should look like this:

WebElement element;
ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
        element = d.findElement(By.id("fullname"));
        return Boolean.TRUE;
    }
};

Wait<WebDriver> w = new WebDriverWait(driver, timeOutInSeconds);
w.until(e);

This is a really big piece of code, so you should consider utilizing the Page Objects pattern which is a one of the best practices for writing Selenium tests. Example page which contains your field will be something like this:

public class MyPage {
    @FindBy(id="fullname")
    private WebElement fullName;

    public MyPage(WebDriver driver) {
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
    }

    public void setFullName(String value) {
        fullName.clear();
        fullName.sendKeys(value);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜