Getting "http///" in address bar - watir

"URL.txt" file contains "www.google.com". the puts shows the value in the console. But after launching IE, in address bar it shows "[http:///]" and program tarminates.
Here is my Watir code.
require 'rubygems'
require 'watir'
File.open("URL.txt", "r").each_line do |line|
puts line
end
a = Watir::Browser.new
a.goto '#{line}'
am I doing anything wrong?

You are asking IE to go the url #{line}. If you do this manually, IE automatically goes to http:///.
You have 2 issues:
When you do '#{line}', the single quotes mean that there is no string interpolation - ie you get what you see. To have the string interpolation, you need double quotes - "#{line}". However, in this case, you could simply do line (ie line is already a string).
Assuming you fixed issue 1, the next problem is that line is undefined in a.goto "#{line}". You have only defined it within the File.open block. It is no longer available when you get to the a.goto.
Assuming that the script is meant to visit each url in the file, you probably meant to do:
require 'rubygems'
require 'watir'
File.open("URL.txt", "r").each_line do |line|
puts line
a = Watir::Browser.new
a.goto line
end
Or, if you were to visit each page using the same browser:
require 'rubygems'
require 'watir'
a = Watir::Browser.new
File.open("URL.txt", "r").each_line do |line|
puts line
a.goto line
end

Related

Spreadsheet::ParseExcel::SaveParser Issues - Cannot Obtain a Defined Workbook Object

I am trying to use the Spreadsheet::ParseExcel::SaveParser plugin using example code at both metacpan.org and on SO and I cannot define the template (workbook).
I have played around with variations on the new statement, quotes, file parh, etc - nothing works. I put a die after the template statement and it prints the error message. Without that I have either a $template->worksheet() or worksheets() statement and if I skip the die I get a different message. I confirmed that the path to the Excel file is correct. I also new()'ed Spreadsheet::ParseXLSX instead and the code got past the template undefined problem - of course it crashed when I tried to do an AddCell.
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
my $saveParser = Spreadsheet::ParseExcel::SaveParser->new();
my $template = $saveParser->Parse("some Excel file verified to exist");
die "Error! Template not defined!\n" if (!defined($template));
dies
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
my $saveParser = Spreadsheet::ParseExcel::SaveParser->new();
my $template = $saveParser->Parse("some Excel file verified to exist");
my $worksheet = $template->worksheet(0);
Can't call method "worksheet" on an undefined value at ../bin/update_tp.pl line nnn. It also errors out if I use the worksheet name instead of number.
Obviously I expect the $saveParser->Parse command to return a valid object so I can work with it - it doesn't. FYI all modules I'm using are at the current rev of 0.65 except WriteExcel (which isn't relevant yet), which is 2.4.
Think you'd want to track down what error-handling is available for the Parse() function by reading the CPAN page for this module. Find and adapt that error-handling to give you an inkling of why Parse() would be failing. Your error about "worksheet on an undefined value" is indicative of that call failing. Could be you're not escaping file paths, maybe you don't have read permissions on the file, lots of other reasons Parse() could be failing. Eww, just went and looked myself, and there's fairly inadequate error-handling documented for that function. Maybe try die'ing with some error-handling variables? See https://perldoc.perl.org/perlvar.html#Error-Variables

Incrementing Through URLs and Downloading

I would just like a simple browser automation that increments one number in a URL and downloads the information from that place. For example, if the address looks like this:
www.test.com/something/part1_0.jpg
How could I increment the '1' and download the file from each successive web page?
Thanks
P.S. I'm using OS X 10.9
Here's a ruby solution using open-uri:
require 'open-uri'
(1..100).each do |num|
File.open("part#{num}_0.jpg", 'wb') do |f|
f.write open("www.test.com/something/part#{num}_0.jpg").read
end
end
This snippet A) creates a range of numbers; B) iterates over the range of numbers; C) opens an image file in binary mode and interpolates the current number into the file name; and D) reads the image from the URL and writes it.
But the easiest way would probably be to use curl from your command line:
curl -O www.test.com/something/part[1-100]_0.jpg
Depending on the number of webpages that you need to access, modify the numbers in brackets accordingly.

linux - running system command in R and then writing output to a file

I have R code as below. Below code resides in a file called 'iot.R'. I am executing it in Linux.
I want to print content of variable 'fileinformation' to a file mentioned by file=fileConn...
I thought that the 3rd line will solve the issue, but it is not giving the required output :(
fileinformation = system(paste("file", filenames[1]))
#print(fileinformation)
cat(print(fileinformation),"\r\n","\r\n", file=fileConn)
When I run the file, i get below result. It prints to my screen, rather than writing to the file :(
> source('iot.R')
CH7Data_20130401T135010.csv: ASCII text, with CRLF line terminators
[1] 0
--------------------update1
I also tried below command, but didnt get the expected rsult
cat(capture.output(fileinformation),"\r\n","\r\n", file=fileConn)
You need to set the intern argument to TRUE in your call to system. For instance:
fileinformation<-system("file cinzia_2.gif",intern=TRUE)
fileinformation
#[1] "cinzia_2.gif: GIF image data, version 89a, 640 x 640"
Of course I tried a file on my pc. Setting intern to TRUE the return value of system becomes the console output of the command. Then, when you call cat, you don't need to enclose fileinformation into print, but a simple cat(fileinformation,"\r\n","\r\n", file=fileConn) will suffice.
Hi Just a comment as I dont have enough rep to comment in the normal way. but cant you use
write.table
to save the output to a file? It may be easier?

Issue with filepath name, possible corrupt characters

Perl and html, CGI on Linux.
Issue with file path name, being passed in a form field, to a CGI on server.
The issue is with the Linux file path, not the PC side.
I am using 2 programs,
1) program written years ago, dynamic html generated in a perl program, and presented to the user as a form. I modified by inserting the needed code to allow a the user to select a file from their PC, to be placed on the Linux machine.
Because this program already knew the filepath, needed on the linux side, I pass this filepath in a hidden form field, to program 2.
2) CGI program on Linux side, to run when form on (1) is posted.
Strange issue.
The filepath that I pass, has a very strange issue.
I can extract it using
my $filepath = $query->param("serverfpath");
The above does populate $filepath with what looks like exactly the correct path.
But it fails, and not in a way that takes me to the file open error block, but such that the call to the CGI script gives an error.
However, if I populate $filepath with EXACTLY the same string, via hard coding it, it works, and my file successfully uploads.
For example:
$fpath1 = $query->param("serverfpath");
$fpath2 = "/opt/webhost/ims/DOCURVC/data"
A comparison of $fpath1 and $fpath2 reveals that they are exactly equal.
A length check of $fpath1 and $fpath2 reveals that they are exactly the same length.
I have tried many methods of cleaning the data in $fpath1.
I chomp it.
I remove any non standard characters.
$fpath1 =~ s/[^A-Za-z0-9\-\.\/]//g;
and this:
my $safe_filepath_characters = "a-zA-Z0-9_.-/";
$fpath1 =~ s/[^$safe_filepath_characters]//g;
But no matter what I do, using $fpath1 causes an error, using $fpath2 works.
What could be wrong with the data in the $fpath1, that would cause it to successfully compare to $fpath2, yet not be equal, visually look exactly equal, show as having the exact same length, but not work the same?
For the below file open block.
$upload_dir = $fpath1
causes complete failure of CGI to load, as if it can not find the CGI (which I know is sometimes caused by syntax error in the CGI script).
$uplaod_dir = $fpath2
I get a successful file upload
$uplaod_dir = ""
The call to the cgi does not fail, it executes the else block of the below if, as expected.
here is the file open block:
if (open ( UPLOADFILE, ">$upload_dir/$filename" ))
{
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
$msgstr="Done with Upload: upload_dir=$upload_dir filename=$filename";
}
else
{
$msgstr="ERROR opening for upload: upload_dir=$upload_dir filename=$filename";
}
What other tests should I be performing on $fpath1, to find out why it does not work the same as its hard-coded equivalent $fpath2
I did try character replacement, a single character at a time, from $fpath2 to $fpath1.
Even doing this with a single character, caused $fpath1 to have the same error as $fpath2, although the character looked exactly the same.
Is your CGI perhaps running perl with the -T (taint mode) switch (e.g., #!/usr/bin/perl -T)? If so, any value coming from untrusted sources (such as user input, URIs, and form fields) is not allowed to be used in system operations, such as open, until it has been untainted by using a regex capture. Note that using s/// to modify it in-place will not untaint the value.
$fpath1 =~ /^([A-Za-z0-9\-\.\/]*)$/;
$fpath1 = $1;
die "Illegal character in fpath1" unless defined $fpath1;
should work if taint mode is your issue.
But it fails, and not in a way that takes me to the file open error block, but such that the call to the CGI script gives an error.
Premature end of script headers? Try running the CGI from the command line:
perl your_upload_script.cgi serverfpath=/opt/webhost/ims/DOCURVC/data

Watir::Wait.until not working with frames

I'm automating an internal tool that is rife with frames, using Watir. I am able to manipulate all the various elements so I know I am identifying the frames correctly, but any time I attempt to use a Wait statement for any of these elements it fails. Tracking back through the error message, it always hits the activesupport gem in core_ext/time/calculations and it looks like it can't get the duration value, it gets set to false, and then the operation fails because it is expecting a Float. Is this a bug?
Using Ruby 1.8.7 and Watir 1.6.7
My code is:
require 'rubygems'
require 'watir/testcase'
require 'main_setup'
require 'win32ole'
require 'common'
class Smoketest < Watir::TestCase
include CommonCode
def test_AddEdit_Endpoint
Watir::Wait.until { #b.link(:id,"lbShowEndpointForm").exists? }
end
end
Error is the following:
test_basic_smoke(Smoketest):
TypeError: can't convert false into Float
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/core_ext/time/calculations.rb:278:in `plus_without_duration'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/core_ext/time/calculations.rb:278:in `+'
C:/Ruby187/lib/ruby/gems/1.8/gems/commonwatir-1.6.7/lib/watir/wait.rb:15:in `until'
C:/qa/trunk/CCAdmin/Automation/CCAdmin/lib/smoketest.rb:27:in `test_basic_smoke'
So, which line is the C:/qa/trunk/CCAdmin/Automation/CCAdmin/lib/smoketest.rb:27?
I thought the correct usage for the command was wait_until, unless it's changes from Watir 1.6.5, http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/Waiter.html

Resources