【问题标题】:Waiting for a chart to refresh, selenium Java等待图表刷新,selenium Java
【发布时间】:2013-06-10 15:16:08
【问题描述】:

我正在尝试为这种情况找到合适的 ExpectedConditions 方法。我有一个图表,我想在重新排序图表后检查每一行中的文本。问题是,当图表刷新时,文本仍然存在,只是变灰了。因此,当我单击一个按钮以重新排序图表,然后立即查找我要查找的文本时,测试失败,因为文本尚未更改。我不能使用visibilityOfElementLocated,因为刷新图表时元素仍然可见,我只是在等待元素更改。

不确定这是否有意义!这是一个很难解释的问题。

一点背景知识:我正在使用 Selenium Java 并使用 Chrome 进行测试。到目前为止,这是我的方法。它工作正常,我只需要弄清楚如何让程序等待足够长的时间让图表刷新而不使用睡眠语句。

非常感谢大家!我知道这不是很清楚,但如果您需要任何澄清,请告诉我。

public void Check_for_text_in_column(String text, String row, String column)
{
    By by = By.xpath("//*[@id=\"table_Table_table_ktg\"]/tbody/tr[" + row + "]/td[" + column + "]/div/div/span");
    WebDriverWait wait = new WebDriverWait(getWebDriver(), WAIT_TIME);

    //This is the line that I need to change: 
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

    if(!element.getText().equals(text))
    {
        fail("\nDid not find text: " + text + "\nFound text: " + element.getText() + "\n");
    }
}

干杯!

【问题讨论】:

  • 您当前的WAIT_TIME 设置为多长时间?
  • WAIT_TIME 当前设置为“30”,超时间隔为 30 秒。

标签: java testing refresh selenium-webdriver


【解决方案1】:

你可以替换

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

与

WebElement element = wait.until(ExpectedConditions.textToBePresentInElement(by, text));

编辑:

您的WAIT_TIME 是您等待的超时时间。

如果在根据您的WAIT_TIME 超时之前预期的条件还没有返回true,那么element 将是null。

因此,您的支票可能如下所示:

if(element == null)
{
    fail("\nDid not find text: " + text + "\nFound text: " + element.getText() + "\n");
}

编辑:

也许另一种选择可能是这样的:

public void Check_for_text_in_column(String text, String row, String column)
{
    By by = By.xpath("//*[@id=\"table_Table_table_ktg\"]/tbody/tr[" + row + "]/td[" + column + "]/div/div/span");
    WebDriverWait wait = new WebDriverWait(getWebDriver(), WAIT_TIME);

    // your original find
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

    // flag to set when text is found, for exiting loop
    boolean hasText = false;

    // counter for # of times to loop, finally timing out
    int tries = 0;

    // until text is found or loop has executed however many times...
    while (hasText == false && tries < 20) {

        // get the element
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));

        // if text is not present, wait 250 millis before trying again
        if(!element.getText().equals(text){
            Thread.sleep(250);
            tries++;
        }
        else{
            // text found, so set flag to exit loop
            hasText = true;
        }   
    }


    if(!element.getText().equals(text))
    {
        fail("\nDid not find text: " + text + "\nFound text: " + element.getText() + "\n");
    }
}

我知道您说过您不想要 sleep 语句,但我认为您的意思是您不想要一个不必要的长语句。甚至ExpectedConditions 也在内部使用sleep。他们 sleep 在轮询更改之间有几毫秒的时间 - 这正是它所做的,只是没有 ExpectedCondition 类型包装器。

【讨论】:

  • 感谢您的回复!如果我不检查以确保文本在那里,那将是一个很好的解决方案。因此,如果我要查找的文本碰巧不存在,我希望它失败,而不是一直等待。有没有办法让我检测到超时并使测试失败,但继续前进?
  • WAIT_TIME 是超时。更新了我的答案以显示如何使用它。
  • 我现在就试一试。请注意, textToBePresentInElement 返回一个布尔值,而不是一个元素。我会尝试使用它。再次感谢!非常感谢您的帮助。如果可以的话,我会赞成你的回答。如果一切顺利,我会接受! :) 干杯!!编辑 - 它也是一个静态方法,所以我可以直接调用它。
  • ExpectedCondition 被传递给返回 WebElement 的 wait
  • 哦!呃。现在要弄清楚为什么我的 IDE 不接受它。再次感谢!
猜你喜欢
  • 2017-05-06
  • 1970-01-01
  • 2019-11-13
  • 2012-02-14
  • 1970-01-01
  • 2022-09-24
  • 2012-06-14
  • 1970-01-01
相关资源
最近更新 更多