2011-06-16 159 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的詞。

謝謝

相關問題