使用 WebDriver、Junit 4.11、Maven、IntelliJ v.13
我试图找到编写此测试的正确方法,即验证一旦单击某个元素,它就会将我带到下一页。
研究了 WaitTool 之后,我尝试使 implicitlyWait()
无效,然后执行 WebDriverWait()
,然后重置 implicitlyWait()
我收到初始化错误。
最终,我的主要目标是通过使用新的 WaitTool 来解决我遇到的同步问题。
我遇到的问题是,当我单击按钮时,我偶尔会在等待元素时收到错误new WebDriverWait(chrome, 30).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.listContainer) ")));
出现在下一页。
这是我执行测试时的代码:
@Test
public void selectBlankProject(){
new WebDriverWait(chrome, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[1]")));
WebElement item1 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[1]"));
WebElement item2 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[2]"));
WebElement item3 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/header/span"));
WebElement item4 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]"));
Actions click = new Actions(chrome);
click.moveToElement(item1).moveToElement(item2).moveToElement(item3).moveToElement(item4).click().build().perform();
System.out.println("Blank Project has been selected");
}
@Test
public void dragVideoCompoenentOnToTheCanvas(){
chrome.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
new WebDriverWait(chrome, 30).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.listContainer")));
Actions dragAndDrop = new Actions(chrome);
//Dragging the video component onto the canvas
WebElement listContainerVideo = chrome.findElement(By.cssSelector("div.listContainer"));
WebElement componentListVideo = chrome.findElement(By.cssSelector("ul.componentList.j-componentList"));
WebElement videoComponent = chrome.findElement(By.cssSelector("li.componentItem.ui-draggable[data-id=\"c5\"]"));
WebElement componentThumbVideo = chrome.findElement(By.cssSelector("div.componentThumb"));
WebElement componentNameVideo = chrome.findElement(By.cssSelector("div.componentName.f-feature-A"));
//finds the canvas to drop the video component onto
WebElement canvas = chrome.findElement(By.cssSelector("div#page-c3"));
dragAndDrop.clickAndHold(videoComponent)
.moveToElement(listContainerVideo)
.moveToElement(componentListVideo)
.moveToElement(componentThumbVideo)
.moveToElement(componentNameVideo)
.release(canvas).perform();
WebElement draggableVideoComponent = chrome.findElement(By.cssSelector("div.t-component-A.videoComponent.component.draggableComponent.ui-draggable.layerSelected"));
Assert.assertEquals("video", draggableVideoComponent.getAttribute("data-type"));
}
我在第二个测试中添加了 chrome.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
,然后意识到这也在设置 WebDriverWait()
。因此,我遇到了 WaitTool
,我尝试使用它来看看它是否能解决我的同步问题。
引用这里:WaitTool
但是,这给我带来了更多问题。当我尝试使用以下代码时,我收到初始化错误:
@Test
public int dragClickAreaComponentToStage(int element){
try{
chrome.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
new WebDriverWait(chrome, 30).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("li.componentItem.ui-draggable[data-id=\"c3\"]")));
chrome.manage().timeouts().implicitlyWait(DEFAULT_WAIT_FOR_PAGE, TimeUnit.SECONDS);
return element;
}catch (StaleElementReferenceException e){
e.printStackTrace();
}
WebElement clickArea = chrome.findElement(By.cssSelector("li.componentItem.ui-draggable[data-id=\"c3\"]"));
WebElement arrowHead = chrome.findElement(By.cssSelector("div.componentThumb"));
WebElement imageHolderClickArea = chrome.findElement(By.cssSelector("div.imageHolder"));
WebElement componentNameClickArea = chrome.findElement(By.cssSelector("div.componentName.f-feature-A"));
WebElement canvas = chrome.findElement(By.cssSelector("div.j-page.t-page-A[id=\"page-c3\"]"));
Actions click = new Actions(chrome);
click.clickAndHold(clickArea).moveToElement(arrowHead).moveToElement(imageHolderClickArea).moveToElement(componentNameClickArea).moveToElement(canvas).release();
click.perform();
//checking that the draggable click area component has a data-type value as clickArea
WebElement clickAreaComp = chrome.findElement(By.cssSelector("div.t-componentImg-A.component.clickAreaComponent.draggableComponent.ui-draggable"));
Assert.assertEquals("clickArea", clickAreaComp.getAttribute("data-type"));
return element;
}
所以在堆栈跟踪中我得到了
java.lang.Exception:方法 cDragClickAreaComponentToStage() 应该为 void
和
java.lang.Exception:方法 cDragClickAreaComponentToStage 不应有参数
好的,排队“那么,你的问题是什么?”。
首先,我想知道在哪里设置我的 @Test
public int。通常我会把它声明为 void,但在这种情况下我想返回
一些必须是int
的东西,对吗?
其次,我想知道当尝试等待元素时同步问题出了什么问题。
最佳答案
关于异常,如果您使用 JUnit @Test 注释,则测试方法必须是不带参数的 public void
。 Reference 。如果你想使用参数,我找到this resource非常有帮助。
关于设置隐式等待
chrome.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
我真的认为这样做没有任何值(value)。至少我从未设置过它,也从未看到过您遇到的任何异常。
关于元素的过时性 - 当发生数据绑定(bind)或其他任何情况时,这种情况偶尔会发生。您可以选择忽略它。我正在使用的等待方法是:
/**
* If no timeout is given, default to 60 sec.
*
* @param element - WebElement to wait for
*/
public void waitUntil(WebElement element) {
waitUntil(element, 60);
}
/**
* Waits for given WebElement to be present and visible (height and length > 1px) in DOM.
*
* @param element
* @param timeOutInSeconds
*/
public void waitUntil(WebElement element, long timeOutInSeconds) {
new WebDriverWait(driver, timeOutInSeconds)
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class)
.until(ExpectedConditions.visibilityOf(element));
}
关于java - 等待元素时的 WebDriver 同步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21257619/