Friday, August 31, 2012

Sample Selenium RC Perl Script File

Perl Script File

Always Save script files with extension as  (.t)

For e.g.

Script File Name -     1.t

#author @amit

#!/usr/bin/perl

use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost",
                                    port => 4444,
                                    browser => "*chrome",
                                    browser_url => "http://www.google.co.in/" );
$sel->set_timeout("200000");
$sel->open_ok("/");
$sel->window_maximize();

$sel->set_speed("2000"); #Set execution speed (millisecond length of a delay which will follow each selenium operation).

#$sel->highlight_ok(""); #Highlight action

$sel->title_is("Google");

$sel->highlight_ok("xpath=id('lst-ib')");#Highlight action
sleep("3");
$sel->type_ok("xpath=id('lst-ib')", "camera");

$sel->highlight_ok("//input[\@type=\"submit\"]");#Highlight action
sleep("3");
$sel->click_ok("//input[\@type=\"submit\"]");
sleep("10");

$sel->highlight_ok("xpath=id('gb_2')/span[2]"); #Highlight action
sleep("3");
$sel->click_ok("xpath=id('gb_2')/span[2]");
sleep("10");
$sel->is_text_present_ok("Images");

$sel->highlight_ok("//a[contains(text(),'Maps')]"); #Highlight action
sleep("3");
$sel->text_is("//a[contains(text(),'Maps')]", "Maps");

$sel->highlight_ok("link=Past 24 hours"); #Highlight action
sleep("3");
$sel->is_element_present_ok("link=Past 24 hours");

$sel->highlight_ok("xpath=id('cdrlnk')"); #Highlight action
sleep("3");
$sel->text_is("xpath=id('cdrlnk')", "Custom range...");

$sel->highlight_ok("link=By subject"); #Highlight action
sleep("3");
my $a = $sel->is_element_present("link=By subject");

$sel->highlight_ok("link=Show sizes"); #Highlight action
sleep("3");
$sel->click_ok("link=Show sizes");

$sel->wait_for_page_to_load_ok("60000");
$sel->title_is("camera - Google Search");

$sel->highlight_ok("xpath=id('resultStats')"); #Highlight action
sleep("3");
my $b = $sel->get_text("xpath=id('resultStats')");
print "$b\n";
$sel->is_text_present_ok("Sign in");

$sel->highlight_ok("xpath=id('gbqfb')"); #Highlight action
sleep("3");
$sel->text_is("xpath=id('gbqfb')", "");

$sel->title_is("camera - Google Search");

$sel->highlight_ok("xpath=id('rg_hpl')"); #Highlight action
sleep("3");
$sel->click_ok("xpath=id('rg_hpl')");
sleep("12");

$sel->highlight_ok("//a[\@id=\"rg_hpl\"]"); #Highlight action
sleep("3");
$sel->is_element_present_ok("//a[\@id=\"rg_hpl\"]");


Always Save Testsuite files with extension as  (.pl)

For e.g.

Testsuite File Name -     1.pl

#! /usr/bin/perl

use strict;
use warnings;

use TAP::Harness;
use TAP::Formatter::HTML;

my $fmt = TAP::Formatter::HTML->new;

my @tests = glob('1.t');



# In case you have more than one test script files, you just need to add the other test script one 
# after the other

#For e.g. my @tests = glob('1.t
#                                           2.t
#                                           3.t');
                   
my $harness = TAP::Harness->new( { formatter => $fmt, merge => 1 } );
#my $now = localtime time;
my ($sec,$min,$hour,$day,$month,$yr19,@rest) =   localtime(time);
my $data = ($day."-".++$month."-".($yr19+1900)."-");

$fmt->output_file("D:/".$data."Myfile.html");

$harness->runtests(@tests);


How the test scripts run in command prompt:





Sample Selenium Test Execution Report-

PASS Report collapsed view-




PASS Report expanded view-  




FAILURE Report collapsed view-







FAILURE Report expanded view- 









1 comment:

  1. Hi Amit

    Good post..

    While i was trying to use this noticed error when executing the second file. Did not know if you come across this.

    Parse error: Plan (1..3) must be at the beginning or end of the TAP output
    Parse error: Tests out of sequence. Found (1) but expected (4)
    Parse error: Tests out of sequence. Found (2) but expected (5)
    Parse error: Tests out of sequence. Found (3) but expected (6)
    Parse error: More than one plan found in TAP output

    ReplyDelete