This article is the first from a series about how to do cross browser test automation with Selenium WebDriver.
The series has 3 parts.
Part 1 explains how to run test scripts for one browser.
Part 2 goes into running test scripts on multiple local browsers.
Part 3 concludes with running test scripts in a cloud Selenium grid.
TEST CASE TO BE AUTOMATED
I will use the www.londondrugs.com site.
The test case to be automated consists in
2. clicking on the SEARCH option of the top menu
3. typing a keyword in the SEARCH textbox
5. checking that the keyword is included in the results page
The project uses the following architecture:
The test scripts are created in the test class that belongs to the TESTS layer (of the automation architecture).
The test scripts do not use directly any Selenium WebDriver classes and do not declare a WebDriver object.
They use the page object classes instead.
There are 2 page object classes: Home Page class, Results Page class
They belong to the FRAMEWORK layer of the automation architecture.
Both page object classes implement user actions for each site page by using Selenium WebDriver classes (which correspond to the SELENIUM layer of the automation architecture).
The WebDriver object is being created by a Driver class that belongs to the FRAMEWORK layer as well.
The project uses 2 folders:
TESTS folder: TestScripts_OneBrowser class
FRAMEWORK folder: Settings class, HomePage class, ResultsPage class, Driver class
The test scripts are created in the test class that belongs to the TESTS layer (of the automation architecture).
The test scripts do not use directly any Selenium WebDriver classes and do not declare a WebDriver object.
They use the page object classes instead.
There are 2 page object classes: Home Page class, Results Page class
They belong to the FRAMEWORK layer of the automation architecture.
Both page object classes implement user actions for each site page by using Selenium WebDriver classes (which correspond to the SELENIUM layer of the automation architecture).
The WebDriver object is being created by a Driver class that belongs to the FRAMEWORK layer as well.
The project uses 2 folders:
TESTS folder: TestScripts_OneBrowser class
FRAMEWORK folder: Settings class, HomePage class, ResultsPage class, Driver class
SETTINGS.JAVA
The purpose of this class is to store all settings needed in the project such as
- site url
- keyword search
- page title
- regular expression used for extracting the results number
public class Settings { public final static String siteUrl = "http://www.londondrugs.com"; public final static String keyword = "iphone"; public final static String homePageTitle = "London Drugs"; public final static String resultsPageTitle = "Sites-LondonDrugs-Site"; public final static String resultsCountRegEx = "(.*)(of )(\\d+)( matches)"; }
DRIVER.JAVA
This class creates the browser driver and stores it into a static class member.
By default, the browser driver is for Firefox.
The driver object is created in a static initialize() method.
The driver object is closed in a static quit() method.
The initialize() and quit() methods are created as static so that
- there is no need of declaring a WebDriver object in the test class
- the page object classes do not need to pass the driver object from one to another
import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.NotFoundException; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; package FRAMEWORK; public class Driver { private static WebDriver driver; public static WebDriver getDriver() { return driver; } public static void initialize() { driver = getFirefoxDriver(); } public static void quit() { getDriver().quit(); } }
HOME PAGE OBJECT CLASS
The home page class declares a few locator members for the
- search menu item element
- search text box element
- submit button
It declares members for the driver and explicit wait objects that are initialized in the class constructor.
The class constructor opens the site's home page in the url and checks if the home page title is correct.
The home page class has also a search method that
- clicks the search menu item
- types the keyword in the search text box
- clicks the submit button
The result of the search method is a ResultsPage object.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; package FRAMEWORK; public class HomePage { String searchMenuItemLocator = "//div[@id='search-cta']"; String searchTextBoxLocator = "//input[@id='searchinput']"; String searchButtonLocator = "//button[@name='simplesearch']"; WebDriver driver; WebDriverWait wait; public HomePage() { this.driver = Driver.getDriver(); wait = new WebDriverWait(driver, 10); driver.get(Settings.siteUrl); wait.until(ExpectedConditions.titleIs(Settings.homePageTitle)); } public ResultsPage search() { wait.until(ExpectedConditions.elementToBeClickable(By.xpath(searchMenuItemLocator))). click(); wait.until(ExpectedConditions.elementToBeClickable(By.xpath(searchTextBoxLocator))). sendKeys(Settings.keyword); wait.until(ExpectedConditions.elementToBeClickable(By.xpath(searchButtonLocator))). click(); return new ResultsPage(); } }
RESULTS PAGE OBJECT CLASS
The results page class declares a few locator members for the
- search keyword element
- results count element
It declares members for the driver and explicit wait objects that are initialized in the class constructor.
The class constructor checks if the results page title is correct.
The results page class has the following methods
- isKeywordDisplayed()
checks if the keyword is displayed in the results page
- resultsCount()
returns the number of results;
uses the extractValue method which uses a regular expression for extracting the results count
The results of the search method is a ResultsPage object.
import java.util.regex.Matcher; import java.util.regex.Pattern; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; package FRAMEWORK; public class ResultsPage { String searchKeywordLocator = "//h2[@class='product-search']/a"; String resultsCountLocator = "(//div[@class='resultshits'])[1]"; WebDriver driver; WebDriverWait wait; public ResultsPage() { this.driver = Driver.getDriver(); wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.titleIs(Settings.resultsPageTitle)); } public Boolean isKeywordDisplayed() { return wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath(searchKeywordLocator))).isDisplayed(); } public int resultsCount() { WebElement resultCountElement = wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath(resultsCountLocator))); return Integer.parseInt(extractValue(resultCountElement.getText(), Settings.resultsCountRegEx, 3)); } public String extractValue(String textValue, String regEx, int position) { String result = ""; Pattern pattern = Pattern.compile(regEx); Matcher matcher = pattern.matcher(textValue); if (matcher.find()) result = matcher.group(position); return result; } }
TEST CLASS
It includes the following methods:
- setUp()
It uses the @Before annotation so it is executed automatically before the test script.
It initializes the browser driver.
It initializes the browser driver.
- tearDown()
It uses the @After annotation so it is executed automatically after the test script.
It closes the browser driver.
It closes the browser driver.
- testResultsInfo()
It uses the HomePage and ResultsPage classes for implementing the test script.
import static org.junit.Assert.*; import java.net.MalformedURLException; import org.junit.After; import org.junit.Before; import org.junit.Test; import FRAMEWORK.Driver; import FRAMEWORK.HomePage; import FRAMEWORK.ResultsPage; public class TestScripts_OneBrowser { @Before public void setUp() throws MalformedURLException { Driver.initialize(); } @After public void tearDown() { Driver.getDriver().quit(); } @Test public void testResultsInfo() throws InterruptedException { ResultsPage results = (new HomePage()).search(); assertTrue(results.isKeywordDisplayed() == true); assertTrue(results.resultsCount() > 0); } }
The code included in the article works.
If you have any difficulty using it or have any questions, please leave them in the Comments section.
Part 2 will show how to extend this code so that the test script runs in multiple local browsers.
0 Comment to "Do Cross Browser Test Automation Like A PRO"
Post a Comment