2011-06-16 160 views
1

我正在测试一个与Google非常相似的网站。所以,如果用户输入了一些搜索条件,比如“Selenium Tutorial”,结果页面会包含一连串带有片段的链接。Selenium:有没有办法在整个页面上搜索给定的关键字?

每个结果都在一个表格中。所以,如果有10个结果,则有10个表。 对于每个表(结果),都有几行(如r1,r2,r3等)。 r1是结果的标题,r2是大小,格式,作者等的描述,而r3是摘录。我需要确保r3符合单词“selenium”“tutorial”。我试着做类似这样的事情:

String variable2 = Sel.GetTable("//div[@id='RepeaterContent']/table[2].3.1"); 

并且这给了我在r3中的文本片段。但是,我需要获取所有表格(结果)的文本(片段)。然后使用类似于的东西在代码片段上执行搜索包含

我不知道,如果下面的代码是非常有用的,因为我不能复制源代码,但结果页面的源代码看起来像下面这样:

<HTML> 
<BODY> 
<DIV id="RepeaterContent"> 
<TABLE> 
<TR> 
    <TD> 
    <SPAN> 
     <a id="linkcft87021" class="ResultList_Title_Link" title="" href="DocsDetail.aspx?searchtranid=1bnt5d92" target="">Sample Tutorial</a> 
     <br> 
    </SPAN> 
    </TD> 
</TR> 

<TR> 
    <TD> 
    ...an integrated development environment for performing Selenium tests... 
    </TD> 
</TR> 
</TABLE> 

<TABLE> 
<TR> 
    <TD> 
    <SPAN> 
     <a id="linkcft87021" class="ResultList_Title_Link" title="" href="DocsDetail.aspx?searchtranid=1g5t5d92" target="">Selenium</a> 
     <br> 
    </SPAN> 
    </TD> 
</TR> 

<TR> 
    <TD> 
    ...With Selenium you can automate the whole process and run the test as required. In this part we will see how to create a simple test in Selenium... 
    </TD> 
</TR> 
</TABLE> 

</BODY> 
</HTML> 

而下面是我的硒测试:

namespace Automation 
{ 
    [TestClass] 
    public class SEARCH 
    { 
     public ISelenium Sel; 
     public string TestStatus = "FAIL"; 


     // Constructor 
     public LIT_SEARCH_TAB() 
     { 
      Sel = new DefaultSelenium("localhost", 4444, "*iexplore", "http://localhost:8080"); 
     Sel.Start(); 
     } 


     [TestMethod] 
     public void SEARCH_METHOD() 
     { 
      Sel.Open("http://localhost:8080"); 
      Sel.Type("Search_Text_Box", "Selenium Tutorial"); 
      Sel.Click("Go"); 

     if (Sel.IsTextPresent("Selenium") || (Sel.IsTextPresent("Tutorial"))) 
     { 
        TestStatus = "PASS"; 
     } 

     } 
    } 
} 

所以,我想确保在整个结果页面中有单词“Selenium”和“Tutorial”。硒能否满足我的要求?有些方法会在整个页面中搜索这些关键字,并验证关键字是否存在?提前致谢。

回答

6

您可以使用IsTextPresent(pattern)来检查页面上是否存在特定的字符串。 pattern可以是glob,regexpexact之一,默认值为glob

+0

感谢Tnem!我尝试使用IsTextPresent(“Selenium教程”);但每次都会返回false。 – Maya 2011-06-16 16:23:49

+0

@Maya您确定屏幕上显示“Selenium Tutorial”的确切文字吗? – Tnem 2011-06-16 16:32:01

+0

@Tnem:我正在使用IsTextPresent(“Selenium教程”)的页面是带有片段链接的结果页面。所以,文本“Selenium教程”分散在整个结果页面。此外,如果仅找到单词“Selenium”或者只找到单词“Tutorial”,我希望IsTextPresent()返回true。 – Maya 2011-06-16 17:03:22

0

Selenium有一个getBodyText方法,它为您提供整个页面的HTML文本。

+0

有没有一种方法可以用来搜索getBodyText返回的HTML文本中的关键字? – Maya 2011-06-16 15:30:36

+0

当我使用getBodyText时,它给了我null。 – Maya 2011-06-20 19:51:14

1
try { 
    $this->assertTrue((bool)preg_match('/Selenium/',$this->getText("//div[@id='RepeaterContent']/table[1]/tbody/tr[2]/td"));); 
} catch (PHPUnit_Framework_AssertionFailedError $e) { 
    array_push($this->verificationErrors, $e->toString()); 
} 

这个你需要在PHP中的代码。

,你必须使用
命令verifyText
目标// TR [2]/TD
正则表达式:硒

这将搜索硒在给定的目标可以取代您想要或使用RE的词。

谢谢

相关问题