RSS Feed

Javascript Watir to check element is visible in viewport

Tested in google page search field

element = $browser.text_field(:id => ‘lst-ib’)
script = < 0 &&
rect.right > 0 &&
rect.left < (window.innerWidth || document. documentElement.clientWidth) /*or $(window).width() */ && < (window.innerHeight || document. documentElement.clientHeight) /*or $(window).height() */;
return isElementInViewport(arguments[0]);
puts $browser.execute_script(script, element)

Watir Execute java script on element

element = => ‘link_id’)
script = <<-JS
return arguments[0].innerHTML
puts browser.execute_script(script, element)

Perl – Modules

Including Other Files:

  • do – do will look for a file by searching the @INC path (more on that later). If the file can’t be found, it’ll silently move on. If it is found, it will run the file just as if it was placed in a block within our main program
  • require – is like do, but it’ll only do once. It’ll record the fact that a file has been loaded and will ignore further requests to require it again. It also fails with an error if it can’t find the file you’re loading.
  • use – include all modules before the start of the program

Bless Reference:
bless($self, $class);
Creating own modules:
First parameter passed along with calling a method in class is its own object.
package Person;
# Class for storing data about a person
use warnings;
use strict;
sub new {
my $class = shift;
my $self = {@_};
bless($self, $class);
return $self;
my $object = Person::new(“Person”,
“surname”, “Galilei”,
“forename”, “Galileo”,
“address”, “9.81 Pisa Apts.”,

Perl – Subroutine:

sub name(parameters) {
Calling Sub routines:
sub sub_name {
if only sub_name is given then it throws error as bare word.
if need to use only sub_name have define the sub routine first and then call the sub.

Return statement:
In general last line of the sub routines values will get return or else we can use the keyword return.
sub secs2hms {
my ($h,$m);
my $seconds = shift;
$h = int($seconds/(60*60)); $seconds %= 60*60;
$m = int($seconds/60); $seconds %= 60;
return ($h,$m,$seconds); or ($h,$m,$seconds);
Sub routine reference:
Declaring References to Subroutines
sub something { print “Wibble!\n” }
my $ref = \&something;
Dereferencing subs:

Perl – Reference

Array Reference:

  • my @array = (1, 2, 3, 4, 5);
  • my $array_r = \@array;

Hash Reference:

  • my %hash = ( apple => “pomme”, pear => “poire” );
  • my $hash_r = \%hash;

Scalar Reference:

  • my $scalar = 42;
  • my $scalar_r = \$scalar;

To create Direct reference:

  • Array reference – To get an array reference instead of an array, use square brackets [] instead of parentheses. eg: $a = [1,2,3]
  • Hah Reference – To get a hash reference instead of a hash, use curly braces {} instead of parentheses. eg: $a = {a,2,3}

my @array2 = @{$array_r}; or @$array_r or $ref->[0] or $$ref[0][1]
my %hash = %{$array_r}; or %$array_r or $ref->{key} or $$ref[0]{key}

ARGV in Perl:

ARGV in Perl:
@ARGV array holds any text after the program’s name on the command line.
<ARGV> takes each file after the program’s name on the command line.

Perl – File Handle

File Handle:

  • open FH, “c:\test\news.txt” or die $!;
  • $! contains the message which perl wants to convey us.

Program to Read every line in file in perl:
use warnings;
use strict;
open FILE, “nlexample.txt” or die $!;
my $lineno = 1;
while (<FILE>) {
print $lineno++;
print “: $_”;

Reading Lines for file names passed in command line:
use warnings;
use strict;
my $lineno;
my $current = “”;
while (<>) {
if ($current ne $ARGV) {
$current = $ARGV;
print “\n\t\tFile: $ARGV\n\n”;
print $lineno++;
print “: $_”;

Line Separator in File handle:

  • The special variable $/ is called the ‘input record separator’.
  • Usually, it’s set to be the newline character, \n, and each ‘record’ is a line.
  • If you set the input record separator, $/, to the empty string, “”, Perl reads in a paragraph at a time.
  • If $/ set to undef then can read entire file.

Writing to a File Handle:

open FH, “> $filename” or die $!;
print FH ‘abc’;
This will either create a new file or completely wipe out the contents of an already existing file and let us start writing from the beginning.
open FH, “>> $filename” or die $!;
This will append to the file content.
Getting <STDIN> from File:
>perl summary.plx < chapter6.txt – Input can be get from the mentined file after ‘<‘ symbol
Print <STDOUT> to file:
>perl summary.plx > chapter6.txt – Output will get write in chapter6.txt file

Selecting a Filehandle:
Normally, when you print, the data goes to the STDOUT filehandle. To send it somewhere else, you say print FILEHANDLE …; However, if you’re sending a lot of data to a file, you might not want to have to give the filehandle every time, it would be useful if it were possible to change the default filehandle. You can do this very simply by selecting the filehandle: select FILEHANDLE; This will change the default location for print to FILEHANDLE.