JUnit - 测试网站
我对 JUnit 很陌生,但我想设置一些执行以下操作的测试。
测试一系列服务器到服务器 API 调用 - 验证响应是否正确 - 我可以做到这一点。
打开一个网页,在其中输入数据并验证提交时发生的情况 - 这是我正在努力解决的问题。这可能吗?
我想我可以使用服务器端 http Web 请求来调用网页,但我不确定如何与网站本身交互,即在表单中输入数据。
有什么想法吗?
谢谢
史蒂夫
I'm very new to JUnit, but I want to set up some tests which does the following..
Tests a range of server to server API calls - verifying the responses are correct - I can do that fine.
Open a web page, enter data onto it and verify what happens on submit - This I am struggling with. Is it even possible?
I am thinking that I could call a web page using a server side http web request, but I'm not sure how I can interact with the site itself, i.e. enter data into the forms.
Any thoughts?
Thanks
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Selenium 来实现此目的。我建议您使用当前正在开发的版本 2,应该很快就会推出测试版(alpha 版本已经可用)。
You could use Selenium for this. I suggest you use the version 2 which is currently in development and should have a beta available soon (alphas are already available).
看看 Selenium,它是一个测试 Web 应用程序(以及事实上的网站)的系统,您可以编写所有测试在Java中。有一个名为 Tellurium 的项目,基于 Selenium,但 Tellurium 可以与 groovy 和 DSL 配合使用,一开始可能会更容易处理。
这是如何工作的?
Have a look at Selenium, it's a system to test web applications (and de facto websites) you can write all your tests in java. There is an ather project named Tellurium, based on Selenium but Tellurium works with groovy and a DSL, it might be easier to handle at first.
How does this works ?
您还可以利用网络的性质。没有真正的理由渲染表单、填写并提交它来测试表单处理代码。表单的显示是一个HTTP请求,提交是另一个。通过模拟浏览器将发送的内容并断言其处理正确来测试表单提交代码是完全合理的。
您确实需要确保表单呈现和提交测试代码同步,但您也不一定需要对此进行完全集成。
有些工具允许在不启动浏览器的情况下进行测试...我想到的一个是 HTMLUnit (和还有其他的)。如果您发现 Selenium 编写起来很痛苦,或者测试很脆弱或不稳定,请寻找像这样的更简单的工具。
You can also exploit the nature of the web. There's no real reason to render a form, fill it out and submit it to test the form processing code. The display of the form is one HTTP request, and the submission is another. It's perfectly reasonable to test form submission code by mocking up what a browser would send and asserting that it's handled correctly.
You do need to make sure that the form rendering and submission test code are in sync, but you don't necessarily need a full integration for this either.
There are tools that allow testing without booting up a browser... one that springs to mind is HTMLUnit (and there are others). If you find that Selenium is a pain to write, or the tests brittle or flakey, look for simpler tools like this.
我建议您尝试机器人框架。这是由诺基亚西门子通信工程师开发的开源测试框架。
它主要基于 python 和 Selenium 测试库构建。它还支持通过 Jython 库测试 Java/J2EE 服务器端代码。我个人有时会在工作中使用它,编写测试用例就像通过使用关键字描述端到端流程一样简单(大多数必需的关键字已经内置)。如果您发现 Selenium 很难使用,您可以尝试一下。 Robot 框架提供了对原始 selenium 的相当简单的抽象,并且还具有进行 Java/J2EE 服务器端调用的功能。
问候,
纳根德拉大学
I suggest you to try the Robot Framework. This is an open source testing framework developed by engineers in Nokia Siemens Networks.
It is primarily built on python and the Selenium testing libraries. It also includes support for testing Java/J2EE server side code through Jython libraries. I personally use it in my work sometimes, and writing a test case is just as easy as describing an end-to-end flow through the use of Keywords (most of required ones are already inbuilt). You could go ahead and give this a shot if you find Selenium a li'l tough to work with. The Robot framework provides a fairly simple abstraction over raw selenium, coupled with the power to make Java/J2EE server-side calls too.
Regards,
Nagendra U M