isTextPresent在Selenium测试中一直失败。不确定我做错了什么

时间:2010-07-14 16:14:39

标签: java selenium

我有一小段代码只检查页面上某个位置的特定文本,如果找到该文本,它会检查其他内容。这是代码片段:

if(!selenium.isTextPresent(“// div [contains(concat('',@ class,''),'contentLg')] / h2”)){                         System.out.println(“关于我们的文本不存在”);         }         其他{             verifyEquals(aboutText,“// * [@ class ='content contentLg'] / p [3]”);         }

这是生成的输出,我试图从中获取文本“关于我们”。我试图查看该页面是否有“关于我们”文本(位于

标签中)。如果有,只需将“p”标签中的一个与预定义文本匹配:

 <!-- MAIN CONTENT CONTAINER -->

                                                  关于我们

            

        <p><span class="faqQuestion">About The Site</span><br>
        this is about the site</p> 

        <p>test one two three</p>

        <p>this test test test test test test</p>

        <br>

        <p><span class="faqQuestion">About Company</span><br>
        This is about the company</p> 

        <p>fafasdfsafasdfafasfasfasasfasfasfafsasf</p> 

        <p>afasfasdfasdffafffffffffffffffffffffffffffffffffffffffffffffffffffffffffff</p> 

        <br>

        <p><span class="faqQuestion">Helping Businesses</span><br>
        Helping business
        <a href="/businesses.asp">click here</a> to learn more.</p> 

1 个答案:

答案 0 :(得分:0)

以下是我在扩展DefaultSelenium的CustomSelenium类中编写的内容。

public boolean waitForTextPresent(final String text, long timeout) {
    Wait wait = new Wait() {

        @Override
        public boolean until() {
            try {
                return CustomSelenium.this.isTextPresent(text);
            } catch (SeleniumException e) {
                return false;
            }
        }
    };
    try {
        wait.wait("Error: text " + text + " not present in the page", timeout, 50);
    } catch (NumberFormatException e) {
        log.error(e.getMessage(), e);
        throw e;
    } catch (WaitTimedOutException e) {
        log.warn(e.getMessage(), e);
        return false;
    }
    return true;
}

在调用isTextPresent之前调用它。这是a Selenium bug