开发者

how can I add code reuse to my Selenium tests?

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 a开发者_StackOverflow中文版fter that)?


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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜