The next and final step is to use the newly generated code to write a test. Start by creating a initializer method to create the driver, config and site object like this
@Before public void initSite() throws MalformedURLException, MissingElementsException { Properties p = new Properties(); config = new ConfigurationFromProperties(p); driver = new FirefoxDriver(); site = new AmazonSite(config, driver, new HttpUrl("http://www.amazon.com")); homePage = site.pages().home().load(); } @After public static void closeDriver() { driver.close(); }
Next, for the actual test, you start with the site
object and dig down
@Test public void testBookSearchResultsCount() throws MalformedURLException, MissingElementsException { homePage.searchBar().departmentsDropDown().selectByVisibleText("Books"); homePage.searchBar().searchText().sendKeys("game of thrones"); SearchResultsPagePage resultsPage = homePage.searchBar() .goButton().clickAndTransit(); ComponentList<SearchResultsPagePage.ASearchResultElementComponent> results = resultsPage.results(); Assert.assertEquals(12, results.getComponents().size()); }
So in the first line, we start by getting the searchBar from the homepage, obtaining the departments dropdown and selecting the Books value. Next we get the query field and write some text, finally, we click the goButton. Note that we have a method that is not present in Selenium, clickAndTransit
. This method click the button and expect the page to change according to the transition we declared in our a34 file. If multiple transitions are possible, multiple clickAndTransitToXXX
methods will be available. From this point there will be a validation that the page has changed and that the elements declared on the next page are all present before continuing. When you declare your elements, you can use the is lazy
keyword to specify that the component is to appear later on the page and not when the page is loaded. Only elements that are not marked as lazy are expected when the page is loading.
Next on our test, we get the results and check that we have 12 results. Test done !