Thursday, 31 March 2011

Example - Select Random Wordpress Entry

Example Purpose:
Select a random wordpress blog entry and then click to open it

What you will learn from this example?
  • command: storeXpathCount
  • command: echo for debugging
  • command: store
  • command: storeEval
  • command: CaptureEntirePageScreenshot
  • javascript functions
  • xpath and xpather to select an entry


Pre-requisites:
>> Firefox browser having Selenium IDE

Tips when executing:
  • If your script fails on the xpath command, check that you are logged out of wordpress

How to establish the xpath for the blog entry click?

* open up firefox
* goto http://localhost/wordpress/
* right click on the first blog entry link and select "show in xpather" ( install xpather here)
* check that the xpath extracted indeed extracts the text/web clipping for the blog entry you have xpathed
* now copy the xpath into notepad++
/html/body[@class='home blog']/div[@class='hfeed' and position()=1]/div[2]/div[1]/div/div[@class='post-52 post type-post hentry category-uncategorized' and position()=2]/h2[@class='entry-title']/a
* modify the xpath to make it generic and retest in xpather and notice the same result as before
/html/body[@class='home blog']/div[@class='hfeed' and position()=1]/div[2]/div[1]/div/div[position()=2]/h2[@class='entry-title']/a
* modify again and change [position()=2] to [position()=3] and retest in xpather and notice the next blog entry is selected.
/html/body[@class='home blog']/div[@class='hfeed' and position()=1]/div[2]/div[1]/div/div[position()=3]/h2[@class='entry-title']/a
* now finally, to we want to use a variable for the blog entry position ${RandonBlogEntryId}
/html/body[@class='home blog']/div[@class='hfeed' and position()=1]/div[2]/div[1]/div/div[position()=${RandomBlogEntryId}]/h2[@class='entry-title']/a
* put the this xpath in quotes and place into Selenium IDE for the command storeEval
"/html/body[@class='home blog']/div[@class='hfeed' and position()=1]/div[2]/div[1]/div/div[position()=${RandomBlogEntryId}]/h2[@class='entry-title']/a"



<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;>
<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;>
<head profile=&quot;http://selenium-ide.openqa.org/profiles/test-case&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; />
<link rel=&quot;selenium.base&quot; href=&quot;http://localhost&quot; />
<title>selectRandomBlogEntry</title>
</head>
<body>
<table cellpadding=&quot;1&quot; cellspacing=&quot;1&quot; border=&quot;1&quot;>
<thead>
<tr><td rowspan=&quot;1&quot; colspan=&quot;3&quot;>selectRandomBlogEntry</td></tr>
</thead><tbody>
<tr>
 <td>open</td>
 <td>/wordpress/</td>
 <td></td>
</tr>
<tr>
 <td>storeXpathCount</td>
 <td>//h2</td>
 <td>countId</td>
</tr>
<tr>
 <td>echo</td>
 <td>javascript{&quot;xpath count &quot; + storedVars['countId'];}</td>
 <td></td>
</tr>
<tr>
 <td>store</td>
 <td>javascript{ var mycountId = storedVars['countId']; Math.floor(Math.random()*(mycountId)); }</td>
 <td>RandomBlogEntryId</td>
</tr>
<tr>
 <td>echo</td>
 <td>javascript{&quot;randomBlogEntryId=&quot; + storedVars['RandomBlogEntryId'];}</td>
 <td></td>
</tr>
<tr>
 <td>storeEval</td>
 <td>&quot;/html/body[@class='home blog']/div[@class='hfeed' and position()=1]/div[2]/div[1]/div/div[position()=${RandomBlogEntryId}]/h2[@class='entry-title']/a&quot;</td>
 <td>myxpath</td>
</tr>
<tr>
 <td>echo</td>
 <td>javascript{&quot;myxpath=&quot; + storedVars['myxpath'];}</td>
 <td></td>
</tr>
<tr>
 <td>clickAtAndWait</td>
 <td>xpath=${myxpath}</td>
 <td></td>
</tr>
<tr>
 <td>captureEntirePageScreenshot</td>
 <td>D:\PROFILES\doshiv2\.hudson\jobs\wordpresstest\workspace\wordpressScreenshot_SelectRandomBlogEntry.jpg</td>
 <td></td>
</tr>
</tbody></table>
</body>
</html>


http://www.digitalcoding.com/tools/convert-htmlentities.html

3 comments:

  1. feedback for vipin:
    1. can you ensure that this example works in teh VM.
    2. modify if necessary
    3. Also, create a video
    ensure to use xpather as per the steps.

    ReplyDelete
  2. Viresh's feedback for vipin:
    1. can you ensure that this example works in teh VM.
    2. modify if necessary
    3. Also, create a video
    ensure to use xpather as per the steps.

    ReplyDelete
  3. Here is the posted solution link for your feedback:
    http://time2testcourse.blogspot.com/2011/06/example-select-random-wordpress-entry.html

    ReplyDelete

Thank you for your comment
- Time2test Team