如何将代码重用添加到我的 Selenium 测试中?
这是我正在处理的情况:
- 在 Selenium 中构建测试
- 让所有测试正确运行(在 Firefox 中)
- 将所有测试导出到 MSTest(以便每个测试都可以在 IE、Chrome 和 FF 中运行)
- 如果有任何测试需要进行修改,在 Selenium IDE 中进行编辑
所以这是一个非常单向的工作流程。然而,我现在想做更多的自动化。例如,我希望每个测试都在两个帐户下运行。我遇到了维护问题。如果我想在两个帐户下运行 6 个测试,突然间我需要在 Selenium IDE 测试中进行 12 个测试。这编辑太多了但大量代码是完全相同的。
如何在测试之间共享 Selenium 测试块?我是否应该第一次使用 Selenium IDE 来开发测试,然后不再使用它(之后只在 VS 中进行编辑)?
Here's the situation that I'm working with:
- Build tests in Selenium
- Get all the tests running correctly (in Firefox)
- Export all the tests to MSTest (so that each test can be run in IE, Chrome and FF)
- If any test needs to be modified, do that editing in Selenium IDE
So it's a very one-way workflow. However, I'd now like to do a bit more automation. For instance, I'd like every test to run under each of two accounts. I'm getting into a maintenance issue. If I have 6 tests that I want to run under two accounts, suddenly I'd need 12 tests in the Selenium IDE tests. That's too much editing. But a ton of that code is exactly the same.
How can I share chunks of Selenium tests among tests? Should I use Selenium IDE to develop the test first time then never use it again (only doing edits in VS after that)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 IDE 导出后,Selenium 代码非常线性。
例如(忽略语法):
这是登录页面。您的每一项测试都必须使用它。您应该将其重构为方法。
performLogin()
方法不必与测试代码本身位于同一文件中。您可以使用您的方法为其创建一个单独的类,并在测试之间共享它。我们有与 UI 上的某些功能相对应的类。例如,我们有很多方法可以在我们的应用程序中进行搜索。所有帮助您使用搜索功能的方法都将位于 SearchUtil 类中。
类似地构建您的测试将为您带来以下优势:
此网站是您想要实现的目标的绝佳资源。
祝你好运!
Selenium code is very linear after you export it from the IDE.
For example (ignore syntax):
This is the login page. Every single one of your tests will have to use it. You should refactor it into a method.
The
performLogin()
method does not have to be in the same file as your test code itself. You can create a separate class for it with your methods and share it between your tests.We have classes that correspond to certain functionalities on our UI. For example, we have many ways to search in our app. All methods that helps you with search functionality will be in the SearchUtil class.
Structuring your tests similarly will give you the following advantages:
This website is an excellent resource on what you are trying to accomplish.
Good Luck!
关于代码重用,需要考虑两个方面:
我应该指出,我的评论很大程度上倾向于您正在使用的单向工作流程,jcollum,但更重要的是:我使用 IDE 为给定的测试用例生成代码仅一次。我从来没有回到 IDE 修改测试用例并重新导出它。 (当我想在代码中微调和自定义测试用例(在我的例子中,C#)时进行试验时,我确实将 IDE 测试用例保留为诊断工具。
我赞成使用 IDE 测试的原因仅在于一个出发点是:
所以回到 IDE 生成的代码:它总是如此。示例:
生成此代码块:
每个后续 verifyText 命令都会生成相同的代码块,仅两个参数不同。
我对这种刺鼻代码气味的解决方案是开发 <。 Selenium Sushi,一个 Visual Studio C# 项目模板和库,可让您消除大部分(如果不是全部)这种重复。有了这个库,我可以简单地编写这一行代码来匹配 IDE 测试用例中的原始代码行:
我有一篇详细的文章介绍了这一点 (使用 Selenium Sushi 进行 Web 测试:实用指南和工具集) 2011 年 2 月刚刚在 Simple-Talk.com 上发布。
There are two aspects to consider regarding code reuse:
I should point out that my comments lean heavily to the one-way workflow that you are using, jcollum, but even more so: I use IDE to generate code just once for a given test case. I never go back to the IDE to modify the test case and re-export it. (I do keep the IDE test case around as a diagnostic tool when I want to experiment with things while I am fine-tuning and customizing my test case in code (in my case, C#).
The reasons I favor using IDE tests only as a starting point are:
So back to IDE-generated code: it always has massive amounts of duplication. Example:
generates this block of code:
Each subsequent verifyText command generates an identical block of code, differing only by the two parameters.
My solution to this pungent code smell was to develop Selenium Sushi, a Visual Studio C# project template and library that lets you eliminate most if not all of this duplication. With the library I can simply write this one line of code to match the original line of code from the IDE test case:
I have an extensive article covering this (Web Testing with Selenium Sushi: A Practical Guide and Toolset) that was just published on Simple-Talk.com in February, 2011.
您还可以将一些片段或单行代码放入
IDE 的“代码片段”集合中(我使用 Eclipse)。
这不是真正的重用,但是它对我来说适用于一次性测试脚本或现有测试脚本的快速增强。
You can also put some fragments or one-liners, e.g.
into the "code snippets" collection of your IDE ( I use Eclipse).
That's not true reuse, but hey it works for me for throwaway testscripts or quick enhancements of existing testscripts.