On a modern setup (Windows 7 64-bit, IE 11, ActiveState Perl 5.16 64-bit), the Click method in Win32::IEAutomation (v0.5) does not seem to work. Here is an example, lightly adapted from the documentation:
use Win32::IEAutomation;
my $ie = Win32::IEAutomation->new( visible => 1);
$ie->gotoURL('http://www.google.com');
$ie->getLink('linktext:', "About")->Click;
At this point, I should see the "About" page in IE. But I still see Google's home page in IE, and I cannot use the Content method in Win32::IEAutomation to get the source of the "About" page.
I have the same problem on an older setup (Vista SP2 64-bit, IE 9, ActiveState Perl 5.10.1). But the problem does not arise when I use a similar setup with IE8 instead of IE9. The problem thus seems to lie with a difference between IE8 and subsequent IE versions.
Is there anything that I can do to get the example script working with more recent versions of IE?
Win32::IEAutomation is a thin wrapper around the various interfaces exposed by InternetExplorer.Application and MSHTML.
Therefore, I tried to replicate the problem by writing a script to do the navigation without using Win32::IEAutomation. Using the click method on a link did not initiate navigation whereas passing its href to Navigate2 did.
The click method "Simulates a click by causing the HTMLFrameSiteEvents::onclick event to fire," meaning any onClick handlers defined on the page will be involved. I am not sure why specifically navigation is not being initiated.
However, the problem is not specific to Google's home page: I tried it with example.com, and invoking the click method on a link on that page did not initiate navigation either.
Here is the script I used as a testbed:
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use Win32::OLE qw(EVENTS in valof);
$Win32::OLE::Warn = 3;
my $url = 'https://www.google.com/';
my %event_handler = (
DocumentComplete => \&onDocumentComplete,
);
my %page_handler = (
'https://www.google.com/'
=> \&onPageGoogleHome,
'https://www.google.com/intl/en/about/'
=> \&onPageGoogleAbout,
);
my $ie = Win32::OLE->new(
"InternetExplorer.Application", sub { $_[0]->Quit }
);
Win32::OLE->WithEvents($ie, \&Event, 'DWebBrowserEvents2');
$ie->{Visible} = 1;
$ie->Navigate2($url);
Win32::OLE->MessageLoop;
Win32::OLE->SpinMessageLoop;
$ie->Quit;
sub Event {
my ($ie, $event, #argv) = #_;
if (exists $event_handler{$event}) {
$event_handler{$event}->($ie, \#argv);
}
else {
# unhandled event
}
return;
}
sub onDocumentComplete {
my ($ie, $argv) = #_;
my $url = valof($argv->[-1]);
if (exists $page_handler{$url}) {
$page_handler{$url}->($ie, $argv);
}
else {
# unhandled page
}
return;
}
sub onPageGoogleHome {
my ($ie, $argv) = #_;
say "We are on Google's home page";
my $links = $ie->Document->links;
my $about_link;
for my $link (in $links) {
if ($link->innerText eq 'About') {
say "Found 'About' link";
$about_link = $link;
last;
}
}
if ($about_link) {
# Doesn't work:
# $about_link->click;
$ie->Navigate2($about_link->href);
}
return;
}
sub onPageGoogleAbout {
my ($ie, $argv) = #_;
say "Yay, we are on the about page!";
Win32::OLE->QuitMessageLoop;
return;
}
Version information:
This is perl 5, version 19, subversion 12 (v5.19.12) built for MSWin32-x64-multi-thread
Internet Explorer 11
Windows 8.1 Pro 64-bit
I observe the same faulty behavior with the ->Click() in Strawberry Perl v5.18.2 and Win32::IEAutomation v0.5 and IE v11.0.9600.17105.
My work around is to use the gotoURL() method directly. This obviously would not work with javascript actions, but does for this particular example.
use strict;
use warnings;
use Win32::IEAutomation;
my $ie = Win32::IEAutomation->new( visible => 1);
$ie->gotoURL('http://www.google.com');
my $about = $ie->getLink('linktext:' => 'About')
or die "Unable to find About";
# $about->Click(); # <--- does not work, using alternative method
$ie->gotoURL($about->linkUrl());
Related
Has anyone successfully uploaded media to Twitter, ie posted a tweet with an image using Perl?
I would like to upload images from my blog without doing it all manually.
update_with_media(status, media[]) is deprecated, and crashes. Twitter says to use plain update(), passing a media id. However it is first necessary to upload the media to get the id, and I can find no code examples.
Any experience in this area?
Cheers,
Peter
I thought I'd add an update here even though the thread is quite old. I too was looking for an answer to how to use Perl to upload media to twitter.
Net::Twitter is perfectly capable of sending PNG images up to Twitter, though the documentation is poor. The OP is correct that update_with_media is deprecated and crashed for us.
You do need to use the $nt->upload AND $nt->update methods combined. You upload the RAW PNG file encoded with base64, I could not get the RAW PNG file upload to work but no issues when base64 encoded. The upload method returns, on success, a JSON structure which has the media_ids in. These id's are then passed with the $nt->update method. Here's some actual code
use Net::Twitter;
use File::Slurp;
use MIME::Base64;
use Data::Dumper;
my $nt = Net::Twitter->new(
ssl => 1,
traits => [qw/API::RESTv1_1/],
consumer_key => $config->{twitter}{api_key},
consumer_secret => $config->{twitter}{api_secret},
access_token => $config->{twitter}{access_token},
access_token_secret => $config->{twitter}{access_token_secret},
);
my $filename = "<somelink to a PNG file>";
my $file_contents = read_file ($filename , binmode => ':raw');
my $status = $nt->upload(encode_base64($file_contents));
print "SendTweet: status = ".Dumper($status);
my $status2;
eval {
$status2 = $nt->update({status => $s , media_ids => $status->{media_id} });
print "status2 = ".Dumper($status2);
};
if ($#)
{
print "Error: $#\n";
}
The code is pulled directly from our working test code so should work. This code is purely proof of concept so you will need to add in all your twitter authentication etc.
We have only done PNG files but I see no reason why video etc shouldn't work fine as we simply followed the Twitter docs.
Rob
In the end I used readpipe with twurl. Had I known about twurl in the first place I likely would not have bothered with Net::Twitter! twurl works great, and returns a full json string to tell you what went wrong, if anything.
Pre-requisite: you need to get the Oauth keys for your twitter account. (See here - https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens.html). There are some step by step online exmaples elsewhere also that can help.
Here's the code I ended up with.
First call the tweet module (and these are not valid keys by the way - just insert yours)
use Jimtweet;
my $tweet=Jimtweet->new();
$tweet->consumer_key('KvfevhjwkKJvinvalidkeycvhejwkKJVCwe');
$tweet->consumer_secret('KvfevhjwkKJvcvnvalidkeyhejwkKJVCwe');
$tweet->oauth_token('KvfevhjwkKJvcvhenvalidkeyjwkKJVCwKvfevhjwkKJvcvhejwkKJVCweVU');
$tweet->oauth_token_secret('KvfevhjwkKJvcvhejwnvalidkeykKJVCwe');
my $res = $tweet->update_status($message, $ENV{DOCUMENT_ROOT}.$li);
Jimtweet is a free module I found online (I forget where), but I had to modify it to do the image upload. It follows below.
$message is the text status message to send
$li contains the local path to the file I want to upload. This is a file local on the server. $ENV{DOCUMENT_ROOT} contains the server path to the public HTML files on my website.
$res contains a JSON reply from twitter you can look at if you need to.
If you want to use this, cut & paste everything from 'package Jimtweet;' and on into a file called Jimtweet.pm which the above code should use. If your perl installation can't find the module try adding the line use lib "/home/your/path/to/jimtweet/directory;" before the use Jimtweet; line.
Twitter requires an image to be uploaded, it then returns a media_id, then you include the media_id in a regular message you want to post. See Jimtweet package below:
package Jimtweet;
#####JimTweet 0.1 By James Januszka 2010
#####Email:jimjanuszka#gmail.com
#####Twitter:#jamesjanuszka
#####Modifications by John Bell to include image upload. Twitter: #BellUkcbajr
use strict;
use warnings;
use LWP;
use HTTP::Headers;
use URI::Encode qw(uri_encode);
use URI::Escape qw(uri_escape);
use Digest::HMAC_SHA1;
####Constructor####
sub new {
my $self={};
$self->{OAUTH_VERSION}=uri_escape("1.0");
$self->{OAUTH_SIGNATURE_METHOD}=uri_escape("HMAC-SHA1");
$self->{OAUTH_TIMESTAMP}=undef;
$self->{OAUTH_NONCE}=undef;
$self->{AGENT}="jimtweet/0.1";
#####################
#Use this for status updates
$self->{URLx}="https://api.twitter.com/1.1/statuses/update.json";
#####################
#Use this for image upload
$self->{URL}="https://upload.twitter.com/1.1/media/upload.json";
$self->{BROWSER}=LWP::UserAgent->new(agent =>$self->{AGENT});
$self->{CONSUMER_KEY}=undef;
$self->{CONSUMER_SECRET}=undef;
$self->{OAUTH_TOKEN}=undef;
$self->{OAUTH_TOKEN_SECRET}=undef;
$self->{STATUS}=undef;
$self->{MEDIAurl}=undef;
bless($self);
return $self;
}
sub consumer_key{
my $self=shift;
if (#_) { $self->{CONSUMER_KEY}=uri_escape(shift) }
return $self->{CONSUMER_KEY};
}
sub consumer_secret{
my $self = shift;
if (#_) { $self->{CONSUMER_SECRET}=uri_escape(shift) }
return $self->{CONSUMER_SECRET};
}
sub oauth_token{
my $self = shift;
if (#_) { $self->{OAUTH_TOKEN}=uri_escape(shift) }
return $self->{OAUTH_TOKEN};
}
sub oauth_token_secret{
my $self = shift;
if (#_) { $self->{OAUTH_TOKEN_SECRET}=uri_escape(shift) }
return $self->{OAUTH_TOKEN_SECRET};
}
sub update_status(#){
sleep(2);
my $self = shift;
if (#_) { $self->{STATUS}=uri_escape(shift) }
if (#_) { $self->{MEDIAurl}=shift }
#Got parameters. Now create the POST to upload an image
my $seconds = time();
$self->{OAUTH_TIMESTAMP}=uri_escape($seconds);
$self->{OAUTH_NONCE}=$self->{OAUTH_TIMESTAMP};
#####################
#Use this for uploads. Parameters have to be in alphabetical order!
my $query=qq(oauth_consumer_key=$self->{CONSUMER_KEY}&oauth_nonce=$self->{OAUTH_NONCE}&oauth_signature_method=$self->{OAUTH_SIGNATURE_METHOD}&oauth_timestamp=$self->{OAUTH_TIMESTAMP}&oauth_token=$self->{OAUTH_TOKEN}&oauth_version=$self->{OAUTH_VERSION});
my $sig="POST&";
$sig .=uri_encode($self->{URL},1);
$sig .="&";
$sig .=uri_encode($query,1);
my $sig_key=$self->{CONSUMER_SECRET};
$sig_key .="&";
$sig_key .=$self->{OAUTH_TOKEN_SECRET};
my $hmac = Digest::HMAC_SHA1->new($sig_key);
$hmac->add($sig);
my $oauth_signature_base64=$hmac->b64digest;
$oauth_signature_base64 .="=";
my $utf8_oauth_signature_base64=uri_escape($oauth_signature_base64);
my $header=qq(OAuth oauth_nonce="$self->{OAUTH_NONCE}", oauth_signature_method="$self->{OAUTH_SIGNATURE_METHOD}", oauth_timestamp="$self->{OAUTH_TIMESTAMP}", oauth_consumer_key="$self->{CONSUMER_KEY}", oauth_token="$self->{OAUTH_TOKEN}", oauth_signature="$utf8_oauth_signature_base64", oauth_version="$self->{OAUTH_VERSION}");
my $res = $self->{BROWSER}->post(
$self->{URL},
'Authorization' => $header,
'content-type' => 'form-data',
'Content' => [ media => ["$self->{MEDIAurl}"] ]
);
use JSON;
my $response = decode_json ($res->content);
my $media_id = $response->{'media_id'};
$seconds = time();
$self->{OAUTH_TIMESTAMP}=uri_escape($seconds);
$self->{OAUTH_NONCE}=$self->{OAUTH_TIMESTAMP}
my $queryx=qq(media_ids=$media_id&oauth_consumer_key=$self->{CONSUMER_KEY}&oauth_nonce=$self->{OAUTH_NONCE}&oauth_signature_method=$self->{OAUTH_SIGNATURE_METHOD}&oauth_timestamp=$self->{OAUTH_TIMESTAMP}&oauth_token=$self->{OAUTH_TOKEN}&oauth_version=$self->{OAUTH_VERSION}&status=$self->{STATUS});
my $sigx="POST&";
$sigx .=uri_encode($self->{URLx},1);
$sigx .="&";
$sigx .=uri_encode($queryx,1);
my $hmacx = Digest::HMAC_SHA1->new($sig_key);
$hmacx->add($sigx);
my $oauth_signature_base64x=$hmacx->b64digest;
$oauth_signature_base64x .="=";
my $utf8_oauth_signature_base64x=uri_escape($oauth_signature_base64x);
my $headerx=qq(OAuth oauth_nonce="$self->{OAUTH_NONCE}", oauth_signature_method="$self->{OAUTH_SIGNATURE_METHOD}", oauth_timestamp="$self->{OAUTH_TIMESTAMP}", oauth_consumer_key="$self->{CONSUMER_KEY}", oauth_token="$self->{OAUTH_TOKEN}", oauth_signature="$utf8_oauth_signature_base64x", oauth_version="$self->{OAUTH_VERSION}");
#And done generating content. Now to POST to twitter.
$res = $self->{BROWSER}->post(
$self->{URLx},
'Authorization' => $headerx,
'content-type' => 'application/x-www-form-urlencoded',
'Content' => qq(media_ids=$media_id&status=$self->{STATUS})
);
return $res;
}
####Footer####
1; #so the require or use succeeds
I have small app based on mojolicious. And I have index.html in public dir. I want to have route to this file when user asks for '/'.
I wrote two solution, but I don't like them.
First solution - add simple controller.
sub stratup {
//...
$r->get('/')->to('general#index_html');
//...
}
package MyPackage::General;
use Mojo::Base 'Mojolicious::Controller';
use strict;
use warnings;
sub index_html {
my $self = shift;
$self->render_static('index.html');
return;
}
1;
Second solution - add hook
sub startup {
my $self = shift;
$self->hook(before_dispatch => sub {
my $self = shift;
if ($self->req->url eq '/') {
$self->req->url( Mojo::URL->new('/index.html') );
}
});
What I want:
$r->get('/')->to('/index.html');
or something like that.
P.S. I know, than usualy nginx/apache do it, but I use morbo to run code.
You want:
$r->get('...')->to(cb => sub {
my $c = shift;
$c->reply->static('index.html')
});
(As long as you're after Mojolicous 5.45 2014-09-26)
By far the simplest way is
get "/" => "index";
I'll dig this up from the graveyard, why not.
I found myself similarly trying to serve a static html file in a docker container that I had using to serve both a Mojolicious REST API and a Vue.js front end. After searching around and piecing sporadic information together, this is what seems to work for me.
** disclaimer: I have not fully tested this with Vue routing and other aspects as yet.
My directory structure:
/app
/app/script
/app/modules/ui
/app/modules/ui/dist
From the command line the app directory, using morbo to test:
morbo script/ui.pl
ui.pl script
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use Mojo::File qw(curfile);
use v5.25;
my $app = app;
my $static = $app->static;
push #{$static->paths}, curfile->dirname->sibling('modules/ui/dist')->to_string;
any '/' => sub {
my $c = shift;
my $content = $static->file("/index.html")->slurp;
$c->render(text => $content);
};
$app->start;
Using a combo of information from https://metacpan.org/pod/Mojolicious::Static and basic routing information at https://docs.mojolicious.org/Mojolicious/Lite, I could get the vue.js index page to render as expected.
** UPDATED A DAY LATER **
As it turns out, there is an easier way, though not clearly documented. If you place the static files inside your public folder, you can use the default helpers included with Mojolicious to render the files. The documentation refers to it here, https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Serving-static-files, but it's not very clear on how to make it happen.
I tooled around some, but it took browsing the code of Controller.pm of for Mojolicious to sort it out. This section of the POD led me to determine how to get the reply object:
=head2 helpers
my $helpers = $c->helpers;
Return a proxy object containing the current controller object and on which helpers provided by /app can be called. This includes all helpers from Mojolicious::Plugin::DefaultHelpers and Mojolicious::Plugin::TagHelpers.
# Make sure to use the "title" helper and not the controller method
$c->helpers->title('Welcome!');
# Use a nested helper instead of the "reply" controller method
$c->helpers->reply->not_found;
Based on this, I can drop my files into the public folder:
/app/public/index.html
Then modify my controller to match:
# https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Serving-static-files
any '/' => sub {
my $c = shift;
$c->helpers->reply->static('index.html');
};
I have an app that may choose to serve a file from disk - or go to Catalyst and generate a dynamic one.
Something like this (inside call()):
if (-f $path){
my $app = Plack::App::File->new(file => $path)->to_app; #serve published page
$res = $app->($env);
}else{
log_debug "Fall through to app ";
$res = $self->app->($env);.
}
I'd like to set some cookies when it gets back. So I use Plack::Util
Plack::Util::response_cb($res, sub {
my $res = shift;
log_debug "Handling app response";
...
});
The results? In the first case (Plack::App::File), everything works as expected. In the second (continue to the app the normal way) it never comes back in.
I wonder why this is happenning? Here is my psgi initialization:
my $app = MainApp->psgi_app(#_);
$app = Plack::MyAppAbove->wrap($app);
$app = MainApp->apply_default_middlewares($app);
I need some help getting a bugzilla extension off the ground.
I want to hook into bug_format_comment (FWIW: to change some plain text automatically added as a comment when I commit to SVN to links to the respective SCM commit.)
Right now, nothing seems to happen when I manually add a comment to a bug.
Is there anything special I need to do to make the extension run, besides putting it in the /extensions/my-ext-name/ dir ?
How can I test if the extension is called at all?
I use an old version of Bugzilla (3.2.x). Is that hook even supported? (I can't find that info in the documentation).
Here's my complete Extension.pm file (I have no experience in Perl. I took the example of the hook from the example extension and ran from there)
package Bugzilla::Extension::Websvn-scmbug-autolink;
use strict;
use base qw(Bugzilla::Extension);
# This code for this is in ./extensions/Websvn-scmbug-autolink/lib/Util.pm
use Bugzilla::Extension::Websvn-scmbug-autolink::Util;
use URI::Escape;
our $VERSION = '0.01';
# See the documentation of Bugzilla::Hook ("perldoc Bugzilla::Hook"
# in the bugzilla directory) for a list of all available hooks.
sub install_update_db {
my ($self, $args) = #_;
}
sub bug_format_comment {
my ($self, $args) = #_;
my $regexes = $args->{'regexes'};
# push(#$regexes, { match => qr/\bfoo\b/, replace => 'bar' });
# 6665 --> 6666
# CTUFramework:trunk/CTUCsharpRuntime/CtuFramework/text1-renamed.txt
#my $bar_match = qr/\b(bar)\b/;
my $bar_match = qr/(?:^|\r|\n)(\d+|NONE) (-->) (\d+|NONE)[ \r\n\t]+([^:]+):(.*?)[\r\n]/s; #/s - treat as single line
push(#$regexes, { match => $bar_match, replace => \&_replace_bar });
my $scm_match2 = qr/(?:^|\r|\n)(\d+|NONE) (-->) (\d+|NONE)[ \r\n\t]+([^:]+):(.*?)[\r\n]/s; #/s - treat as single line
push(#$regexes, { match => $scm_match2, replace => \&_replace_bar });
}
# Used by bug_format_comment--see its code for an explanation.
sub _replace_bar {
my $args = shift;
my $scmFromVer = $args->{matches}->[0];
my $scmToVer = $args->{matches}->[1];
my $scmArrow = $args->{matches}->[2];
my $scmProject = $args->{matches}->[3];
my $scmFile = $args->{matches}->[4];
# Remember, you have to HTML-escape any data that you are returning!
my $websvnRoot = "http://devlinux/websvn";
my $websvnRepo = uri_escape($scmProject); #maybe do a mapping
my $websvnFilePath = uri_escape("/".$scmFile);
my $fromRevUrl = sprintf("%s/revision.php?repname=%s&rev=%s",
$websvnRoot, $websvnRepo, $scmFromVer);
my $toRevUrl = sprintf("%s/revision.php?repname=%s&rev=%s",
$websvnRoot, $websvnRepo, $scmToVer);
my $diffUrl = sprintf("%s/diff.php?repname=%s&path=%s&rev=%s",
$websvnRoot, $websvnRepo, $websvnFilePath, $scmToVer);
my $fileUrl = sprintf("%s/filedetails.php?repname=%s&path=%s&rev=%s",
$websvnRoot, $websvnRepo, $websvnFilePath, $scmToVer);
# TODO no link for 'NONE'
my $fromRevLink = sprintf(qq{%s}, $fromRevUrl, $scmFromVer);
my $toRevLink = sprintf(qq{%s}, $toRevUrl, $scmToVer);
my $diffLink = sprintf(qq{%s}, $diffUrl, $scmArrow);
my $fileLink = sprintf(qq{%s}, $fileUrl, $scmFilePath);
# $match = html_quote($match);
return "$fromRevLink $diffLink $toRevLink:$fileLink";
};
__PACKAGE__->NAME;
As written, your extension won't even load: dashes are not valid in Perl package names.
Change the name from Websvn-scmbug-autolink to Websvn_scmbug_autolink.
I searched the Bugzilla sources, and found that the hook is simply not supported in version 3.2.x. The hook was introduced in Bugzilla 3.6: http://bzr.mozilla.org/bugzilla/3.6/revision/6762
PS. I hacked the regex replaces right in the template script for comments. Hacky, but it works.
Selenium 2.* is running on Linux with Firefox as the browser.
I am using perl with the Selenium::Remote::Driver module to interact with the server.
Is there anything available to check if an alert is present? The perl module provides several functions to click OK on an alert, or to get the text from it, but this will raise an error if there is no alert - how to avoid an error and still get rid of any alert?
Basically, I want to remove all alerts when a page finished loading (if any), but not sure how?
Another option that I tried is disabling all alerts by setting a variable in the firefox profile (which works when you use the browser yourself), but somehow the alert is still there when the browser is used by Selenium because i think Selenium handles the alert itself because of the "handlesAlerts" capability, which is always set to true, and I'm not sure how to disable it. It may be the solution if it's not possible to check for the existence of an alert.
Anyone have an idea?
You could try to dismiss alerts, use eval block to handle exceptions
eval {
$driver->accept_alert;
};
if ($#){
warn "Maybe no alert?":
warn $#;
}
I created a couple of functions that check for an alert and then either cancel or interact with it as needed.
use Try::Tiny qw( try catch );
# checks if there is a javascript alert/confirm/input on the screen
sub alert_is_present
{
my $d = shift;
my $alertPresent = 0;
try{
my $alertTxt = $d->get_alert_text();
logIt( "alert open: $alertTxt", 'DEBUG' ) if $alertTxt;
$alertPresent++;
}catch{
my $err = $_;
if( $err =~ 'modal dialog when one was not open' ){
logIt( 'no alert open', 'DEBUG2' );
}else{
logIt( "ERROR: getting alert_text: $_", 'ERROR' );
}
};
return $alertPresent;
}
# Assumes caller confirmed an alert is present!! Either cancels the alert or
types any passed in data and accepts it.
sub handle_alert
{
my ( $d, $action, $data ) = #_;
logIt( "handle_alert called with: $action, $data", 'DEBUG' );
if( $action eq 'CANCEL' ){
$d->dismiss_alert();
}else{
$d->send_keys_to_alert( $data )
if $data;
$d->accept_alert();
}
$d->pause( 500 );
}