我的Web应用程序具有在MouseOver上打开的菜单.我正在使用htmlunitdriver编写测试.
My web application has menus that open on MouseOver. I'm writing tests using htmlunitdriver.
触发菜单的测试代码为
Actions builder = new Actions(driver); WebElement menu = driver.findElement(By.xpath("//a[starts-with(@href,'/index.html')]")); Thread.sleep(2000); builder.moveToElement(menu).build().perform(); Thread.sleep(2000); driver.findElement(By.xpath("//a[starts-with(@href,'/submenuitem')]")).click(); driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);当我运行一个测试时,它就可以通过了.但是当我尝试一次运行所有80个测试时,我得到了
When I run a single test, it passes just fine. But when I try to run all my 80 tests at once, I get
无法使用//a [starts-with(@href,'/submenuitem'
unable to locate node using //a[starts-with(@href,'/submenuitem'
我猜子菜单尚未打开,htmlunitdriver速度太快. 您可能只与可见元素进行交互,也可能在单次运行中发生.有人可以帮助我解决此问题吗?使用FirefoxDriver大约不是我的选择.
I guess the submenu is not yet open, htmlunitdriver has too much speed. Somethimes a "You may only interact with elements that are visible is occured on single runs too. Can someone help me fix this issue? Using FirefoxDriver or so is not an option for me.
推荐答案使用手动Thread.sleep(time)等待硒操作是一种肮脏的解决方案,根本不应该使用.
Using a manual Thread.sleep(time) to wait for selenium actions is a dirty solution and should not be used at all.
相反,您可以在与元素进行交互之前检查该元素是否可见.
Instead you could run a check is the element is visible before interacting with it.
public void waitUntilVisible(WebDriver driver, WebElement element){ WebDriverWait waiting = new WebDriverWait(driver, 10); waiting.until(ExpectedConditions.visibilityOf(element)); } public void waitUntilClickable(WebDriver driver, By locator){ WebDriverWait waiting = new WebDriverWait(driver, 10); waiting.until(ExpectedConditions.elementToBeClickable(locator)); } Actions builder = new Actions(driver); WebElement menu = driver.findElement(By.xpath("//a[starts-with(@href,'/index.html')]")); waitUntilVisible(driver, menu); builder.moveToElement(menu).build().perform(); WebElement menuItem = driver.findElement(By.xpath("//a[starts-with(@href,'/submenuitem')]")); waitUntilClickable(driver, By.xpath("//a[starts-with(@href,'/submenuitem')]")); menuItem.click();更多推荐
动作和htmlunitdriver
发布评论