Question

So I have an interesting conundrum I was curious to get some feedback from other Webdriver framework architects. Currently I follow a pretty standard execution model:

  • baseobject
  • pageobject (extends baseobject)
  • Junit testobject (references one or multiple pageobjects)

Within my pageobjects I chose to define my findBy UI mappings as variables, and in-turn reference them within the various methods I write for that pageobject. I find this works very well. However, one item I am waffling on is how to handle method design for pages (and their respective pageobject) when there exist potentially 50 separate hyperlinks.

My inclination and design thus far has been to create methods (I think of them as services really) for each link on most pageobjects I've created so that @Test I can simply call the method I want and be done with it. This eliminates the potential for test maintenance...standard practice I know. But I am now trying to decide...does it make sense to create 50 methods, one for each link for a page object, or do I go against my wishes and pass in linktext from the test itself, feeding into a single method that builds the findBy using that passed in parameter.

On one hand there is way less code within the pageobject, but on the other, tests become more brittle. There is potential for these links to be references in hundreds of tests.

Here is a brief example of my model:

classname extends baseobject{

   By someLocator = By.linkText("some text");
   By someOtherLocator = By.linkText("some other text");
   By andAnotherLocator = By.id("someid");

   public void someLinkMethod(){
    driver.findElement(someLocator).click();
   }

   public void someOtherLinkMethod(){
    driver.findElement(someOtherLocator).click();
   }

   public void someidMethod(){
    driver.findElement(andAnotherLocator).click();
   }

}

Thus we come to the end of the question. This model works great for test design. My services (methods) are insulated and easily maintainable. But what would I do if there were 50 UI mappings for links instead of 2 as I have shown above? I toyed with the following design, but really dislike it @Test:

public void selectFromLeftBar(String barItem){
  driver.findElement(by.linkText(barItem)).click();
}

Any thoughts would be greatly appreciated!

Was it helpful?

Solution

Do it in your page object class. Here are the reasons:

  1. What does your code do if your page changes the link text? You have to go into each test and change that text, even if the link does the same thing.

  2. What happens if your page removes that link? You are stuck with the same problem, namely, having to find each time you call that link. If its a method...then you delete the method, and your IDE notifies you of each instance that you used it.

  3. Finally, you are providing a standard interface for the test. If you make an exception here, what would stop you from passing other things into your page?

As a side note, I would recommend only mapping elements that you are going to use. I've found that if I map out every element I could possibly ever need then I end up with a massive class filled with fluff and less time on my hands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top