Running Selenium::WebDriver::Firefox inside Xvfb f

2019-02-13 15:32发布

问题:

I'm trying to do some headless testing using a ruby script. Essentially I'm executing Xvfb on display :1, and then firing up Watir::Browser.new(:firefox) using watir-webdriver.

This works great if you run the script as root - I can run x11vnc and watch the script execute the browser and interact with it.

The problem is, I need to be able to call this ruby script from a Rails app, and NOT run it as root... if I try to run the script from the command line as a regular user, Xvfb fires up on :1 as usual, but Watir won't start a browser... it eventually times out after 60 seconds. Connecting via VNC reveals a black screen with mouse cursor.

I can do the whole lot from the command line - start Xvfb, then start firefox --display=:1 and the browser starts up, so it must be a Selenium issue, right?

Nutshell: Selenium/Webdriver refuses to start a browser inside Xvfb unless I run the script as root.

回答1:

  • SOLVED - EDIT * - sorry I couldn't ANSWER the question, but apparently my account is too new to do that just yet.

Solved! Amazing how things like this tend to solve themselves AFTER you've asked the question...

For those wanting to know why, there's a few things I needed to do...

1) Make sure the user in question has a working home directory (www-data didn't by default... its home dir was owned by root)

2) Start Xvfb from command line, and then start firefox from the command line (NOT from within the script) - this will populate the users home folder with the usual .dbus, .gconf etc folders

3) If you get a massive delay and then an error message along the lines of "an error occurred while loading or saving configuration information for firefox-bin", delete the .dbus folder in the users home directory

4) Try #2 again - if you had a config error before, it should now be gone.

5) Try running the script again.

This worked like a charm for me, so if you're having similar issues running Xvfb and watir-webdriver from within a web app, give it a try.

Thanks to all for contributing to what is hands-down the BEST Q&A site on the web... this place has saved my butt more times than I care to mention.

Note: the user originally entered this amended to their question, because they could not figure out how to post it as an answer. That was later edited out of the question by some well meaning person but never added as an answer.



回答2:

Hi don't know if this link could be a help for you. This helped me a lot. I needed to run watir-webdriver automatically at startup/reboot - it worked for me (read the comments as well).



回答3:

You should also take a look at Ruby wrapper for Xvfb http://rubygems.org/gems/headless