以非 root 用户身份从 ruby​​ 在 Xvfb 内运行 Selenium::WebDriver::Firefox

发布于 2024-11-03 09:58:13 字数 508 浏览 1 评论 0原文

我正在尝试使用 ruby​​ 脚本进行一些无头测试。本质上,我在显示器 :1 上执行 Xvfb,然后使用 watir-webdriver 启动 Watir::Browser.new(:firefox)

如果您以 root 身份运行脚本,这会非常有效 - 我可以运行 x11vnc 并观察脚本执行浏览器并与其交互。

问题是,我需要能够从 Rails 应用程序调用这个 ruby​​ 脚本,而不是以 root 身份运行它...如果我尝试以普通用户身份从命令行运行该脚本,Xvfb 会启动:1像往常一样,但 Watir 不会启动浏览器...它最终会在 60 秒后超时。通过 VNC 连接会显示带有鼠标光标的黑屏。

我可以从命令行执行所有操作 - 启动 Xvfb,然后启动 firefox --display=:1 并且浏览器启动,所以这一定是 Selenium 问题,对吧?

简而言之:Selenium/Webdriver 拒绝在 Xvfb 内启动浏览器,除非我以 root 身份运行脚本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一百个冬季 2024-11-10 09:58:13
  • 已解决 - 编辑 * - 抱歉,我无法回答这个问题,但显然我的帐户还太新,无法做到这一点。

解决了!令人惊讶的是,在你提出问题后,这样的事情往往会自行解决...

对于那些想知道原因的人,我需要做一些事情...

1)确保相关用户有一个工作主目录(www-data 默认情况下没有...它的主目录由 root 拥有)

2)从命令行启动 Xvfb,然后从命令行启动 firefox(不是从脚本内) - 这将填充用户主目录文件夹与通常的 .dbus、.gconf 等文件夹

3) 如果出现严重延迟,然后出现错误消息“加载或保存 firefox-bin 的配置信息时发生错误”,请删除 .dbus 文件夹用户主目录

4) 再次尝试 #2 - 如果您之前遇到过配置错误,现在它应该消失了。

5) 尝试再次运行该脚本。

这对我来说就像一个魅力,所以如果您在 Web 应用程序中运行 Xvfb 和 watir-webdriver 时遇到类似问题,请尝试一下。

感谢所有人为这个毫无疑问是网络上最好的问答网站做出的贡献……这个地方拯救了我的屁股的次数比我愿意提及的还要多。

注意:用户最初输入此修改是为了解决他们的问题,因为他们不知道如何将其发布为答案。后来这个问题被一些好心的人编辑掉了,但从未添加为答案。

  • 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.

流年已逝 2024-11-10 09:58:13

您好,不知道这个链接是否有帮助为你。这对我帮助很大。我需要在启动/重新启动时自动运行 watir-webdriver - 它对我有用(也请阅读评论)。

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).

栀梦 2024-11-10 09:58:13

您还应该看看 Xvfb 的 Ruby 包装器 http://rubygems.org/gems/headless

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文