Archive

Archive for July, 2012

Selenium Webdriver with Ruby Unit Example of Google Search

Copy the code and save as google_search.rb and run “ruby google_search.rb”

require “selenium-webdriver”
require “test/unit”
class GoogleSearch < Test::Unit::TestCase
  def setup
    @driver = Selenium::WebDriver.for :firefox
    @base_url = “http://www.google.com/&#8221;
    @driver.manage.timeouts.implicit_wait = 30
    @verification_errors = []
  end
  def teardown
    @driver.quit
    assert_equal [], @verification_errors
  end
  def test_google_search
    @driver.get(@base_url)
    @driver.find_element(:name, “q”).clear
    @driver.find_element(:name, “q”).send_keys “Thiyagarajan Veluchamy”
    @driver.find_element(:name, “btnK”).click
  end
  def element_present?(how, what)
    @driver.find_element(how, what)
    true
  rescue Selenium::WebDriver::Error::NoSuchElementError
    false
  end
  def verify(&blk)
    yield
  rescue Test::Unit::AssertionFailedError => ex
    @verification_errors << ex
  end
end
Categories: Uncategorized Tags:

Generate random email id in Ruby

Guys,

We can return random email using following method

def email
      emailid = (0..8).map{(‘a’..’z’).to_a[rand(26)]}.join+”@yopmail.com”
      return emailid
end
while need random email id, just call the method “email()”.
Categories: Selenium Tags:

get tomorrow’s Date using javascript for selenium IDE

Selenium.prototype.doGenerateTomorrowDate = function( varName1, varName2 ) 
{
    var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()
    var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
    var month = monthNames[month];
    storedVars[ varName1 ] =  day;
    storedVars[ varName2 ] =  month;
};

<tr>
    <td>generateTomorrowDate</td>
    <td>x</td>
    <td>y</td>
</tr>
<tr>
    <td>echo</td>
    <td>${x}</td>
    <td></td>
</tr>
<tr>
    <td>echo</td>
    <td>${y}</td>
    <td></td>
</tr>
Categories: Selenium Tags: ,

if statement in Selenium IDE

open  | URL of site
storeTextPresent  | any unique text of page a  | title
gotoIf  | storedVars.title  | true
getEval | alert(“page B”);
gotolabel | finish
label  | true
getEval | alert(“Page A”);
label | finish

This will check whether title is present or not, and if present control will redirect to label true, else will execute next statement after gotoif.

Categories: Selenium Tags: ,

Get id of div from its class name using JavaScript for Selenium IDE

Get id of div from its class name using JavaScript for Selenium IDE
<tr>
<td>storeEval</td>
<td>window.document.getElementsByClassName(‘classname’)[0].id</td>
<td>idname</td>
</tr>
<tr>
<td>echo</td>
<td>${idname}</td>
<td></td>
</tr>
Categories: Selenium Tags:

How to solve the link has target ‘_blank’, which is not supported in Selenium

For the example,

<a target=”_blank” id=”verify_email” href=”https://thiyagarajan.wordpress.com/users/verify/1341484215″>Verify your email</a>

So We can use following,

<tr>
<td>storeAttribute</td>
<td>//a[@id=’verify_email’]/@href</td>
<td>href</td>
</tr>
<tr>
<td>echo</td> ‘ this is only optional to see the link
<td>${href}</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>${href}</td>
<td></td>
</tr>

 

Categories: Selenium Tags: ,

Generates a random word using JavaScript in Selenium IDE

Selenium.prototype.doRandomWord = function( length, varName ) {
    var consonants = ‘bcdfghjklmnpqrstvwxyz’,
        vowels = ‘aeiou’,
        rand = function(limit) {
            return Math.floor(Math.random()*limit);
        },
        i, word=”, length = parseInt(length,10),
        consonants = consonants.split(”),
        vowels = vowels.split(”);
    for (i=0;i<length/2;i++) {
        var randConsonant = consonants[rand(consonants.length)],
            randVowel = vowels[rand(vowels.length)];
        word += (i===0) ? randConsonant.toUpperCase() : randConsonant;
        word += i*2<length-1 ? randVowel : ”;
    }
storedVars[ varName ] =  word;
};
Categories: Selenium Tags: ,