Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Perl and Selenium Grid 2 - Redirecting STDOUT not working how I need it to.

by Doozer (Beadle)
on Nov 20, 2012 at 14:51 UTC ( #1004730=perlquestion: print w/ replies, xml ) Need Help??
Doozer has asked for the wisdom of the Perl Monks concerning the following question:

I am working with perl and Selenium Grid 2 to do some automated web browser testing on routers. The below script works with 2 'foreach' loops to test each browser version in a number of Windows and MAC operating systems.

The problem I have is I want the script to print it's STDOUT to different files for each Browser/OS combination. You will probably get a better understanding of what I am doing by reading the script. Printing the STDOUT content to a different file each time is fine, but the output from the Selenium tests ($sel) still only shows in the terminal and do not get written to the specified file.

Please feel free to pick apart my script and suggest easier/shorter ways of doing things. I am still fairly new to perl so am open to learning new techniques. I would also greatly accept any guidance from anyone who has experience running Selenium Grid tests with Perl.

#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(sleep); use Test::WWW::Selenium; use Test::More "no_plan"; use Test::Exception; #use WWW::Mechanize; #use WWW::Mechanize::FormFiller; #use URI::URL; #use LWP 5.64; #use LWP::Simple; my $sel=''; my $browser=''; my $host=''; my $host1=('192.168.1.206'); my $host2=('192.168.1.207'); my $host3=('192.168.1.208'); my $host4=('192.168.1.230'); my $host5=('192.168.1.231'); my @hosts=($host1, $host2, $host3, $host4, $host5); my @browsers=("firefox", "googlechrome", "iexplore"); my $browserurl=('http://admin:sky@192.168.0.1/'); my $date=(localtime); my $hostname=(''); foreach $browser(@browsers) { foreach $host(@hosts) { $sel = Test::WWW::Selenium->new(host => "$host", port => 5555, browser => "$browser", browser_url => "http://192.168.0.1" ); if ($host eq "192.168.1.206") { $hostname = "Windows_XP"; } if ($host eq "192.168.1.207") { $hostname = "Windows_Vista"; } if ($host eq "192.168.1.208") { $hostname = "Windows_7"; } if ($host eq "192.168.1.230") { $hostname = "MAC_OSX_Mountain_Lion"; } if ($host eq "192.168.1.231") { $hostname = "MAC_OSX_Lion"; } open STDOUT, "+>/var/www/cgi-bin/Perl-Scripts/$hostname\_$browser\.txt +"; print "$host - $hostname - $browser - $date\n\n"; $sel->open_ok("$browserurl"); $sel->window_maximize(); $sel->window_focus(); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Wireless"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("link=Setup Access List"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("name=wlFltMacMode"); $sel->click_ok("name=wlFltMacMode"); $sel->click_ok("link=Delete"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("xpath=(//a[contains(text(),'Add')])[3]"); $sel->alert_is("Device Name cannot be blank "); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("link=Add WPS Client"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless WPS"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless WPS"); $sel->click_ok("link=Security"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Logs"); $sel->click_ok("link=Refresh"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Logs"); $sel->click_ok("id=blockedsites"); $sel->click_ok("id=blockedsites"); $sel->click_ok("id=connections"); $sel->click_ok("id=connections"); $sel->click_ok("id=send"); $sel->click_ok("id=disable"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Logs"); $sel->click_ok("link=Block Sites"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Block Sites"); $sel->click_ok("id=trustedip"); $sel->click_ok("id=trustedip"); $sel->click_ok("id=per"); $sel->click_ok("id=never"); $sel->click_ok("id=always"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Block Sites"); $sel->click_ok("link=Firewall Rules"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("link=Delete"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("link=Edit"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("link=Move Up"); $sel->click_ok("link=Move Down"); $sel->click_ok("xpath=(//a[contains(text(),'Edit')])[2]"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("xpath=(//a[contains(text(),'Delete')])[2]"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("xpath=(//a[contains(text(),'Move Up')])[2]"); $sel->click_ok("xpath=(//a[contains(text(),'Move Down')])[2]"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("link=Services"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Services"); $sel->click_ok("link=Delete Service"); $sel->title_is("Sky Broadband Router > Services"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("link=Edit Service"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("link=Add Custom Service"); $sel->wait_for_page_to_load_ok("100000"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Services"); $sel->click_ok("link=Firewall Rules"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("link=Add"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("xpath=(//a[contains(text(),'Add')])[2]"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Firewall Rules"); $sel->click_ok("link=Schedule"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Schedule"); $sel->click_ok("name=checkboxNameAll"); $sel->click_ok("name=checkboxNameSun"); $sel->click_ok("name=checkboxNameMon"); $sel->click_ok("name=checkboxNameTue"); $sel->click_ok("name=checkboxNameWed"); $sel->click_ok("name=checkboxNameThu"); $sel->click_ok("name=checkboxNameFri"); $sel->click_ok("name=checkboxNameSat"); $sel->click_ok("name=checkboxNamehours"); $sel->click_ok("name=checkboxNamehours"); $sel->click_ok("name=checkboxNameSat"); $sel->click_ok("name=checkboxNameFri"); $sel->click_ok("name=checkboxNameThu"); $sel->click_ok("name=checkboxNameWed"); $sel->click_ok("name=checkboxNameTue"); $sel->click_ok("name=checkboxNameMon"); $sel->click_ok("name=checkboxNameSun"); $sel->click_ok("name=checkboxNameAll"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Schedule"); $sel->click_ok("link=Maintenance"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router"); $sel->click_ok("link=Connection Status"); $sel->wait_for_pop_up_ok("datasub_win", "30000"); $sel->select_pop_up("datasub_win"); #$sel->select_window_ok("name=datasub_win"); $sel->pause("3000"); $sel->click_ok("link=Close"); $sel->pause("3000"); $sel->select_window_ok(""); #$sel->wait_for_page_to_load_ok("100000"); $sel->pause("3000"); $sel->open_ok("http://192.168.0.1/sky_router_status.html"); $sel->title_is("Sky Broadband Router"); $sel->pause("3000"); $sel->click_ok("link=Show Statistics"); $sel->wait_for_pop_up_ok("datasub_win", "30000"); #$sel->select_pop_up("datasub_win"); #$sel->select_window_ok("datasub_win"); #$sel->close_ok(); $sel->pause("3000"); $sel->select_window_ok(""); $sel->pause("3000"); $sel->window_focus(); $sel->pause("3000"); $sel->click_ok("link=Attached Devices"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Attached Devices"); $sel->click_ok("link=Refresh"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Attached Devices"); $sel->click_ok("link=Backup Settings"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Backup Settings"); $sel->click_ok("link=Erase"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Backup Settings"); $sel->click_ok("link=No"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Backup Settings"); $sel->click_ok("link=Backup"); $sel->click_ok("link=Set Password"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Set Password"); $sel->click_ok("link=Diagnostics"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Diagnostics"); $sel->click_ok("link=Ping"); $sel->alert_is("Invalid IP address, please re-enter! "); $sel->click_ok("link=Look Up"); $sel->alert_is("Internet name can not be blank! "); $sel->click_ok("link=Display"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Diagnostics"); $sel->click_ok("link=Back"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Diagnostics"); $sel->choose_cancel_on_next_confirmation_ok(); $sel->click_ok("link=Reboot"); $sel->confirmation_is("Restarting the Sky Hub will terminate any activ +e connections to the Internet"); $sel->click_ok("link=Router Upgrade"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Router Upgrade"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Router Upgrade"); $sel->click_ok("link=Advanced"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > WAN Setup"); $sel->click_ok("name=dmz_enable"); $sel->click_ok("name=dmz_enable"); $sel->click_ok("name=rspToPing"); $sel->click_ok("name=rspToPing"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > WAN Setup"); $sel->click_ok("link=Dynamic DNS"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Dynamic DNS"); $sel->click_ok("name=dyndnsEnable"); $sel->click_ok("name=dyndnsEnable"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Dynamic DNS"); $sel->click_ok("link=LAN IP Setup"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > LAN IP Setup"); $sel->click_ok("name=dhcp_server"); $sel->click_ok("name=dhcp_server"); $sel->click_ok("link=Edit"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("link=Delete"); $sel->alert_is("No entry selected. Click a radio button to select an +entry."); $sel->click_ok("link=Add"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Attached Devices"); $sel->click_ok("link=Add"); $sel->alert_is("Invalid MAC address! The MAC address must be 12 hexade +cimal digits and seperated by colon (e.g 01:23:45:67:89:ab)."); $sel->click_ok("link=Refresh"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Attached Devices"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > LAN IP Setup"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > LAN IP Setup"); $sel->click_ok("link=Remote Management"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Remote Management"); $sel->click_ok("name=remote_mg"); $sel->click_ok("name=remote_mg"); $sel->click_ok("name=rm_access"); $sel->click_ok("xpath=(//input[\@name='rm_access'])[2]"); $sel->click_ok("xpath=(//input[\@name='rm_access'])[3]"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Remote Management"); $sel->click_ok("link=UPnP"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > UPnP"); $sel->click_ok("name=enblUpnp"); $sel->click_ok("name=enblUpnp"); $sel->click_ok("link=Refresh"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > UPnP"); $sel->click_ok("link=Cancel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > UPnP"); $sel->click_ok("link=Support"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Sky Support"); $sel->click_ok("//div[\@id='leftcolcontent']/table/tbody/tr[4]/td/b/a/ +u"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router"); $sel->click_ok("link=Support"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Sky Support"); $sel->click_ok("//div[\@id='leftcolcontent']/table/tbody/tr[5]/td/b/a/ +u"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("link=Support"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Sky Support"); $sel->click_ok("link=Licensing Information"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Sky Support"); $sel->click_ok("link=Support"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Sky Support"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Change Router Password"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Set Password"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Disable or Enable Wireless Network"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("name=enable_ap"); $sel->click_ok("name=enable_ap"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Change Wireless Network Name"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Change Wireless Channel"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Wireless Settings"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Change Parental Controls"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Block Sites"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Revert to factory default settings"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Backup Settings"); $sel->click_ok("link=No"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Backup Settings"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->choose_cancel_on_next_confirmation_ok(); $sel->click_ok("link=Reboot Router"); $sel->confirmation_is("Restarting the Sky Hub will terminate any activ +e connections to the internet"); $sel->click_ok("id=quick-options"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Refresh Summary Status"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("id=header"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->click_ok("link=Logout"); $sel->wait_for_page_to_load_ok("100000"); $sel->title_is("Sky Broadband Router > Home"); $sel->close(); } }

Comment on Perl and Selenium Grid 2 - Redirecting STDOUT not working how I need it to.
Download Code
Re: Perl and Selenium Grid 2 - Redirecting STDOUT not working how I need it to. (or die)
by tye (Cardinal) on Nov 20, 2012 at 15:30 UTC
    open ... or die "Failed to write ...: $!\n";

    Including $! will give you a big hint about why 'open' is failing. Replacing "..." with the path you gave to 'open' (by storing it in a variable first) will likely also be useful.

    - tye        

      Thank you for the response. I added the code you suggested and didn't get any errors. The 'print' command gets written to each file but the results of the following Selenium tests do not which suggests they are output by something other than STDOUT ?

        The most likely "something other than STDOUT" would be STDERR. So redirect that. Or read the documentation (or code) of tools that aren't writing to STDOUT?

        - tye        

Re: Perl and Selenium Grid 2 - Redirecting STDOUT not working how I need it to.
by marquezc329 (Scribe) on Nov 20, 2012 at 15:55 UTC

    hello Doozer,

    Not a solution, but I rewrote the beginning of your script using a hash to avoid having to test before $hostname assignment. Loop variables probably shouldn't be declared globally either. Anyways, here's what I came up with.

    #!/usr/bin/perl use strict; use warnings; my %hosts = ( "192.168.1.206" => "Windows_XP", "192.168.1.207" => "Windows_Vista", "192.168.1.208" => "Windows_7", "192.168.1.230" => "MAC_OSX_Mountain_Lion", "192.168.1.231" => "MAC_OSX_Lion" ); my @browsers = qw/ firefox googlechrome iexplore /; my $browserurl = 'http://admin:sky@192.168.0.1/'; my $date = (localtime); foreach my $browser (@browsers) { foreach my $host (keys %hosts) { my $sel = Test::WWW::Selenium->new(host => "$host", port => 5555, browser => $browser, browser_url => "http://192. +168.0.1" ); my $hostname = $hosts{$host}; open STDOUT, "+>/varwww/cgi-bin/Perl-Scripts/$hostname\_$brows +er\.txt" or die "Failed to open $hostname\_$browser.txt: $!\n"; print "$host - $hostname - $browser - $date\n\n"; } }

    Happy Tuesday.

      Thanks! I really appreciate you taking the time to do that and I will definitely look at using it in my script to make things neater.

      On another note, I actually solved my problem with a lucky find on Google instigated by tye's last comment. The package Test::More has a function where test output can be redirected to any file I like and can be split apart just like STDOUT and STDERR. Thanks for the input you all have already made and if there are any more suggestions to streamline the script I would welcome them!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004730]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2014-07-23 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (144 votes), past polls