How can I add code reuse to my Selenium tests?

Here is the situation I'm working with:

  • Building tests in Selenium
  • Get all tests correctly (in Firefox)
  • Export all tests to MSTest (so that each test can be run in IE, Chrome and FF)
  • If any test needs to be changed, do this editing in the Selenium IDE

So this is a very one-way workflow. However, I would like to do a little more automation. For example, I would like each test to run under each of two accounts. I deal with maintenance issues. If I have 6 tests that I want to run under two accounts, I suddenly need 12 tests in the Selenium IDE tests. This is too much editing. But the ton of this code is exactly the same.

How can I share pieces of Selenium tests among tests? Should I use the Selenium IDE to develop a test for the first time, and then never use it again (only after that editing in VS)?

+3
source share
3 answers

Selenium code is very linear after it is exported 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. Each of your tests will have to use it. You must reorganize 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 method performLogin()does not have to be in the same file as your test code. You can create a separate class for it using your methods and share it between your tests.

, . , . , , SearchUtil.

:

  • ( ), , ,
  • ,
  • , , . , .
  • , . , .
  • ! . , , , ..

- - , .

!

+3

:

  • - c_maker .
  • , Selenium IDE.

, , , jcollum, : IDE . IDE, . ( 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 # , , . , IDE:

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

, (- Selenium Sushi: ), Simple-Talk.com 2011 .

+2

, .

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

IDE ( Eclipse).

This is not true reuse, but it works for me for test cases or quick improvements to existing test cases.

0
source

Source: https://habr.com/ru/post/1790841/


All Articles