setting the browser from command line using watir webdriver - watir-webdriver

my learning for ruby and watir webdriver is ongoing and i was wondering if there is a way to set the browser to be used from the command line when running a rb script, eg c:\ruby login.rb --set browser
I've done some looking around and can find nothing specific, any help would be greatly appreciated.
thanks

This is what I do.
My code has the following:
case ENV['BROWSER']
when 'ff', 'Firefox'
#b = Watir::Browser.new :firefox
browser_name = 'Firefox'
when 'chrome'
#b = Watir::Browser.new :chrome
browser_name = 'Chrome'
when 'debug'
debug_profile = Selenium::WebDriver::Firefox::Profile.new
debug_profile.add_extension "firebug-1.9.1-fx.xpi"
#b = Watir::Browser.new :firefox, :profile => debug_profile
browser_name = 'Firefox (Firebug)'
when 'mobile'
mobile_profile = Selenium::WebDriver::Firefox::Profile.new
mobile_profile['general.useragent.override'] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)
AppleWebKit/420+ (KHTML, like Gecko) Version/3.0
Mobile/1A535b Safari/419.3"
#b = Watir::Browser.new :firefox, :profile => mobile_profile
browser_name = 'Mobile'
when 'ie'
#b = Watir::Browser.new :ie
browser_name = 'Chrome'
when 'headless'
#b ||= Watir::Browser.new :phantomjs
browser_name = 'phantomjs'
else
#b = Watir::Browser.new :firefox
browser_name = 'Firefox'
##b ||= Watir::Browser.new :chrome
end
I can then set the browser variable in the cmd prompt before I run the test:
set BROWSER=Environment_ID

I do the same with an environment variable; taking from #Skjoldan code, this would be the minimal to set your browser via environment variable:
case ENV['BROWSER']
when 'ff'
#b = Watir::Browser.new :firefox
else
#b = Watir::Browser.new :chrome
end
No environment variable set, defaults to chrome.

Related

Alias definition in .perldb (Perl Debugger) for expression evaluation 'x' doesn't work

I am wondering if there is a way to define aliases in the Perl Debugger configuration file .perldb for the command x which dumps expressions.
All the following imaginable varieties of alias definitions doesn't work:
cat .perldb
$DB::alias{ 'x0' } = "x '%states'"; # Does not work
$DB::alias{ 'x1' } = 'x %config'; # Does not work
$DB::alias{ 'x2' } = '"x %config"'; # Does not work
$DB::alias{ 'x3' } = 'x "%config, $foo"'; # Does not work
$DB::alias{ 'x4' } = 'x "#L"'; # Does not work
$DB::alias{ 'x5' } = 'x %config, $foo'; # Does not work
$DB::alias{ 'x6' } = '"x %config, $foo"'; # Does not work
$DB::alias{ 'xc' } = 'x %config'; # Does not work
$DB::alias{ 'pFoo1' } = 'print("foo=$var1\n")'; # Works
$DB::alias{ 'pFoo2' } = 'print("$var2\n")'; # Works
$DB::alias{ 'p1' } = 'print "\$_ = $_"'; # Works
$DB::alias{ 'code' } = 's/^.*$/l 1+99999/'; # Works
After starting the debugger with 'perl -d script.pl' and display the aliases with = I get:
code s/^.*$/l 1+99999/
p1 print "\$_ = $_"
pFoo1 print("foo=$var1\n")
pFoo2 print("$var2\n")
x0 x '%states'
x1 x %config
x2 "x %config"
x3 x "%config, $foo"
x4 x "#L"
x5 x %config, $foo
x6 "x %config, $foo"
xc x %config
Any ideas if the alias definitions in .perldb expect a special syntax or does it just not work?
This is not documented, but the contents of the %alias hash are strings that are used as the right-hand side of a $cmd =~ ... expression. That is, to work as expected, the aliases must be s/// commands.
Try something like
$DB::alias{ 'x1' } = 's/^.*/x %config/';

PL/Perl trigger cannot use ENV variables defined in .bashrc

I'm trying to use an environment variable (for example HOME) inside a PL/perl trigger in postgresql and it appears to be empty.
Running printenv | grep HOME in the terminal returns the desired path.
I am defining the trigger as
CREATE EXTENSION IF NOT EXISTS plperlu;
CREATE OR REPLACE FUNCTION update_solved() RETURNS trigger AS $update_solved$
elog(NOTICE, "Hello");
elog(NOTICE, $ENV{'HOME'});
return;
$update_solved$ LANGUAGE plperlu;
When a query activates the trigger, I get the output
NOTICE: Hello
NOTICE:
I checked what %ENV contains inside the script(using use Data::Dumper;
elog(NOTICE, Dumper(%ENV));:
$VAR1 = 'LANG';
$VAR2 = 'en_US.UTF-8';
$VAR3 = 'LC_TIME';
$VAR4 = 'C';
$VAR5 = 'LC_MONETARY';
$VAR6 = 'C';
$VAR7 = 'PGSYSCONFDIR';
$VAR8 = '/etc/postgresql-common';
$VAR9 = 'PG_GRANDPARENT_PID';
$VAR10 = '513';
$VAR11 = 'PGLOCALEDIR';
$VAR12 = '/usr/share/locale';
$VAR13 = 'LC_CTYPE';
$VAR14 = 'en_US.UTF-8';
$VAR15 = 'LC_COLLATE';
$VAR16 = 'en_US.UTF-8';
$VAR17 = 'PGDATA';
$VAR18 = '/var/lib/postgresql/9.6/main';
$VAR19 = 'LC_MESSAGES';
$VAR20 = 'en_US.UTF-8';
$VAR21 = 'LC_NUMERIC';
$VAR22 = 'C';
$VAR23 = 'PWD';
$VAR24 = '/var/lib/postgresql';
$VAR25 = 'PG_OOM_ADJUST_FILE';
$VAR26 = '/proc/self/oom_score_adj';
Do you know how I can set the env variables that I need that are defined in the terminal session, to be used inside the PL/perl trigger?
Although you are correctly calling the untrusted plperlu variant of PL/Perl, the initial environment is still the same restricted environment as for the normal "trusted" PL/Perl.
One solution would be to run a sub-shell which will then initialise its environment. Something like:
my $home = `bash -lc "echo \$HOME"`;
You may need to tweak the arguments to bash to get what you want. The above command explicitly runs bash as I'm not sure what default shell you are going to have, and you mentioned your .bashrc so the -l might be needed to ensure that gets read.
You could also explicitly read the information you want from an arbitrary text file on the system, which might be easier than messing with environment variables.

Perl preprocessor appended to other variable

I am trying to write following code, where I want $varPRE1 to be $var1 and $varPRE2 to be $var2 after preprocessor is evaluated, but it is not working. Is there a work around?
#define PRE1 1
#define PRE2 2
my $var1 = 10;
my $var2 = 20;
print $varPRE1;
print $varPRE2;
What you wrote doesn't make sense even for the C preprocessor, since varPRE1 would get parsed as one token.
What you want to do is sort of possible, using what are called symbolic references, but it is a bad idea.
$PRE1 = '1';
$PRE2 = '2';
$var1 = 10;
$var2 = 20;
print ${'var' . $PRE1}; # same as print $var1 => 10
print ${'var' . $PRE2};
This will not work under use strict refs. It is also a bad idea to not use strict refs.

Why is Perl HTTP::Response not decoding this apostrophe?

I'm using
my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/5.0 (Windows NT 6.1; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 ");
my $url = "http://somedomain.com/page/";
my $req = new HTTP::Request 'GET' => $url;
$req->header('Accept' => 'text/html');
my $response = $ua->request($req);
my $html = $response->decoded_content;
to get a web page. On this page, Abobo's Big Adventure appears. In $request->content and $request->decoded_content, this is shown as Abobo's Big Adventure.
Is there something I can do to make this decode correctly?
Why, that is completely valid HTML! However, you can decode the Entities using HTML::Entities from CPAN.
use HTML::Entities;
...;
my $html = $response->decoded_content;
my $decoded_string = decode_entities($html);
The docs for HTTP::Response::decoded_content state that the Content-encoding and charsets are reversed, not HTML entities (which are a HTML/XML language feature, not really an encoding).
Edit:
However, as ikegami pointed out, decoding the entities immediately could render the HTML unparsable. Therefore, it might be best to parse the HTML first (e.g. using HTML::Tree), and then only decoding the text nodes when needed.
use HTML::TreeBuilder;
my $url = ...;
my $tree = HTML::TreeBuilder->new_from_url($url); # invokes LWP automatically
my $decoded_text = decode_entities($tree->as_text); # dumps the tree as flat text, then decodes.
I'm guessing there probably is an ampersand there before the hash mark. Making it the HTML entity expressed ' These aren't that hard to change. You can do something like this:
my $content = $response->decoded_content;
$content
=~ s{(&#(\d{2,3});)}{
$2 < 128 ? ord( $2 ) : $1
}gem
;
The range check pretty much assures you you're dealing with ASCII. If you want to get more complex, you could also put together a hash of values, and change it like so:
my %entity_lookup
= ( 150 => '-'
, 151 => '--' # m-dash
, 160 => ' '
...
);
...
$content
=~ s{(&#(\d+);)}{
$2 < 128 ? ord( $2 ) : $entity_lookup{ $2 } // $1
}gem
;
But that would be up to you.

Is there an equivalent to the perl debugger 'x' in pdl2 (or Devel::REPL)?

I am using pdl2 (the PDL shell) also as a my default Perl interactive shell (it loads all the nice plugins for Devel::REPL). But I am missing the x dumper-printing alias. p is nice for piddles but it does not work for a normal array ref or hash ref. I have loaded Data::Dumper but it lacks an easy way of controlling depth and I like the way you can quickly set depth limits with x, e.g. x 2 $deep_datastruct for complex data structures. But with Data::Dumper the process is more cumbersome:
pdl> say $c
HASH(0x53b0b60)
pdl> p $c
HASH(0x12b14018)
pdl> use Data::Dumper
pdl> p Dumper $c
$VAR1 = {
'c' => {
'c' => 3,
'a' => 1,
'b' => {
'c' => '3',
'a' => '1',
'b' => '2'
}
},
'a' => 1,
'b' => 4
};
pdl> $Data::Dumper::Maxdepth = 1;
pdl> p Dumper $c
$VAR1 = {
'c' => 'HASH(0x97fba70)',
'a' => 1,
'b' => 4
};
In the Perl debugger you can achieve the same thing with x 1 $c directly. Does pdl2 have something similar and so concise?
[update]
And related with this question: does pdl2 or Devel::REPL have convenience functions like the Perl debugger commands m or y? Or should one create a module with PadWalker and export them? I would like to use a real REPL instead of the Perl debugger as an interactive shell, but still the Perl debugger has some important things that I don't know how to do with Devel::REPL or pdl2.
For example to see all variables (pdl2 only show piddles):
pdl> help vars
PDL variables in package main::
Name Type Dimension Flow State Mem
----------------------------------------------------------------
no PDL objects in package main::
By the way, does someone know a Devel::REPL plugin for listing all the variables in use (like y in the debugger, but only the names, not the values) and then have a x-like to dump the wanted one?
It looks like Devel::REPL provides an straightforward alternative for your first question. Create a file called '.perldlrc' in your home directory that looks like:
use Data::Dumper;
sub x {
my $depth = shift;
$Data::Dumper::Maxdepth = $depth;
print Data::Dumper->Dump([#_])
}
Unfortunately, you need a comma as in:
pdl> x 1, $c
It looks like you can implement the other commands with this same control-file approach. I don't see a way to get rid the need for the comma, although I don't think there's any reason Devel::REPL cannot be made to recognize and parse these kinds of commands.
The Devel::REPL shell re.pl already dumps the value of the last expression by default:
[foo#host]$ re.pl
$ { a => 23, b => 34}
$HASH1 = {
a => 23,
b => 34
};
$