How to design Selenium 2 tests using a page object template using inheritance

Now we are writing a new test suite using Selenium 2 (Webdriver) and TestNG . Our tests use an Object Object , and we are very pleased with how everything looks like this. However, we ran into a design problem with our tests, and we seem to be unable to find a good solution for this. Let me give you an example. Here is our LoginTestCase:

public class LoginTestCase extends MyTestCase { @BeforeTest public void login() { HomePage homepage = PageFactory.initElements(getDriver(), HomePage.class); LoginPage loginPage = homepage.login(); DashboardPage dashboardPage = loginPage.loginUser("username", "password"); } } 

We would like to expand our tests that require user login from this test. Ideally, we could write something like this:

 public class DashboardTestCase extends LoginTestCase { @Test public void testDashboard(DashboardPage dashboardPage) { ... } } 

At this point, the user is in DashboardPage , and the only thing needed is the object of this page that was created in LoginTestCase .

I know that the obvious solution is to save this object in a variable (in LoginTestCase ), after which there will be access to children's test cases. However, this looks very ugly and can lead to misuse of this variable.

Is there a better solution for this or some kind of template that solves this problem?

+4
source share
3 answers

We used dependency injection and Guice with Guice-berry to solve this problem. Page objects were introduced into test methods, and WebDriver was introduced into page objects.

As a compromise, we did not use all this nice little chain between page objects.

But static state and singletones are a pain in the ass when it comes to running a parallel test, so they should be avoided.

+3
source

I have not yet found a better solution. I have a Singelton WebDriver in a static class where all my test windows can access WebDriver. My test classes must be running on the same WebDriver due to login / session.

+1
source

I think you should save the link to the page object in a private variable in the LoginTestCase class and provide a secure getter so that child tests can access the page object. This helps to achieve two things.

  • Encapsulate the variable with your LoginTestCase so that no one can mess it up with.
  • Providing a secure getter ensures that only test files for children will have access to it and what you want in the end. so this is not a problem.

This is how your code will look after the changes. Hope this makes sense:

 public class LoginTestCase extends MyTestCase { private dashboardPage; @BeforeTest public void login() { HomePage homepage = PageFactory.initElements(getDriver(), HomePage.class); LoginPage loginPage = homepage.login(); dashboardPage = loginPage.loginUser("username", "password"); } protected void getDashBoardPage() { return dashboardPage; } } public class DashboardTestCase extends LoginTestCase { @Test public void testDashboard() { DashboardPage dashboardPage = getDashBoardPage(); ... } } 
0
source

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


All Articles