如何将代码重用添加到我的 Selenium 测试中?

发布于 2024-10-16 19:19:56 字数 413 浏览 1 评论 0原文

这是我正在处理的情况:

  • 在 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 技术交流群。

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

发布评论

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

评论(3

许仙没带伞 2024-10-23 19:19:56

从 IDE 导出后,Selenium 代码非常线性。

例如(忽略语法):

  someTestMethod() {
     selenium.open("http://someLoginPage.com");
     selenium.type("usernameField", "foo");
     selenium.type("passwordField", "bar");
     selenium.click("loginButton");
     selenium.waitForPageToLoad("30000");
     assertTrue(selenium.isTextPresent("Welcome * foo"));
  }

这是登录页面。您的每一项测试都必须使用它。您应该将其重构为方法。

  someTestMethod(){
     selenium.open("http://someLoginPage.com");
     String username = "foo";
     String password = "bar";
     performLogin(username, password);
  }

  performLogin(String username, String password){
      selenium.type("usernameField", username);
      selenium.type("passwordField", password);
      selenium.click("loginButton");
      selenium.waitForPageToLoad("30000");
      assertTrue(selenium.isTextPresent("Welcome * foo"));
  }

performLogin() 方法不必与测试代码本身位于同一文件中。您可以使用您的方法为其创建一个单独的类,并在测试之间共享它。

我们有与 UI 上的某些功能相对应的类。例如,我们有很多方法可以在我们的应用程序中进行搜索。所有帮助您使用搜索功能的方法都将位于 SearchUtil 类中。

类似地构建您的测试将为您带来以下优势:

  • 如果 UI 更改(字段的 id),您可以使用一种方法,更新 id,然后就可以开始了
  • 如果 >逻辑更改流程您也只有一个地方可以更新
  • 测试您的更改是否有效,您只需运行其中一项测试即可验证。所有其他测试都使用相同的代码,因此它应该可以工作。
  • 当您查看代码时,会更具表现力。使用命名良好的方法,您可以创建更容易阅读和理解的更高级别的抽象。
  • 灵活且可扩展!可能性是无限的。此时,您可以使用条件、循环、异常,您可以执行自己的报告等...

此网站是您想要实现的目标的绝佳资源。

祝你好运!

Selenium code is very linear after you export it from the IDE.

For example (ignore syntax):

  someTestMethod() {
     selenium.open("http://someLoginPage.com");
     selenium.type("usernameField", "foo");
     selenium.type("passwordField", "bar");
     selenium.click("loginButton");
     selenium.waitForPageToLoad("30000");
     assertTrue(selenium.isTextPresent("Welcome * foo"));
  }

This is the login page. Every single one of your tests will have to use it. You should refactor it into a method.

  someTestMethod(){
     selenium.open("http://someLoginPage.com");
     String username = "foo";
     String password = "bar";
     performLogin(username, password);
  }

  performLogin(String username, String password){
      selenium.type("usernameField", username);
      selenium.type("passwordField", password);
      selenium.click("loginButton");
      selenium.waitForPageToLoad("30000");
      assertTrue(selenium.isTextPresent("Welcome * foo"));
  }

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:

  • If the UI changes (an id of a field), you go to your one method, update the id and you are good to go
  • If the flow of your logic changes you also have only one place to update
  • To test whether your changes worked, you only have to run one of the tests to verify. All other tests use the same code so it should work.
  • A lot more expressive as you look at the code. With well named methods, you create a higher level of abstraction that is easier to read and understand.
  • Flexible and extensible! The possibilities are limitless. At this point you can use conditions, loops, exceptions, you can do your own reporting, etc...

This website is an excellent resource on what you are trying to accomplish.

Good Luck!

沫尐诺 2024-10-23 19:19:56

关于代码重用,需要考虑两个方面:

  1. 消除您自己的代码库中的代码重复 - c_maker 谈到了这一点。
  2. 消除 Selenium IDE 生成的代码中的重复代码。

我应该指出,我的评论很大程度上倾向于您正在使用的单向工作流程,jcollum,但更重要的是:我使用 IDE 为给定的测试用例生成代码仅一次。我从来没有回到 IDE 修改测试用例并重新导出它。 (当我想在代码中微调和自定义测试用例(在我的例子中,C#)时进行试验时,我确实将 IDE 测试用例保留为诊断工具。

我赞成使用 IDE 测试的原因仅在于一个出发点是:

  • IDE 测试总是会在一个测试和另一个测试之间存在大量代码重复;这就是代码的本质,
  • 我可以使测试用例更加“用户友好”。 ,即我可以将晦涩难懂的定位器封装在有意义的命名属性或方法中,这样测试用例在做什么就
  • 比在 IDE 中工作更清楚,

所以回到 IDE 生成的代码:它总是如此。示例:

verifyText "//form[@id='aspnetForm']/div[2]/div/div[2]/div[1]/span" Home

生成此代码块:

try
{
  Assert.AreEqual("Home",
    selenium.GetText("//form[@id='aspnetForm']/div[2]/div/div[2]/div[1]/span"));
}
catch (AssertionException e)
{
  verificationErrors.Append(e.Message);
}

每个后续 verifyText 命令都会生成相同的代码块,仅两个参数不同。

我对这种刺鼻代码气味的解决方案是开发 <。 Selenium Sushi,一个 Visual Studio C# 项目模板和库,可让您消除大部分(如果不是全部)这种重复。有了这个库,我可以简单地编写这一行代码来匹配 IDE 测试用例中的原始代码行:

Verify.AreEqual("Home",
  selenium.GetText("//form[@id='aspnetForm']/div[2]/div/div[2]/div[1]/span"));

我有一篇详细的文章介绍了这一点 (使用 Selenium Sushi 进行 Web 测试:实用指南和工具集) 2011 年 2 月刚刚在 Simple-Talk.com 上发布。

There are two aspects to consider regarding code reuse:

  1. Eliminating code duplication in your own code base -- c_maker touched on this.
  2. Eliminating code duplication from code generated by Selenium IDE.

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:

  • IDE tests will always have a lot of code duplication from one test to another; sometimes even within one test. That is just the nature of the beast.
  • In code I can make the test case more "user-friendly", i.e. I can encapsulate arcane locators within a meaningful-named property or method so it is much clearer what the test case is doing.
  • Working in code rather than the IDE just provides much greater flexibility.

So back to IDE-generated code: it always has massive amounts of duplication. Example:

verifyText "//form[@id='aspnetForm']/div[2]/div/div[2]/div[1]/span" Home

generates this block of code:

try
{
  Assert.AreEqual("Home",
    selenium.GetText("//form[@id='aspnetForm']/div[2]/div/div[2]/div[1]/span"));
}
catch (AssertionException e)
{
  verificationErrors.Append(e.Message);
}

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:

Verify.AreEqual("Home",
  selenium.GetText("//form[@id='aspnetForm']/div[2]/div/div[2]/div[1]/span"));

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.

深爱不及久伴 2024-10-23 19:19:56

您还可以将一些片段或单行代码放入

note( "now on page: " .  $sel->get_location() . ", " . $sel->get_title() ; 

IDE 的“代码片段”集合中(我使用 Eclipse)。

这不是真正的重用,但是它对我来说适用于一次性测试脚本或现有测试脚本的快速增强。

You can also put some fragments or one-liners, e.g.

note( "now on page: " .  $sel->get_location() . ", " . $sel->get_title() ; 

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.

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