in reply to Re^4: How to create testcase executor?
in thread How to create testcase executor?
Code : package ELABS_Login; use strict; use warnings; use Time::HiRes qw(sleep); use Test::WWW::Selenium; use Test::More "no_plan"; use Test::Exception; use Log::Fast; sub new { my $class = shift; my %args = (PORT => undef, HOST => undef, SELENIUMHOST => "localhost", SELENIUMPORT => "4444", BROWSER => "googlechrome", LOGFOLDER => undef, @_); my $self = {}; $self->{HOST} = $args{HOST}; $self->{PORT} = $args{PORT}; $self->{SELENIUMHOST} = $args{SELENIUMHOST}; $self->{SELENIUMPORT} = $args{SELENIUMPORT}; $self->{BROWSER} = $args{BROWSER}; bless ($self, $class); $self->{LOG} = $self->_log_init($args{LOGFOLDER}); print "Launching the script on browser $self->{BROWSER}\n"; $self->{HANDLE} = Test::WWW::Selenium->new( host => $self->{SELENI +UMHOST}, port => $self->{SELENIUMPORT}, browser => "*$self->{BROWSER}", browser_url => "http://$self->{HOS +T}:$self->{PORT}", default_names => 1, error_callback => sub { print "Fai +led to load the webpage\n"; }, ); return $self; } sub _log_init { my $self = shift; my $logFolder = shift; $logFolder = "C\:\\Users\\esrirsa\\Documents\\Logs\\" if(!defined +$logFolder); mkdir "$logFolder/screenshots" unless -d "$logFolder/screenshots"; my $pid = $$; my $timestamp = scalar(localtime); $timestamp =~ s/\s+|:/_/g; my $logSuffix = $pid."_".$timestamp; $self->{LOGSUFFIX} = $logSuffix; my $logFileName = $logFolder."elab_".$logSuffix.".log"; print "Log file is $logFileName\n"; open(LOG,">$logFileName") or die "Cannot create such file $! \n"; my $LOG = Log::Fast->new({ level => 'DEBUG', prefix => '%D %T [%L] ', type => 'fh', fh => \*LOG, }); return $LOG; } sub login { my $self = shift; my $user = shift; my $password = shift; my $max_seconds_sleep = 60 if (!defined shift); # Parameters are passed through to WWW::Selenium $self->{LOG}->INFO("Starting the main execution"); $self->{LOG}->INFO("This is the process\_id"); $self->{LOG}->INFO("PID\= $$"); my $sel = $self->{HANDLE}; # use special test wrappers around WWW::Selenium commands: my $initial_page = $sel->open_ok("/login"); my $max_window = $sel->window_maximize(); sleep(5); #if($max_window) { #$self->{LOG}->INFO("Successfully maximized the $self{BROWSER} + window "); #} else { # $self->{LOG}->WARN("Couldn't maximize the $self->{BROWSER} wi +ndow"); #} $sel->type("//div[\@id='username']/input", $user); $sel->type("//div[\@id='pass']/input", $password); $sel->click("//div[\@id='edituser']/div[2]"); my $screenshot_path = $sel->capture_screenshot("C\:\\Users\\esrirs +a\\Documents\\Logs\\Screenshots\\login_screen".$self->{LOGSUFFIX}.".p +ng"); $self->{LOG}->INFO("Checking the screenshot stored in the specifie +d path or not "); $self->{LOG}->INFO($screenshot_path); $sel->wait_for_page_to_load("60000"); #$sel->refresh(); $screenshot_path = $sel->capture_screenshot("C\:\\Users\\esrirsa\\ +Documents\\Logs\\Screenshots\\after_login".$self->{LOGSUFFIX}.".png") +; $self->{LOG}->INFO($screenshot_path); for my $second (0..$max_seconds_sleep) { if($sel->is_text_present("ARTS Quick Launch")) { #print "Expected element found\n"; $self->{LOG}->INFO("Login done succesfully"); $screenshot_path = $sel->capture_screenshot("C\:\\Users\\e +srirsa\\Documents\\Logs\\Screenshots\\successful_login" .$self->{LOGS +UFFIX}.".png"); last; #} elsif ($sel->alert_is("Please close you exist")) { # print "Connection lost to E-LAB server\n"; # $screenshot_path = $sel->capture_screenshot("C\:\\Users\\ +esrirsa\\Documents\\Logs\\Screenshots\\failed_login". # $self->{LOGSUFFIX}.".png"); # $self->{LOG}->WARN("Connection lost to E-LAB server"); # exit; }else { $self->{LOG}->WARN("Could not find the expected string in t +he web page after $second/$max_seconds_sleep seconds attempts\n"); sleep ($second); next; } } sleep(5); $screenshot_path = $sel->capture_screenshot("C\:\\Users\\esrirsa\\ +Documents\\Logs\\Screenshots\\login_successful".$self->{LOGSUFFIX}.". +png"); $self->{LOG}->INFO($screenshot_path); $sel->is_text_present("Welcome to e-Lab"); $self->{LOG}->INFO("The execution is done successfully"); return 1; } 1;
This is my .pl file code use strict; use warnings; use ELABS_Login; my $elab = new ELABS_Login(HOST=>"155.53.113.65", PORT=>"3000", BROWSE +R => "googlechrome"); $elab->login("eredmoh","Password2");
|
|---|