Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待元素时WebDriver同步问题_Java_Selenium Webdriver_Junit4 - Fatal编程技术网

Java 等待元素时WebDriver同步问题

Java 等待元素时WebDriver同步问题,java,selenium-webdriver,junit4,Java,Selenium Webdriver,Junit4,使用WebDriver、JUnit4.11、Maven、IntellijV.13 我正在试图找到写这个测试的正确方法,即验证一旦我点击了一个元素,它会带我进入下一页 在研究了WaitTool之后,我试图将隐式Wait置为空,然后执行WebDriverWait,然后重置隐式Wait,尽管我收到了初始化错误 最终,我的主要目标是通过使用新的WaitTool来停止我遇到的同步问题 我遇到的问题是,当我单击一个按钮时,我偶尔会在等待元素new WebDriverWatchrome时收到一个错误,30.u

使用WebDriver、JUnit4.11、Maven、IntellijV.13

我正在试图找到写这个测试的正确方法,即验证一旦我点击了一个元素,它会带我进入下一页

在研究了WaitTool之后,我试图将隐式Wait置为空,然后执行WebDriverWait,然后重置隐式Wait,尽管我收到了初始化错误

最终,我的主要目标是通过使用新的WaitTool来停止我遇到的同步问题

我遇到的问题是,当我单击一个按钮时,我偶尔会在等待元素new WebDriverWatchrome时收到一个错误,30.untilExpectedConditions.Visibility of ElementLocatedBy.cssSelectordV.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.implicitlyWait3,TimeUnit.SECONDS;在第二次测试中,他意识到这也是WebDriverWait的设置。所以我遇到了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;

}
所以在stacktrace里我得到了

java.lang.Exception:方法cdraClickAreaComponentToStage应为空

java.lang.Exception:方法cdraClickAreaComponentToStage不应具有任何参数

好的,那么,你的问题是什么

首先,我想知道在哪里设置@Test public int。通常我会将它声明为void,但在这种情况下,我想返回必须是int的东西,对吗


其次,我想知道当同步问题试图等待一个元素时,我在哪里出了问题。

关于您的异常,如果您使用的是JUnit@Test annotation,那么测试方法必须是public void,没有参数。如果您想使用参数,我觉得非常有用

关于设置隐式等待

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));
}

有时我需要在同一个方法中使用隐式wait和WebDriverWait,因此尝试使用新的WaitTool。因此,我没有使用Thread.sleep,而是使用隐式wait,当我自动化GWT应用程序时,我需要等待页面加载,即使有一秒钟,因为一个元素可以在“旧DOM”中删除,因为它刷新得很快。因此,我正在尝试新的技术,使我的测试尽可能健壮,所以我尝试使用try/catch和public int来返回一些东西。这就是我想要的指导。