在 Java 中使用 Selenium 的工作流程是怎样的?

发布于 2024-12-05 11:02:02 字数 524 浏览 0 评论 0原文

我真的对 Selenium 网站感到困惑。起初他们竭尽全力解释 Selenium IDE,但它输出的是 HTML 测试用例。但是当你查看Java文档时,它使用的是纯java的WebDriver,你的HTML测试用例是没有用的。我发现从 Selenium IDE 导出到 JUnit 功能,但它不使用 WebDriver api,而是将其包装在 Selenium api 内,这甚至不起作用。我收到错误消息,提示我一次只能进行一次会话。我只进行了一项测试,使用 netstat 确保我没有任何其他软件在该端口上侦听,并断开了 selenium 实例的连接。这是行不通的。此外,测试用例是从已弃用的类扩展而来的。

您无法从 Java 代码将测试用例返回到 Selenium IDE,因此您可以在此时丢弃 Selenium IDE。

我将测试用例转换为纯 WebDriver 并且让它工作。那么使用 selenium 和 JUnit 的推荐工作流程是什么?我是否应该忘记 Selenium IDE 和在浏览器中记录操作并只用 Java 编写所有内容?或者是否仍然可以以某种方式使用 Selenium IDE?

I'm really confused by the Selenium web site. At first they go to great lengths to explain Selenium IDE, but it outputs HTML test cases. But when you go to the Java documentation, it uses WebDriver which is pure java and your HTML test cases are useless. I found an export to JUnit feature from Selenium IDE but it doesn't use the WebDriver api, it wraps it inside the Selenium api, which doesn't even work. I get errors that I can only have one session at a time. I had only one test, made sure using netstat that I didn't have any other software listening on the port and disconnected the selenium instance. It just wouldn't work. Additionally the testcase extended from a deprecated class.

You cannot get back your test case to Selenium IDE from Java code so you can at that point throw the Selenium IDE away.

I converted the test case to pure WebDriver and I got it to work. So what is the recommended workflow for working with selenium and JUnit? Should I forget about Selenium IDE and recording actions in browser and just write everything in Java? Or is it still possible to use Selenium IDE somehow?

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

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

发布评论

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

评论(3

儭儭莪哋寶赑 2024-12-12 11:02:02

最近完成了一个使用 Selenium 2.0 的项目,我发现 Selenium IDE 仅适用于原型测试。

该 IDE 有几个缺点,无法用于运行 Selenium 测试。我记得以下内容:

  • 通常您会希望在套件中运行测试。虽然 IDE 确实有这个功能,但我发现 IDE 缺少一个更重要的功能,即运行测试设置和拆卸脚本。这在 JUnit/TestNG 中实现起来很简单,但对于 HTML 中记录的脚本来说却相当痛苦。简而言之,除非您使用单元测试库从 Java 运行测试,否则记录的测试是不可维护的。
  • 测试中的数据不能在测试之间共享;您将需要在每个需要它的测试中复制数据。当测试以 HTML 等表示语言存储时,这是预期的。
  • 导出测试的默认格式不使用 page-object 设计模式 (这对于组织 Selenium 测试非常有效)。我没有尝试为此创建自己的格式模板,但这只是让我相信涉及 WebDriver 和 JUnit/TestNG 的最佳测试是手工编写的。

使用 Selenium IDE 的最佳方法是创建失败测试的记录(由功能测试人员),而不是直接将测试导出到测试套件中。您可以使用 IDE 记录初步测试,以便捕获测试的重要方面(断言/验证调用),然后在套件中重写它。

Having recently completed a project that used Selenium 2.0, I could find that the Selenium IDE is good only for prototying tests.

There are several drawbacks with the IDE that prevent it from being used to run Selenium tests. I could recall the following:

  • Typically you would want to run tests in a Suite. While the IDE does have this feature, I found that the IDE lacks a more important feature of running test setup and tear down scripts. This is trivial to achieve in JUnit/TestNG, but quite a pain with the recorded scripts in HTML. In short, the recorded tests aren't maintainable until you use a unit-testing library to run the tests from Java.
  • Data within the tests cannot be shared across tests; you will need to duplicate data in each test that requires it. This is expected when the tests are stored in a presentation language like HTML.
  • The default format of the exported tests does not use the page-object design pattern (which works very well for organizing Selenium tests). I didn't attempt creating my own format template for this, but this only convinced me that the best tests involving WebDriver and JUnit/TestNG are written by hand.

The optimal way of using the Selenium IDE is to create recordings of failed tests (by functional testers), instead of directly exporting the tests into your test suite. You could use the IDE to record the preliminary test so that the important aspects of the test (the assert/verify calls) are captured, and then rewrite it in your suite.

櫻之舞 2024-12-12 11:02:02

我使用IDE创建一个“工作流程脚本”,将其转换为java代码。然后我用 Java 从头开始​​编写所有内容,但使用转换后的 IDE 脚本中的信息。这将包含所有 ID 等,而且还会按照您计划“单击”的顺序,甚至可能会立即复制其中的某些部分。无论如何,它确实会加快速度,但如果您使用网络驱动程序,事情会变得更加复杂,而且我还没有转移到最新版本。

干杯
斯特凡

I use the IDE to create a "Work Flow Script", convert it into java code. Then I write everything from scratch in Java but with the info from the converted IDE script. That will have all ID's and so on but also in what order you have planned to "click" around, even some parts of it might be copied right off. Anyway it does speed things up a bit, but if you are using the webdriver it will complicate things a bit more and I have not yet moved over to the latest version.

Cheers
Stefan

撩发小公举 2024-12-12 11:02:02

关键是您可以使用其中任何一种,具体取决于您的目标。就您而言,WebDriver 听起来是不错的选择。

如果您想生成 HTML 测试用例,Selenium IDE 非常有用。

Selenium WebDriver 对于用 Java(或其他语言)编写单元测试很有用。

有关来源的明确指示,请参阅 SeleniumHQ 主页。它有一个关于“Selenium 的哪一部分适合我?”的部分,它回答了您的问题。

The point is you can use either one, depending on your goal. In your case, WebDriver sounds like the way to go.

Selenium IDE is useful if you want to generate the HTML test cases.

Selenium WebDriver is useful for writing unit tests in Java (or other languages).

For a clear indication of this from the source, see the SeleniumHQ home page. It has a section on "Which part of Selenium is appropriate for me?", which answers your question.

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