Changing default download directory in Chrome Webdriver - perl

Can anyone let me know how can I change the default download location for Chrome using Selenium-Perl. I am using Chrome Webdriver and the Perl module Selenium::Remote::Driver. I got code for Java but not in Perl for this task.

I do not have the test setup but passing below as desired_capabilities or extra_capabilities to the constructor should work fine.
'download.default_directory', 'C:\New_Folder'
Snippet (untested):
my $driver = Selenium::Remote::Driver->new(
'browser_name' =>'chrome',
'extra_capabilities' => {
'chromeOptions' => {
'prefs' => {
'download.default_directory' => 'C:\New_Folder'
}
}
}
);
Edit: Difference between Selenium::Chrome and Selenium::Remote::Driver
Selenium::Chrome allows you to use the ChromeDriver without needing the JRE or a selenium server running. If the ChromeDriver binary is not found, it falls back to the default Selenium::Remote::Driver.

I had troubles understanding the difference mentioned between Selenium::Chrome and Selenium::Remote::Driver. Here's what I got to work:
my $driver = Selenium::Chrome->new(
extra_capabilities => {
'goog:chromeOptions' => {
prefs => {
'download.default_directory' => '/tmp'
},
args => [ 'window-size=1950,500' ]
}
}
);

Related

Perl Dancer2 infinite-loop on get when calling a method

I made a photobooth with Dancer some years ago and it worked fine.
Now I try to move this to Dancer2. However, It's not working anymore, because I have some infinite-loop.
Let's say my app looks like this:
package App;
use Dancer2;
# Photobox is my pm file with all the magic
use Photobox;
my $photobox = photobox->new();
get '/photo' => sub {
my $photo;
# Trigger cam and download photo. It returns me the filename of the photo.
$photo = $photobox->takePicture();
# Template photo is to show the photo
template 'photo',
{
'photo_filename' => $photo,
'redirect_uri' => "someURI"
};
}
takePicture() looks like this:
sub takePicture {
my $Objekt = shift;
my $return;
my $capture;
$return = `gphoto2 --auto-detect`;
if ($return =~ m/usb:/) {
$capture = `gphoto2 --capture-image-and-download --filename=$photoPath$filename`;
if (!-e $photoPath.$filename) {
return "no-photo-error.png";
}
else {
return $filename;
}
} else {
die "Camera not found: $return";
}
}
When I now call /photo, it'll result in an infinite loop. The browser is "frefreshing" all the time and my cam is shooting one photo after the other. But it never redirects to /showphoto.
It was working with Dancer(1) when I run the application just by perl app.pl from the bin directory. How I use Dancer2 und run it by using plackup app.psgi
I tried to put it into a before hook, but it changed nothing.
Update:
I figured out a way to work around this issue.
First I refactored my code a bit. Basic idea was to split the take photo and show photo operations into two different routes. This makes it easier to see what happens.
get '/takesinglephoto' => sub {
my $photo;
$photo = takePicture();
$single_photo=$photo;
redirect '/showsinglephoto';
;
get '/showsinglephoto' => sub {
set 'layout' => 'fotobox-main';
template 'fotobox_fotostrip',
{
'foto_filename' => $single_photo,
'redirect_uri' => "fotostrip",
'timer' => $timer,
'number' => 'blank'
};
};
And I moved the takePicture method just into my Dancer main App.pm.
Now I recognized from the log output, that the browser does not load the '/takesinglephoto' page once, but refreshes it every some secons. I think the reason is, that takePicture() takes some seconds to run and to return the output. And Dancer does not wait until it ends. With every reload, it triggers the takePicture() again and that causes the infinite-loop.
I worked around this by implementing a simple check to run takePicture() just once.
# define avariable set to 1 / true
my $do_stuff_once = 1;
get '/takesinglephoto' => sub {
my $photo;
# check if variable is true
if ($do_stuff_once == 1) {
$photo = takePicture();
$single_photo=$photo;
# set variable to false
$do_stuff_once = 0;
}
redirect '/showsinglephoto';
};
get '/showsinglephoto' => sub {
# set variable back to true
$do_stuff_once = 1;
set 'layout' => 'fotobox-main';
template 'fotobox_fotostrip',
{
'foto_filename' => $single_photo,
'redirect_uri' => "fotostrip",
'timer' => $timer,
'number' => 'blank'
};
};
Now it still refreshes /takesinglephoto, but it does not trigger takePicture() again and again and finally, when the method returns the photo filename, it redirects to /showsinglephoto.
I would call this a workaround. Is there a better way to solve this?
BR
Arne

How do I ensure a Puppet exec resource always includes `set -o pipefail`?

In a particular project, I have a lot of Puppet exec resources with pipes. This seems to work just fine.
exec { 'foobar':
command => 'foo | bar',
}
However, there are occasions where foo fails. The default behavior is to report the exit code only for the last command in the pipeline. I can fix this manually.
exec { 'foobar':
command => 'set -o pipefail; foo | bar',
provider => shell,
}
But, I want to make sure this happens in all these cases automatically. I'd like to avoid manual find/replace and auditing all uses of exec.
Am I missing some useful attribute?
Is there a wrapper I can use?
Am I, unfortunately, looking at a custom resource?
Am I missing some useful attribute?
No, Exec has no attribute that would automatically prepend additional code to the command.
Is there a wrapper I can use?
I'm not confident I understand what you mean by a "wrapper", but do see below.
Am I, unfortunately, looking at a custom resource?
If you're asking whether you need to implement a native custom type, then surely not. You can undoubtedly address this problem with a (DSL-level) defined type, though you will need to adjust all your Exec declarations to be declarations of the defined type instead. This may be what you mean by a "wrapper" -- I'm fairly sure there's no existing one for your particular purpose, but it shouldn't be hard to create one:
define mymodule::exec (
command => $title,
creates => 'NOT SET',
cwd => 'NOT SET',
# ... all other regular parameters ...
) {
$real_provider = $provider ? { 'NOT SET' => 'shell', default => $provider }
if $real_provider == 'shell' {
$real_command = "set -o pipefail; $command"
} else {
warning('Non-shell command declared via mymodule::exec')
$real_command = $command
}
exec { $title:
command => $real_command,
provider => $real_provider,
creates => $creates ? { 'NOT SET' => undef, default => $creates },
cwd => $cwd ? { 'NOT SET' => undef, default => $cwd },
# ... all remaining regular parameters ...
}
}

How to solve lighttpd "No input file specified."

Last time I was using Apache2+PHP5 as my web server and it run normally unless too slow when server is process my script and I had change it to lighttpd + fastcgi. It faster than and low memory usage.
My problem is when lighttpd running some time it "No input file specified." but some time is ok. But when I restart lighttpd every come to normally.
I don't know why and how to solve it.
This is my config.
$SERVER["socket"] == ":80" {
$HTTP["host"] == "xxx.xxx.xxx.xxx" {
server.document-root = "/var/www/public_html"
server.errorlog = "/var/www/public_html/logs/error.log"
accesslog.filename = "/var/www/public_html/logs/access.log"
compress.cache-dir = "/var/www/public_html/cache"
}
$HTTP["host"] == "sub.domain.com" {
server.document-root = "/var/www/public_html"
server.errorlog = "/var/www/public_html/logs/error.log"
accesslog.filename = "/var/www/public_html/logs/access.log",
compress.cache-dir = "/var/www/public_html/cache"
}
index-file.names = ( "index.php", "index.html", "index.htm", "default.htm" )
url.rewrite-if-not-file = (
"^/image(.*)" => "/image-api.php$1",
"^/go/([a-zA-Z0-9_-]+)" => "/index.php?go=$1",
"^/oembed(.*)" => "/oembed_provider/index.php$1",
"^/player$" => "/library/plugin/video-player/player.swf",
"^/v(.*)" => "/cvd.php$1",
"^/me" => "/user.php",
"^/#(.*)\?(.*)" => "/profile.php?indentity=$1&$2",
"^/#(.*)" => "/profile.php?indentity=$1",
"^/url?g=(.*)" => "/url.php?g=$1",
"^/social_auth/(.*)" => "/partner_api/$1.php",
"^/c/(.*)" => "/view.php?view=$1",
"^/u/(.*)" => "/profile.php?indentity=$1",
"^/project/(.*)" => "/section.php?page=$1",
"^/min/(.*)" => "/mini/index.php$1",
"^/src/(.*)" => "/src/$1",
"^/library/(.*)" => "/library/$1",
"^/\?(.*)" => "/index.php?$1",
"^/(.*)\?(.*)" => "/page.php?p=$1&$2",
"^/(.*)" => "/page.php?p=$1"
)
$HTTP["host"] == "domain.org" {
url.redirect = ("/(.*)$" => "https://domain.com/$1")
}
$HTTP["host"] == "domain.info" {
url.redirect = ("/(.*)$" => "https://domain.com/$1")
}
$HTTP["host"] == "domain.net" {
url.redirect = ("/(.*)$" => "https://domain.com/$1")
}
}
From the FAQ, it looks like there are several possibilities:
I get the error "No input file specified" when trying to use PHP
Sadly, this error message can mean a lot of things.
A common explanation attempt: PHP is unable to locate or open the file which it
is supposed to parse.
This can have a lot of reasons:
You forgot to
add ''cgi.fix_pathinfo=1 to your php.ini'' file. See the comments in
the PHP docs. The issue here is that the environment variable
SCRIPT_FILENAME is not being passed to PHP.
Make sure you did not set
doc_root or userdir in php.ini, or if you have set it, make sure it
has the correct value (doc_root should match lighttpd's
server.document-root option in this case)
If open_basedir is set, make
sure the requested file is below one of the directories which is
specified there. In the past PHP parsed files which were not inside
open_basedir as well, but this security problem was fixed (in
php-5.2.3 or so).
If you are running PHP with different permissions
than lighttpd (spawn-fcgi with -u/-g, execwrap, suexec, ...), check
that PHP can really read the file
If you are unable to find / fix the
problem, you can use strace to see if it is a (OS-related) permission
problem (look out for stat*(...YOURFILE...) = RETURNCODE). It might
help to set max-procs to 1 and PHP_FCGI_CHILDREN as well (see fastcgi
docs) in that case, so that you can easily attach strace to the
correct php-cgi process.

Puppet parameterized classes and changing parameters

I am trying to understand Puppet parameterized classes. I have a parameterized class defined this way:
class defaults(
$no_samba = 'FALSE'
)
{
if ($no_samba =~ /TRUE/) {
notify { "will not install samba": } ;
} else {
# install samba here
}
# More server install tasks here...
}
Furthermore, I define a basenode as follows:
node basenode
{
class {'defaults':
no_samba => 'FALSE',
}
}
and then I instantiate a server:
node myserver1 inherits basenode {
Class['defaults'] { no_samba => 'TRUE' }
}
However, this does not work. The myserver1 node does not show the notify message indicating that samba will not be installed.
Here's my non-typo answer - I think you're running into http://projects.puppetlabs.com/issues/7890
Here's a code sample where I tweaked your code to get the effect you're looking for, based on the rewritten example in the ticket:
class defaults(
$no_samba = 'FALSE'
)
{
notify {"no_samba_hack" :
message => "$no_samba";
}
if ($no_samba =~ /TRUE/) {
notify { "will not install samba": }
} else {
# install samba here
}
# More server install tasks here...
}
class basenode($no_samba="FALSE") {
class {defaults: no_samba => $no_samba}
}
node yourserver {
class { 'basenode' : no_samba => 'TRUE'}
}
When I run that with 'puppet apply sample.pp' with puppet 2.7.11 on Ubuntu 12.04, I get the following output:
notice: will not install samba
notice: /Stage[main]/Defaults/Notify[will not install samba]/message: defined 'message' as 'will not install samba'
notice: TRUE
notice: /Stage[main]/Defaults/Notify[no_samba_hack]/message: defined 'message' as 'TRUE'
notice: Finished catalog run in 0.05 seconds
Was samba installed on myserver1, and/or did any of the other server install tasks get triggered ? If only the notify message wasn't printed, then it may really be a problem with notify type versus the notice function.
Notify should look like "notify{"i have curly brackets and a trailing colon":}
Notice is called like a function: notice("i use parenthesis")
Try changing 'notify' to 'notice' and see if it works. You may also want to check the puppet syntax with 'puppet parser validate default.pp' (assuming your default class is in default.pp)
I believe it has to do with scope. It looks like you're creating the 'default' class in the base node and then setting a resource default for the 'default' class after the fact in something that inherits that basenode.
http://docs.puppetlabs.com/guides/language_guide.html
"Defaults are not global — they only affect the current scope and scopes below the current one."
Here is simple example:
class apache-setup {
class { 'apache':
mpm_module => 'prefork',
}
}
include apache-setup
Or:
class { '::mysql::server':
config_file => '/etc/my.cnf',
root_password => 'root', # Sets MySQL root password.
override_options => {
'mysqld' => {
'max_connections' => '512',
'max_allowed_packet' => '256M',
'log' => 'ON',
'log_slow_queries' => 'ON',
'general_log' => 'ON',
'wait_timeout' => '28800',
}
}
}

"Can't call method "dir_path" on an undefined value" when running Mason component on the command line

Greetings,
I'm trying to develop some tests for Mason components which requires running them on the command line instead of the web server. When I try this, I get an error:
perl -MHTML::Mason::Request -MHTML::Mason::Interp -I./lib \
-e '$int = HTML::Mason::Interp->new( data_dir => "/home/friedo/cache", comp_root => "/home/friedo/comps" ); $m = HTML::Mason::Request->new( comp => "/dummy", interp => $int ); $m->comp("/dummy")'
Results in:
Can't call method "dir_path" on an undefined value at lib/HTML/Mason/Request.pm line 1123.
The error is thrown when the call to ->comp is attempted. I can't figure out what's wrong with the configuration. The component is there and appears to be compiled just fine, and it works via Apache.
This is using HTML::Mason 1.35.
Edit: Let's try a bounty for this one. The alternative is me having to dive deep into Mason's guts! :)
Edit again: Thanks very much to David for pointing out the crucial detail that I missed for getting this to work.
This was actually for a test framework that needed to exercise a module that calls some Mason comps -- under normal operation the module is provided with a Mason request object to use for that purpose, but I couldn't get that to work offline. The key was using an Interpreter object instead, so I ended up doing the following, which is a little silly but makes the tests work:
sub _mason_out {
...
my $buf;
if ( $ENV{MASON_TEST} ) {
my $int = HTML::Mason::Interp->new( comp_root => $self->{env}->comp_dir,
out_method => \$buf );
$int->exec( $comp, %args );
} else {
my $m = $self->{mason_object};
$m->comp( { store => \$buf }, $comp, %args );
}
return $buf;
}
I think this fails because your Request object hasn't built a component stack at the point that it is called. Use the Interp->exec() method instead as described in Using Mason from a Standalone Script
perl -MHTML::Mason::Interp -I./lib \
-e 'HTML::Mason::Interp->new( data_dir => "/home/friedo/cache", comp_root => "/home/friedo/comps" )->exec("/dummy")'