Lesson 5: Complex Page-Object functions
One issue that persists with Page Object classes is that you will often end up writing the same lines of code in your test using these easy to implement functions. While these will still be much faster than not having the functions at all, more time can be saved.
[Example where country is searched, then clicks on capital then assesses the mayor’s name]
This example perfectly illustrates how much more time can be saved writing tests, by implementing this method you can save a lot of time by individually writing each line of code that will go into performing a straightforward and frequently reused behaviour.
For this lesson we will create a test that gets the founding date of a capital city from a country's page and checks that it is correct. For this we will need a new page object class CapitalPage.
HomePage homePage = new HomePage(driver); CountryPage countryPage = homePage.searchForCountryName("France"); CapitalPage capitalPage = countryPage.clickCapital(); Assert.assertTrue("Cannot find Mayor 'Anne Hidalgo'.", capitalPage.getMayor().equals("Anne Hidalgo"));
Assert.assertTrue("Cannot find Mayor 'Anne Hidalgo'.", homePage.getCapitalsMayorFromCountry("France").equals("Anne Hidalgo"));
The first code snippet shows the lines of code required in the example to perform a function that the user would want to reuse later in other tests. The second code snippet shows how the user can consolidate all that code into one method.