如何通过 ssh 连接运行 PHPUnit Selenium 测试?

发布于 2024-09-28 09:53:19 字数 98 浏览 1 评论 0原文

我正在使用 yii 框架,并尝试在 CentOS 服务器上通过 ssh 连接时运行其单元测试。当我运行 phpunit 时,它尝试启动 Firefox,但失败并显示错误“未指定显示”

I'm using the yii framework and trying to get its unit tests running while connected over ssh on a CentOS server. When I run phpunit, it tries to launch Firefox, which fails with the error "no display specifiied"

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

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

发布评论

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

评论(3

杀手六號 2024-10-05 09:53:19

一般理论

Error: no display specified

要理解该错误消息,您首先必须了解 X Windowing System 的工作原理 - 这是 Linux(和其他类型的 Unix)系统用于显示图形用户界面的框架的名称。

X 由两部分组成——客户端和服务器。客户端是想要绘制界面的程序 - 在你的例子中是 Firefox。服务器是一个使绘图成为可能的程序。有适用于所有主要操作系统的 X 服务器。 Linux 和 OSX 通常附带一个,在 Windows 上您必须找到并安装一个 - Cygwin/X 是一种选择,但还有其他选择。

那么为什么这种客户端/服务器架构是必要的?

大多数时候甚至不需要它。如果您碰巧在本地运行 Linux,那么您甚至不会注意到某处发生了任何类型的客户端/服务器通信 - 但确实存在。

X 的亮点在于,这种架构意味着网络功能就内置在其中。您可以在一台计算机上运行客户端 (Firefox),并在另一台完全不同的计算机上显示 GUI。您可以在 10 台不同的机器上运行 10 个不同的客户端,并让它们在一台机器上全部显示输出,这要归功于 X。想想 VNC 或远程桌面 - X 有点相似,但您可以说它与那些相比更强大。 X 已经拥有这种能力很长时间了。

每当您启动 X 客户端(想要显示图形用户界面的程序)时,它都会寻找 X 服务器。客户端找到的一种可能性是名为 DISPLAY 的环境变量。我在 OSX 上,这就是我所看到的。

[~]> echo $DISPLAY
/tmp/launch-ihNtDq/org.x:0

这指向我的本地 X 服务器。它可以指向我本地网络上的任何服务器。当客户端找到这个环境变量时,它将连接到它并弹出用户界面。

如果客户端找不到这个环境变量 - 你会得到熟悉的

Error: no display specified

返回 Yii

看起来 Yii 有 Selenium< /a> 测试捆绑。 PHPUnit 需要启动 Selenium RC 来控制 Firefox 实例来运行这些测试。 Selenium RC(或者可能是 Firefox 本身)无法找到 DISPLAY 环境变量。并因上述错误而死亡。

如何解决这个问题?

有 3 个选项

  1. 在本地安装 Yii、PHPUnit 及其所有依赖项。 Selenium 在 Windows 上运行得很好。它不会在 Windows 上使用 X 协议,因此与 X 客户端和 X 服务器无关。然后您可以在本地运行 Yii 测试套件。

  2. 在您的 Windows 机器上安装 X 服务器。然后在 ssh 客户端设置中启用“X 转发”(或使用 ssh 的 -X 命令行参数)。当您这样做时,当您登录到该 CentOS 服务器时,将会设置一个 DISPLAY 变量。您可以通过输入上面的 echo 命令来验证它。然后,CentOS 上的 X 客户端可以与 Windows 计算机上的 X 服务器通信(显示 GUI)——所有 X 流量都通过 ssh 连接进行隧道传输。然而,这意味着您需要在 CentOS 服务器上安装 Java(Selenium RC 内置)和 Firefox。您可能有也可能没有它们。

  3. 使用虚拟帧缓冲区 - 例如 Xvfb - 执行所有绘图操作的 X 服务器在内存中,没有在任何地方显示任何输出。

那有什么好处呢? Selenium 具有用于在测试运行期间随时截取屏幕截图并将其保存到文件的命令。例如,典型的 Selenium 测试将检查页面上是否存在某个元素 - 并在不存在时进行屏幕截图。然后,屏幕截图将保存在文件中,您可以稍后查看该文件以确定失败的原因。使用虚拟帧缓冲区制作屏幕截图效果很好。

最终说明

请注意,Selenium 测试只是 PHPUnit 可以运行的测试类型之一。 Selenium 不需要编写 PHPUnit 测试,它是一个可选的附加组件。但 Yii 测试套件显然依赖于它。

最后但并非最不重要

集成测试(Selenium 测试)通常不会在生产系统上运行,因为测试数据有可能会留在生产数据库中。此外,获得良好的测试结果意味着尽可能与外部因素隔离 - 生产数据库的内容将不断变化,这可能会影响您的测试。

通常,在将新代码部署到生产服务器之前,所有测试都将在其他地方执行(您的开发计算机、专用 QA 服务器,无论您拥有什么)。毕竟测试的目的是验证系统在更改后是否正常工作。在生产系统上运行它们没有太大价值 - 代码在部署后不会更改。

当然 - 这取决于您 - 如果您看到在生产系统上进行这些测试的价值,那就继续吧。

General theory

Error: no display specified

To understand that error message you first have to understand how the X Windowing System works - that is the name of the framework used by Linux (and other types of Unix) systems used to display graphical user interfaces.

X consists of two parts - there is a client and a server. Client is the program that wants to draw the interface - in your case that would be Firefox. Server is a program that makes drawing possible. There are X servers available for all the major operating systems. Linuxes and OSX usually ship with one, on Windows you will have to find and install one - Cygwin/X is one option, but there are others.

So why is this client/server architecture even necessary?

Most of the time it's not even needed. If you happen to run Linux locally, then you will not even notice that there is any kind of client/server communication happening somewhere - but there is.

Where X shines though is that this architecture means that network capabilities are built right into it. You can run a client (Firefox) on one machine and display the GUI on a completely different machine. You could run 10 different clients on 10 different machines and have them all display output on a single machine thanks to X. Think VNC or Remote Desktop - X is somewhat similar, but you could say that it's on steroids compared to those. And X has had this ability for a really long time.

Whenever your start up a X client (a program that wants to display graphical user interface) it looks for an X server. One possibility for the client to find one is an environment variable named DISPLAY. I'm on OSX and this is what I see.

[~]> echo $DISPLAY
/tmp/launch-ihNtDq/org.x:0

This point to my local X server. It could point to any server on my local network. When the client finds this environment variable it will connect to it and the user interface pops up.

If client cannot find this environment variable - you will get the familiar

Error: no display specified

Back to Yii

Looks like Yii has Selenium tests bundled. PHPUnit needs to start up Selenium RC to control a Firefox instance to run those tests. Selenium RC (or maybe Firefox itself) fails to find DISPLAY environment variable. And dies with the above error.

How do you solve this issue?

There are 3 options

  1. install Yii, PHPUnit and all their dependencies locally. Selenium runs just fine on Windows. It won't use the X protocol on Windows, so none of that business with X clients and X servers. And you can then run Yii testsuite locally.

  2. install an X server on your Windows box. Then enable 'X Forwarding' in your ssh client settings (or use the -X command line parameter for ssh). When you do that, then there will be a DISPLAY variable set when you are logged in to that CentOS server. You can verify it by typing the echo command above. Then the X client on CentOS can talk (show GUI) to the X server on your Windows machine - all the X traffic is tunnelled over the ssh connection. This however means that you need Java (which Selenium RC is built in) and Firefox on that CentOS server. You may or may not have them there.

  3. use a virtual framebuffer - for example Xvfb - an X server that performs all drawing operations in memory, not showing any output anywhere.

What good is that? Selenium has commands for taking screenshots at any point during the test run and saving them to files. For example a typical Selenium test would check whether an element exist on the page - and to make a screenshot when it does not. Screenshot would then be saved in a file, which you can view later to determine what the reason for the failure was. Making screenshots works just fine with a virtual framebuffer.

Final clarification

Note that Selenium tests are merely one type of tests that PHPUnit can run. Selenium in not required to write PHPUnit tests, it's an optional add-on. But Yii testsuite apparently relies upon it.

Last, but not least

Integration tests (which Selenium tests are) are not usually ran on production systems, because there is a chance that test data is left behind in your production database. Also, getting good test results means isolating from external factors as much as possible - the contents of your production database will be constantly changing and this may affect your tests.

Normally all tests will be executed somewhere else (your development machine, dedicated QA server, whatever you have), before the new code is deployed to production servers. After all the point of tests is to verify that the system works after the changes. There is not much value in running them on production systems - the code does not change after it has been deployed.

Of course - it's up to you - if you see value in doing those tests on production system, go right ahead.

同展鸳鸯锦 2024-10-05 09:53:19

它比你想象的要简单。在桌面上本地运行 Selenium,确保在远程服务器上设置 phpunit。然后在 SSH 连接中启动反向 SSH 隧道。这取决于您的 SSH 客户端。在 PuTTY 中,有一个 SSH 隧道设置,您可以通过选择远程选项来反转方向。请查看此页面了解详细信息。使用命令行中的 OpenSSH,其完成方式如下:

ssh -R 4444:localhost:4444 user@remoteserver

这将在端口 4444 上侦听远程服务器并将其转发到在桌面端口 4444 上的本地主机上运行的 selenium 服务器。

完成此操作后,您需要更改 yourproject/protected/tests/WebTestCase.php 中的 TEST_BASE_URL 设置,以转到 yii 项目的远程服务器 URL。

Its simpler than you think. Run Selenium locally on your desktop, make sure phpunit is setup on the remote server. Then start a reverse SSH tunnel in your SSH connection. This varies depending on your SSH client. In PuTTY, there is a setting for SSH tunnels and you can reverse the direction by selecting the remote option. Check out this page for details. With OpenSSH from the command line, its done like this:

ssh -R 4444:localhost:4444 user@remoteserver

This will listen on the remote server on port 4444 and forward it to your selenium server running on localhost on your desktop port 4444.

Once you've done that, you'll need to change the TEST_BASE_URL setting in yourproject/protected/tests/WebTestCase.php to go to the remote server's URL for your yii project.

淡看悲欢离合 2024-10-05 09:53:19

从 Windows 客户端上的另一台代理计算机运行 GUI 测试的最简单方法是使用“psexec”(http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx)。

The simplest way to run a gui test on from another agent machine on a Windows client is to use "psexec" (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx).

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