2012-05-09 55 views
1

我正在使用WebDriver(Eclipse -Java)来自动化注册页面。点击“注册”按钮后,会显示“成功消息”并需要验证。等待在Firefox浏览器中不工作(Webdriver selenium 2.0 + Java)

我可以在IE8中成功地做到这一点。但无法在Firefox中验证。我试过不同的等待 1. d1.manage()。timeouts()。implicitlyWait(60,TimeUnit.SECONDS);

  1. WebDriverWait wait = new WebDriverWait(driver,10); wait.withTimeout(30,TimeUnit.SECONDS); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id( “ElmId”));

  2. 等待等待=新FluentWait等待=新FluentWait(D1).withTimeout(60,秒); wait.until(新()函数 wait.until(ExpectedConditions.visibilityOf(d1.findElement(By.id( “elementid”))));

有没有人遇到过类似问题的任何解决方案

回答

0

也许你?可以尝试与其他条件类型?或者你也可以尝试通过覆盖申请方法来写自己的在使用所提供的条件的情况下少数情况下是不够的。只有在使用我自己的应用方法版本之后,它才是成功的。

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeoutInSeconds, TimeUnit.SECONDS) 
     .pollingEvery(pollingInterval, 
      TimeUnit.MILLISECONDS); 
    return wait.until(new ExpectedCondition<WebElement>() { 

     @Override 
     public WebElement apply(WebDriver arg0) { 
      List<WebElement> findElements = driver.findElements(By.className(someClassName)); 
      for (WebElement webElement : findElements) { 
       if (webElement.getText().equals(string)) { 
        return webElement; 
       } 
      } 
      return null; 
     } 
    }); 

e。 G。像这样的东西几次相当有用。

0

单击按钮后,此“成功消息”:是否显示ajax/javascript或重新加载页面?

如果您使用js进行操作,有时可能无法使用WebDriver命令验证消息,您还需要使用js进行验证。例如:

Object successMessage = null; 
int counter = 0; 

    while ((successMessage == null) && counter < 5) 
    { 
     try 
     { 
      ((JavascriptExecutor)driver).executeScript("return document.getElementById('yourId')"); 
     } 
     catch (Exception e) 
     { 
      counter +=1; 
     } 
    } 

    if (successMessage != null) //would be better to use some assertion instead of conditional statement 
    { 
     //OK 
    } 
    else 
    { 
     //throw exception 
    } 

while循环对于伪等待功能来说是丑陋的方式。如果您不需要等待元素,则可以将其删除。

替代可以是

Object result = ((JavascriptExecutor)driver).executeScript("return document.body.innerHtml"); 
String html = result.toString(); 

,然后手动解析HTML。

相关问题