3

I just want to ask ideas from you on which is more appropriate to use. I'm currently automating a project using Selenium Webdriver adapting the Page Object Model approach. In the midst of my testing, I realize if it's a good way to separate every steps, or put it in one single workflow? Is it OPTION A or B ?. I provided my pseudo-code below.

Example:

=== OPTION A (3 steps, each step has a method) ===

class TestCase{ // the controller

  method login{
    enterUsername();
    enterPassword();
    clickLoginButton();
  }

}

class LoginPage{ // the view

  method enterUsername(){
     sendKeys("user101")
  }

  method enterPassword(){
     sendKeys("password1234")
  }

  method enterPassword(){
     loginButton.click();
  }

}

=== OPTION B (3 steps are in one method)===

class TestCase{ // the controller

  method login{
     userLogin();
  }

}

class LoginPage{ // the view

  method userLogin(){
     sendKeys("user101")
     sendKeys("password1234")
     loginButton.click();
  }
}
4

I would base this on whether the individual components will be used in other tests.

If the methods like enter username are only ever used together with the password methods, then make them be 1 method ('userLogin').

However if you have other tests such as entering a username, but forgetting to enter the password and submitting the form, i.e. testing the sad path when the user fails to enter a required field. This test would want the 'enter username method to be on its own so it would make sense to break it out into its own method.

  • oh yes, thanks , I now get it! Most especially, there are many instances when the application may ask you to enter username. Very useful answer. Thanks Michael – Jong Onin Oct 14 '16 at 10:59
  • Yes I think this is good explanation. Depends on requirements we should write codes – Sagar007 Oct 14 '16 at 12:02
  • ' <-- sorry, you had an unclosed quote and it bugged me – ArtOfCode Oct 14 '16 at 16:57
2

I would prefer option B for now. Apply the YAGNI (You Aren't Gonna Need It) principle. Do you need the functions now? really? ask yourself and only build what you need, not what you expect to need.

In your case I expect most test will just use the login function and not the separate ones.

Just like Michael wrote, currently you do not need more than a userLogin() function. So having one function makes sense. When you get more tests that have a need for separate functions split it up, but keep the original function so you do not need to update all the tests.

Split and refactor your Page Objects when ever you need to make it more complex, but probably you are not going to need it. So don't waste time on it yet.

0

In general, I like Option A because I find it provides a great deal more reusability - it's easier to reuse parts of your page objects when they're presented in smaller chunks. It may not matter for your login PO much, but if you carry the pattern over to your other pages and tests you may find it useful. This also helps avoid creating several methods that do very similar things, but vary slightly (login(), loginWithoutPassword(), etc). The biggest downside I've found is that it makes your tests a little longer and possibly a bit more difficult to read. For me, I found the trade-offs were worthwhile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.