Selenium headless does not run on CentOS 7 with Perl, "no display specified" - perl

I try to run headless Selenium on CentOS7:
# cat /etc/os-release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.2 (Maipo)"
I installed Xvfb and run it as
# /usr/bin/Xvfb :99
I installed firefox:
# firefox -v
Mozilla Firefox 38.5.0
and run it to check if it can be run at all:
# export DISPLAY=:99
# firefox
This is the output:
# firefox
Xlib: extension "RANDR" missing on display ":99".
console.error:
[CustomizableUI]
Custom widget with id loop-button does not return a valid node
console.error:
[CustomizableUI]
Custom widget with id loop-button does not return a valid node
GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
Firefox seems to be running after that command:
# ps aux | grep firefox
root 29476 7.3 14.9 852356 152256 pts/3 Sl+ 10:30 0:03 /usr/lib64/firefox/firefox
EDIT
Yes, it's running. Taking screenshot from the Xvfb by
DISPLAY=:99 import -window root -crop 1264x948+0+0 /tmp/screenshot.jpg
I can see
Now the problematic part.
I installed Selenium Remote Driver for perl
# cpanm Selenium::Remote::Driver
Then I ran standalone selenium driver:
# java -jar selenium-server-standalone-2.49.0.jar
Now I run test script:
#!/usr/bin/perl
use strict;
use warnings;
use Selenium::Remote::Driver;
my $driver = Selenium::Remote::Driver->new(browser_name=>'firefox');
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
After 45 second I get error from the driver:
Could not create new session: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: no display specified
at (eval 89) line 510.
Seems like firefox launched by the driver does not see DISPLAY environment variable. I try to add it from the script:
#!/usr/bin/perl
use strict;
use warnings;
use Selenium::Remote::Driver;
$ENV{DISPLAY}=":99";
my $driver = Selenium::Remote::Driver->new(browser_name=>'firefox');
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
It does not help, the previous error remains.
What do I do?
EDIT2
I tried the current setup with Python. All works.
# pip install selenium
And used the following test script:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
f = open('ptn-sel.txt', 'w')
f.write(driver.title)
driver.close()
f.close()
I understand it's problem of Perl driver.... Any suggestions?

Is python using the standalone server or running firefox itself?
If perl is using the server and the server is spawning firefox then you need $DISPLAY set in the server processes environment not the script's environment. (By running export DISPLAY=:99; java -jar selenium-server-standalone-2.49.0.jaror similar.)
If you don't want to use the standalone server at all then Selenium::Firefox looks like it might be interesting.

Related

CISCO IOS-XR, Python3.7, Not able to run commands like 'ls' and 'df' on Cisco router

Cisco ios-xr router using CLI:
RP/0/RP0#show version
Thu Nov 25 07:53:59.103 UTC
Cisco IOS XR Software, Version 6.5.32.11I
Copyright (c) 2013-2020 by Cisco Systems, Inc.
RP/0/RP0#run
Thu Nov 25 07:54:05.231 UTC
[xr-vm_node0_RP0_CPU0:~]$df
Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 3966080 1332040 2412860 36% /
76892 11848320 43% /mnt/ecu/vdd
[xr-vm_node0_RP0_CPU0:~]$
Using python:
I am able to run show commands using Connecthandler .send.command:
from netmiko import ConnectHandler
import subprocess
Network_Device = {"host": "10.111.22.333", "username": "USER123", "password": "Pass123", "device_type": "cisco_xr",}
Connect = ConnectHandler(**Network_Device)
Connect.enable()
version1 = "show version"
print(Connect.send_command(version1))
But not able to run 'df' or 'ls' commands, as not able to reach bash prompt i reach by running 'run' command on router.
I tried:
disk1files = subprocess.run("df", stdout=subprocess.PIPE)
print(disk1files.stdout.decode())
But seems its wrong. Please suggest the right library or code I can use here.
This is my first question here, so bear some silly questions or mistakes done in code
if on DF you are referring to "Don't fragment" then it is posible to send it like
Connect.send_command("ping 192.168.10.10 df-bit size 1600")
where 1600 represents MTU, and for ls commands is link command,
Connect.send_command("ls-active")
Connect.send_command("ls-active-enabled")
but if you are referring to df and ls in linux (disk free and list files..) then you can use os module for sending commands:
import os
os.system("ls -l")
or use call from subprocess module:
from subprocess import call
call(["ls", "-l"])
If you need to acccess cisco bash:
switch# configure terminal
switch(config)# feature bash-shell
switch# run?
run Execute/run program
run-script Run shell scripts
switch# run bash?
bash Linux-bash
switch# run bash
bash-4.2$ whoami
admin
bash-4.2$ pwd
/bootflash/home/admin
bash-4.2$

CasperJS with SlimerJS under linux

I'm trying to execute a CasperJS script using SlimerJS (My tests must check html5 video which is not available using PhantomJS).
Using CasperJS on top of PhantomJS, all of my scripts work just fine, however with CasperJS on top of SlimerJS, I cannot execute any of them. instead, I just always get the following error message:
[alex#webdev-srv test]$ xvfb-run casperjs --engine=slimerjs test2.js
Script Error: Cannot find package.json at /home/alex/headless/phantomjs/test/package.json
Stack:
-> : 0
1474876419994 addons.repository WARN cacheEnabled: Couldn't get pref: extensions.getAddons.cache.enabled
I'm using CentOS 7 as VPS (No GUI), with Firefox being installed, and XVFB too.
How can I make it work? Or is there any solution for this?

Perl script not working through EPIC plug in in Eclipse

I have the below Perl script in my eclipse:
#!/usr/bin/perl
use Selenium::Remote::Driver;
print "Hello, World!\n";
my $name = "king";
print "Hello, $name!\n";
my $driver = new Selenium::Remote::Driver('browser_name' => 'firefox');
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
Output :
Hello, World!
Hello, king!
Selenium server did not return proper status at (eval 91) line 64.
Why I am getting the error message:
"Selenium server did not return proper status at (eval 91) line 64"
The browser is not starting. Kindly help if anyone knows the solution.
Your code looks correct. Add use strict; use warnings; at the top after shebang and make sure the selenium standalone server is running.
So steps would be
Run selenium server: java -jar selenium-server-standalone-2.44.0.jar
Observe below output
➤ java -jar selenium-server-standalone-2.44.0.jar
15:18:56.677 INFO - Launching a standalone server
15:18:56.900 INFO - Java: Oracle Corporation 25.40-b25
15:18:56.900 INFO - OS: Windows 7 6.1 x86
15:18:56.914 INFO - v2.44.0, with Core v2.44.0. Built from revision 76d78cf
15:18:57.174 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
15:18:57.175 INFO - Version Jetty/5.1.x
15:18:57.176 INFO - Started HttpContext[/selenium-server,/selenium-server]
15:18:57.325 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler#af7cc2
15:18:57.325 INFO - Started HttpContext[/wd,/wd]
15:18:57.325 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
15:18:57.325 INFO - Started HttpContext[/,/]
15:18:57.329 INFO - Started SocketListener on 0.0.0.0:4444
15:18:57.329 INFO - Started org.openqa.jetty.jetty.Server#133314b
Run your script.
I tested this on selenium-server-standalone-2.44.0 and firefox 33.0. It worked fine.
Update: According to the bug mentioned here you must be using Selenium 2.42 or greater.

trouble installing MozRepl

I've been trying to install WWW::Mechanize::Firefox through CPAN and I am having trouble installing the dependency MozRepl. The installation goes through but the tests all fail, and when I force install it and run my perl script, I run into an error
Failed to connect to , at /Library/Perl/5.12/MozRepl/RemoteObject.pm line 467.
SO I uninstalled MozRepl and looked at the tests I get the following errors in the log:
# Failed test at t/10-plugin-repl-enter.t line 11.
Can't locate object method "repl_enter" via package "MozRepl" at t/10-plugin-repl-enter.t line 12.
...
# Failed test at t/20-plugin-json.t line 16.
Can't locate object method "json" via package "MozRepl" at t/20-plugin-json.t line 17.
# Failed test at t/19-plugin-repl-util-doc_for.t line 14.
Can't locate object method "repl_doc_for" via package "MozRepl" at t/19-plugin-repl-util-doc_for.t line 16.
# Failed test at t/18-plugin-repl-util-help_url_for.t line 14.
Can't locate object method "repl_help_url" via package "MozRepl" at t/18-plugin-repl-util-help_url_for.t line 16.
etc..
I am running on Mac OSX 10.8.4, 4 GB Ram 2.5 Ghz, Perl version 5.12. Does anybody have any idea what is causing these errors?
UPDATE:
i reinstalled mozrepl, and now I get this error when i run my script:
Failed to connect to , problem connecting to "localhost", port 4242: Connection refused at /Users/thui/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0/MozRepl/Client.pm line 144
This works for me with perl 5.10 or later, latest Firefox (26 as of writing) and Mozrepl from github.
At command propmpt:
(1) Download MozRepl and build the XPI file (Firefox extension):
git clone git://github.com/bard/mozrepl
cd mozrepl
zip -r ../mozrepl.zip *
cd ..
mv mozrepl.zip mozrepl.xpi
(2) Install the extension in Firefox via about:addons [Install from file].
In Firefox:
(3) Menu->Tools->Mozrepl->Activate On Startup
(4) Menu->Tools->Mozrepl->Start
At command propmpt:
(5) which firefox
Make sure the firefox executable (or your OS's wrapper script) is in $PATH - you should get some output!
(6) cpanm WWW::Mechanize::Firefox
(7) Test it!
At this point, if CPANminus reports no errors then WWW::Mechanize::Firefox should be working. The first example from the synopsis is a good test:
#!/usr/bin/perl
use WWW::Mechanize::Firefox;
my $mech = WWW::Mechanize::Firefox->new();
$mech->get('http://google.com');
That assumes MozRepl is listening on port 4242 (check in Menu->Tools->Mozrepl->Change Port). You can also change the port from the perl side; see options for ->new().
(8) cpanm HTML::Display::Common
I found that bcat.pl from the examples required this module, but it wasn't installed as a dependency.

Start the session bus of DBus with Perl Net::DBus

I am using Perl and the Net::DBus module. I wrote a simple test program:
#!/usr/bin/perl
use strict;
use warnings;
package MyObj;
use Net::DBus::Exporter qw(org.example.Tao);
use base qw(Net::DBus::Object);
sub new {
my $class = shift;
my $service = shift;
my $self = $class->SUPER::new($service, '/MyObj');
bless $self, $class;
return $self;
}
dbus_method("Hello", ["string"]);
sub Hello {
return 'Hello';
}
package main;
use Net::DBus;
use Net::DBus::Reactor;
my $bus = Net::DBus->session;
my $service = $bus->export_service("org.example.Tao");
my $object = MyObj->new($service);
my $reactor = Net::DBus::Reactor->main();
$reactor->run();
return 0;
I am connecting by ssh and using:
Perl, v5.8.8 built for x86_64-linux-thread-multi
Linux example.com 2.6.32.19-0.2.99.17.22250fd-xen #1 SMP 2010-09-13 10:16:50 +0200 x86_64 x86_64 x86_64 GNU/Linux
CentOS release 5.4 (Final)
When I try to start my test.pl, I get the error:
org.freedesktop.DBus.Error.Spawn.ExecFailed:
Failed to execute dbus-launch to autolaunch D-Bus session
This error is raised by this line:
my $bus = Net::DBus->session;
Google hinted to me about dbus-launch. I executed yum install dbus-x11.
I try start my test code again and get error in the same line:
org.freedesktop.DBus.Error.Spawn.ExecFailed:
dbus-launch failed to autolaunch D-Bus session:
Autolaunch error: X11 initialization failed.
After read manuals, I detect that DBUS session daemon isn't started and my ENV var DBUS_SESSION_BUS_ADDRESS is empty:
[root#zion perl]# ps ax|grep dbus|grep -v grep
1019 ? Ss 0:00 dbus-daemon --system
Then I exec:
[root#zion perl]# dbus-launch --sh-syntax
DBUS_SESSION_BUS_ADDRESS='unix:abstract=/tmp/dbus-smHadq6yxV,guid=101ccd74fb75ae501485ed004e2a9043';
export DBUS_SESSION_BUS_ADDRESS;
DBUS_SESSION_BUS_PID=5037;
[root#zion perl]# ps ax|grep dbus|grep -v grep
1019 ? Ss 0:00 dbus-daemon --system
5037 ? Ss 0:00 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
But DBUS_SESSION_BUS_ADDRESS is same empty.
Question:
I need simple two Perl apps. The first app registers the dbus session service. Another app using my registered service. What is the best and correct way to do it in my environment?
First of all, you need to eval dbus-launch output. Like this:
$ env | grep DBUS
(empty output; no DBUS session bus launched yet)
$ eval `dbus-launch --sh-syntax`
(empty output; DBUS session bus started, output is evaluated to set shell vars)
$ env | grep DBUS
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ZkMjn6B47b,guid=85b2da814a8a061d4e7a55004e35b499
Second, you should consider how are you going to use your apps. Try to answer yourself these questions: why are you trying to bind to session bus (which is by definition a bus, associated with an interactive user session)? If this is a system-wide service, it should bind to system bus. If it is a user service, user session manager should take care of starting dbus session bus.