Selenium: Is there a way to search the entire page for given keywords?
I am testing a website very similar to Google. So, if a user entered some search terms like "Selenium Tutorial", the results page would have a bunch of links with snippets.
Each result is in a table. So, if there are 10 results, there are 10 tables. For each table (result), there are several rows (say r1, r2, r3, etc). r1 is the title of the result, r2 is the description like size, format, author, etc, and r3 is the snippet. I need to make sure that r3 consits of the word(s) "selenium" "tutorial". I tried doing something like:
String variable2 = Sel.GetTable("//div[@id='RepeaterContent']/table[2].3.1");
and this gave me the text in r3, the snippet. But, I need to get the text (snippet) of all tables (results). And perform a search on the snippet afterwards using something like contains.
I am not sure if the following code is much helpful because I am unable to copy the source code, but the source of the results page looks something like the following:
<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>
<开发者_Go百科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>
And the following is my selenium test:
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";
}
}
}
}
So, I want to make sure that in the entire results page, there are words "Selenium" and "Tutorial". Is there a way in Selenium that would meet my requirements? Some methods that would search for those keywords in the entire page, and verify the keywords are present? Thanks in advance.
You can use IsTextPresent(pattern)
to check if a specific string is on the page. pattern
can be one of glob
, regexp
or exact
, the default is glob
.
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());
}
This The Code you require in PHP.
You Have to Use
Command verifyText
Target //tr[2]/td
Value regexp:Selenium
This Will search Selenium in the given target you can replace the word by which you want or use RE.
Thanks
Selenium has a getBodyText method which gives you the entire page's HTML text.
精彩评论