使用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: Method cDragClickAreaComponentToStage() should be void
和
java.lang.Exception: Method cDragClickAreaComponentToStage should have no parameters
好的,排队“那么,你的问题是什么?”。
首先,我想通过设置@Test
public int来了解我要去哪里。通常我会把它宣布为无效,但在这种情况下,我想要return
必须是int
的东西,对吧?
其次,我想知道在尝试等待元素时我在哪里遇到同步问题。
答案 0 :(得分:0)
关于例外情况,如果您使用的是JUnit @Test注释,则测试方法必须为public void
且不带参数。 Reference。如果你想使用参数,我发现this resource非常有帮助。
关于设置隐式等待
chrome.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
我认为这样做没有任何价值。至少我从来没有设置它,也从未看到你得到的任何例外。
关于元素的陈旧性 - 当数据绑定或其他任何事情发生时,偶尔会发生这种情况。你可以选择忽略它。我正在使用的等待方法是:
/**
* 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));
}