Tuesday, July 9, 2013

Very useful Perl scripts

1) Perl script to get web page content
 #!/usr/bin/perl
use LWP::Simple;
$website_content = get("http://www.xyz.com");  #enter the URL
open FILEHANDLE, ">D:\Filename.txt";
print FILEHANDLE $website_content;
close FILEHANDLE;



2) Perl script to get Links from web page
#!/usr/bin/perl
use HTML::LinkExtor;
use LWP::Simple;

my $page = get("http://www.xyz.com ");
my $parser = new HTML::LinkExtor;
$parser->parse($page);


 # once we have parsed the page ...
 my @links = $parser->links;
 foreach (@links) {
    # $_ contains [type, [name, value], ...]
    print "Type: ", shift @$_, "\n";
    while (my ($name, $value) = splice(@$_, 0, 2)) {
         print "  $name -> $value\n";
     }
 sub absolutize {
  my($url, $base) = @_;
  use URI;
  return URI->new_abs($url, $base)->canonical;
}

 }



3) Extracting Links from Arbitrary HTML
#!/usr/bin/perl -w
use strict;
use LWP;

my $doc_url = "http://www.xyz.com ";
my $document;
my $browser;
init_browser( );

{  # Get the page whose links we want to check:
  my $response = $browser->get($doc_url);
  die "Couldn't get $doc_url: ", $response->status_line
    unless $response->is_success;
  $document = $response->content;
  $doc_url = $response->base;
  # In case we need to resolve relative URLs later
}

while ($document =~ m/href\s*=\s*"([^"\s]+)"/gi) {
  my $absolute_url = absolutize($1, $doc_url);
  check_url($absolute_url);
}

sub absolutize {
  my($url, $base) = @_;
  use URI;
  return URI->new_abs($url, $base)->canonical;
}

sub init_browser {
  $browser = LWP::UserAgent->new;
  # ...And any other initialization we might need to do...
  return $browser;
}

sub check_url {
  # A temporary placeholder...
  print "Check - $_[0]\n";
}



4) Calculate the load time of webpage
#----  code  ----#
#!/usr/local/bin/perl
use LWP::Simple;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;
use HTML::TreeBuilder;
use WWW::Mechanize;

$url = ‘http://www.xyz.com ';
$started = time;
print "Started: $started\n";

my $mech  = WWW::Mechanize->new();
$mech->get( $url );
my @links = $mech->links();

foreach my $link (@links) {
   print "LINK: "        . $link->url() . "\n";
   print "DESCRIPTION: " . $link->text() . "\n";
   print MYFILE1 "LINK: "        . $link->url() . "\n";
}
# this will get the HTML text
$content = get($url);
print "Got content\n";

# parse the IMG SRC tags from the HTML
while ($content =~ /<IMG.*?SRC="(.*?)"/gis)
    {
    # retrieve each IMG SRC
    print "Getting $1\n";
    get($1); # I have not tried this, but, I don't see
                           # why it would not work
    }
$stopped = time;
print "Stopped: $stopped\n";
$elapsed = $stopped - $started;
print "Elapsed: $elapsed\n";



5) Integrating java code inside Perl script
use LWP::Simple;

$url = ‘http://www.xyz.com ';

# JAVA COMMAND LINE THAT I HAD DONE FOR GETTING TIME IN MILLISECONDS
$started = `java date`;

print "Started: $started\n";

$content = get($url);
print "Got content\n";

while ($content =~ /<IMG.*?SRC="(.*?)"/gis)
    {
    print "Getting $1\n";
    get($1);
    }

#######
$stopped = `java date`;
print "Stopped: $stopped\n";
$elapsed = $stopped - $started;
print "Elapsed: $elapsed\n";


Java Code:

class date
{
   public static void main(String args[]) throws Exception
   {
        System.out.println((new java.util.Date()).getTime());
   }
}



6) Reading Whole File at Once
#!/usr/local/bin/perl

open (MYFILE, 'D:\sample.txt');

 while (<MYFILE>) {
     chomp;
     print "$_\n";
 }
 close (MYFILE);



7) Writing to a File
#!/usr/local/bin/perl

open (MYFILE, '>>D:\sample.txt');

print MYFILE "This is being appended or rather added to the existing file which already has data.\n";

close (MYFILE);



8) Check if a file exists
#!/usr/bin/perl -w

my $filename = 'D:\sample.txt';

if (-e $filename) {print "File Exists!";}



9) Check if a file does not exist
#!/usr/bin/perl -w

my $filename = 'D:\sample.txt';

if (-e $filename) {print "File Exists!";}

unless (-e $filename) {print "File Doesn't Exist!";}



10) Search specific word in file and display the whole line
#!/usr/bin/perl

$file = 'D:\sample.txt';
open(txt, $file);
while($line = <txt>) {
  print "$line" if $line =~ /Services:/;
}
close(txt);



11)Parse all the links in a webpage into an array and print
#parse all the links in a web page into an array organized like this: ($link, $description)
use strict;
use warnings;
my $link;


use WWW::Mechanize;

my $url  = " http://www.xyz.com ";
#my $url   = "http://www.domain.com/webpage.html";

my $mech  = WWW::Mechanize->new();

$mech->get( $url );

my @links = $mech->links();

foreach my $link (@links) {   
    print "LINK: "        . $link->url() . "\n";
    print "DESCRIPTION: " . $link->text() . "\n";
    }



12) Display the line number of matching text


#!/usr/bin/perl
use strict;
use warnings;
my $line;

open('txt', 'D:\sample.txt');
my $i = 0;

while ($line = <txt>)
{
    $i = $i+1;
    print "$i\n" if $line =~ /appended/; #Only prints the Line number that matches the text.
   
    if($line =~ /appended/){  #Prints Line number and full sentence.
    print "$i -- ";print"$line";}
}



13) Dumping webpage content into XML writing into a file

use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
use LWP::Simple;
my $line;

open my $MYFILE, '>', 'outfile.txt';

my $parser = new XML::Simple;
my $url = 'http://www.xyz.com &dumpXML=1';
my $content = get $url or die "Unable to get $url\n";
my $data = $parser->XMLin($content);
#print Dumper($data);
#print FILE Data::Dumper->Dump($data);

print $MYFILE Dumper($data);
close $MYFILE;

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- 









Selenium - Perl Description and Available Command Methods

Selenium - Perl Client for the Selenium Remote Control test tool


For more information visit  http://search.cpan.org/~lukec/Test-WWW-Selenium-1.32/lib"/WWW/Selenium.pm


  use WWW::Selenium;

    my $sel = WWW::Selenium->new( host => "localhost",
                                  port => 4444,
                                  browser => "*iexplore",
                                  browser_url => "http://www.google.com",
                                );

    $sel->start;
    $sel->open("http://www.google.com");
    $sel->type("q", "hello world");
    $sel->click("btnG");
    $sel->wait_for_page_to_load(5000);
    print $sel->get_title;
    $sel->stop;

DESCRIPTION

Selenium Remote Control (SRC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. SRC provides a Selenium Server, which can automatically start/stop/control any supported browser. It works by using Selenium Core, a pure-HTML+JS library that performs automated tasks in JavaScript; the Selenium Server communicates directly with the browser using AJAX (XmlHttpRequest).

http://www.openqa.org/selenium-rc/

This module sends commands directly to the Server using simple HTTP GET/POST requests. Using this module together with the Selenium Server, you can automatically control any supported browser.

To use this module, you need to have already downloaded and started the Selenium Server. (The Selenium Server is a Java application.)

Element Locators

Element Locators tell Selenium which HTML element a command refers to.The format of a locator is:

locatorType=argument
We support the following strategies for locating elements:

identifier=id: Select the element with the specified @id attribute. If no match isfound, select the first element whose @name attribute is id.(This is normally the default; see below.)
id=id:Select the element with the specified @id attribute.
name=name:Select the first element with the specified @name attribute.
username
name=username
The name may optionally be followed by one or more element-filters, separated from the name by whitespace. If the filterType is not specified, value is assumed.

name=flavour value=chocolate
dom=javascriptExpression: Find an element by evaluating the specified string. This allows you to traverse the HTML Document ObjectModel using JavaScript. Note that you must not return a value in this string; simply make it the last expression in the block.
dom=document.forms['myForm'].myDropdown
dom=document.images[56]
dom=function foo() { return document.links[1]; }; foo();
xpath=xpathExpression: Locate an element using an XPath expression.
xpath=//img[@alt='The image alt text']
xpath=//table[@id='table1']//tr[4]/td[2]
xpath=//a[contains(@href,'#id1')]
xpath=//a[contains(@href,'#id1')]/@class
xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
xpath=//input[@name='name2' and @value='yes']
xpath=//*[text()="right"]
link=textPattern:Select the link (anchor) element which contains text matching thespecified pattern.
link=The link text
css=cssSelectorSyntax:Select the element using css selectors. Please refer to http://www.w3.org/TR/REC-CSS2/selector.html (CSS2 selectors), http://www.w3.org/TR/2001/CR-css3-selectors-20011113/ (CSS3 selectors) for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
css=a[href="#id3"]
css=span#firstChild + span
Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).

ui=uiSpecifierString:Locate an element by resolving the UI specifier string to another locator, and evaluating it. See the http://svn.openqa.org/fisheye/browse/~raw,r=trunk/selenium/trunk/src/main/resources/core/scripts/ui-doc.html (Selenium UI-Element Reference) for more details.
ui=loginPages::loginButton()
ui=settingsPages::toggle(label=Hide Email)
ui=forumPages::postBody(index=2)//a[2]
Without an explicit locator prefix, Selenium uses the following defaultstrategies:

dom, for locators starting with "document."
xpath, for locators starting with "//"
identifier, otherwise
Element Filters

Element filters can be used with a locator to refine a list of candidate elements. They are currently used only in the 'name' element-locator.

Filters look much like locators, ie.

filterType=argument
Supported element-filters are:

value=valuePattern
Matches elements based on their values. This is particularly useful for refining a list of similarly-named toggle-buttons.

index=index
Selects a single element based on its position in the list (offset from zero).

String-match Patterns

Various Pattern syntaxes are available for matching string values:

glob:pattern:Match a string against a "glob" (aka "wildmat") pattern. "Glob" is akind of limited regular-expression syntax typically used in command-lineshells. In a glob pattern, "*" represents any sequence of characters, and "?"represents any single character. Glob patterns match against the entirestring.
regexp:regexp:Match a string using a regular-expression. The full power of JavaScriptregular-expressions is available.
regexpi:regexpi:Match a string using a case-insensitive regular-expression.
exact:string:Match a string exactly, verbatim, without any of that fancy wildcardstuff.
If no pattern prefix is specified, Selenium assumes that it's a "glob"pattern.

For commands that return multiple values (such as verifySelectOptions),the string being matched is a comma-separated list of the return values,where both commas and backslashes in the values are backslash-escaped.When providing a pattern, the optional matching syntax (i.e. glob,regexp, etc.) is specified once, as usual, at the beginning of thepattern.


FOR AVAILABLE METHODS

visit "http://search.cpan.org/~lukec/Test-WWW-Selenium-1.32/lib/WWW/Selenium.pm"

Thursday, August 30, 2012

Understanding Selenium IDE


Understanding Selenium IDE



Mozilla Firefox Down load Selenium IDE -  http://seleniumhq.org/download/

Install Selenium IDE and restart Firefox browser.



 
In Mozilla Firefox Browser-
Click on Tools > Click on Selenium IDE > Selenium IDE pop-up is displayed








Click on > Options > Selenium IDE Options pop-up is displayed.
Select all the check box and click OK.




Download Selenium IDE: PERL Formatter
The latest versions of Selenium IDE do not support PERL.


 





Install the Selenium IDE: PERL Formatter and restart your browser.

Click on > Format > list of supported languages are displayed.
Select “PERL”.





Now you are ready to record your scripts in PERL scripting language.