我正在使用Selenium 2.37.1和Chrome Driver 2.7(在Ubuntu 13.10上使用Chrome 31.0.1650.63)为基于Java / Spring / Boostrap的网站编写一些测试。
测试单击“登录”文本,然后在单击“提交”按钮之前尝试将提供的用户名和密码输入表单。我将元素的发现分解开来,并调用sendKeys以确定Selenium跟随的位置(位于username.sendKeys
)
public void login(final String user) {
webDriver.findElement(By.id("login")).click();
final WebElement loginForm = webDriver.findElement(By.id("loginForm"));
final WebElement username = loginForm.findElement(By.id("username"));
username.sendKeys(user);
final WebElement password = loginForm.findElement(By.id("password"));
password.sendKeys(dslConstants.PASSWORD);
loginForm.submit();
}
用户名被成功找到,但是当我运行username.isDisplayed()
时实际上正在显示它。
我假设这与Selenium无法正确处理Bootstrap弹出窗口有关,并且想知道是否有人对此进行了修复?
提前致谢。
编辑:loginForm.isDisplayed()
调试时可见时,也返回false。我还尝试添加等待条件,但这也不能解决问题。
final WebDriverWait wait = new WebDriverWait(webDriver, TIME_OUT_IN_SECONDS);
wait.until(ExpectedConditions.visibilityOf(webDriver.findElement(By.id("username"))));
Edit2:这是页面的HTML / JSTL方面。
<div class="navbar">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<sec:authorize ifAnyGranted="ROLE_ANONYMOUS">
<div class="hide" id="login-popover">
<form role="form" id="loginForm" method="post" action="j_spring_security_check">
<div class="control-group">
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" class="form-control" name="j_username" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" class="form-control" name="j_password" />
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-primary" type="submit">Login</button>
</div>
</div>
</form>
</div>
<li><a href="#" id="login">Login</a></li>
<li><a href="/accounts/register">Register</a></li>
</sec:authorize>
</ul>
</div>
编辑3:使用FireFox 25时也会发生
我设法解决了这个问题。在调试和检查网页时(使用Chrome),我注意到实际上有两种登录表单。
我可以通过在另一个文件中(或在单击“登录”链接时执行的Javascript中)声明登录表单html来解决此问题。无论哪种方式,通过执行webDriver.findElement(By.class(“ popover-content”))然后在其中搜索表单都可以解决我的问题。
public void login(final String user) {
webDriver.findElement(By.id("login")).click();
final WebElement loginForm = webDriver.findElement(By.class("popover-content)).findElement(By.id("loginForm"));
loginForm.findElement(By.id("username")).sendKeys(user);
loginForm.findElement(By.id("password"))password.sendKeys(dslConstants.PASSWORD);
loginForm.submit();
}
我想从中得到的教训是,我回顾了在Web应用程序中使用的库的实际工作方式!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句