/website-automation-framework-phptravel

Selenium test automation framework with PHP travel test site with description on how to set it up from scratch

Primary LanguageJava

automation-framework

Selenium test automation framework This is a step by step documentation on how to build an automation framework from scratch using http://www.phptravels.net/ as the test site.

How to set up an automation framework from scratch?

The basics

  • Choose a Development Environment and download it
  • Create a new project

1. The pom file

Sort the maven pom file out: start a new maven project that gives the structure. It is a maven structure not for the automation project. Open the pom.xml file. Add the dependencies you need. You can get it from here. https://mvnrepository.com Examples: cucumber-java,cucumber-junit, junit, selenium, appium etc.

2. Create the basic test framework structure

Important: the feature files go into resource directory and everything else goes into the test dir. (In this framework there is a java directory under test) src -> test -> java -> yourprojectname (optional) -> supporting code src -> test -> resources -> yourprojectname (optional) -> feature files (This resources dir where your feature files live needs to be marked as source root.Right click>mark as)

3. Cucumber runner

Create cucumber runner in the root dir of test/java because Cucumber runs on Junit in java. The dependencies will be crucial at this point. Luckily there is great documentation on how to set up this class. Go to:https://cucumber.io/docs/reference/jvm If you happen to have issues with the dependencies, enable auto import and let magic happen.

4. The BasePage

4.1 The webdriver

How to get firefox driver? (Steps are pretty similar for other browsers too) You can download geckdriver from here: https://github.com/mozilla/geckodriver/releases

I suggest setting up the driver on the BasePage. Do not forget to: import org.openqa.selenium.WebDriver; Create a constructor for the basePage with your webdriver. So every page has a WebDriver object called (for simplicity we can call it driver).

So it looks like this

protected WebDriver driver;

   public BasePage(WebDriver myDriver) {
       driver = myDriver;
   }

Remember that protected WebDriver driver = new WebDriver(); is not a good idea because: when you create a web driver it starts a new browser window. Instead you want the use the constructor to pass around the same object.

4.2 Commonly used methods

If you declare a ‘loadPage’ method on the BasePage the problem is that every page will have a different URL. You can solve this by extending BasePage. However you must declare this method abstract. By creating protected abstract String url(); you don’t need to implement it now, but anybody that extends your class must implement it. And hence every page needs to implement a url(), we can create the following method on the BasePage.

public void loadPage() { driver.get(url()); }

BasePage should contain the methods that are used all over the tests in order to avoid duplications.

5. The hooks (before and after)

Hooks allow you to perform actions at various points in the cucumber testcycle. This is commonly used for driver management. (before, after hooks) and the information should live in a support class or on a step definition.

protected WebDriver driver;

   @Before
   public void setUp() {
       System.setProperty("webdriver.gecko.driver", "/directoryxyz/geckodriver");
       driver = new FirefoxDriver();
       landingPage = new LandingPage(driver);
   }

6. Start building features and step def files

Recommended basic structure:

  • Feature files
  • Step definitions
  • Respective page files
  • Hooks
  • Base Page

Abstraction and patterns and framework organisation

1. The hooks and page initializations

To get away from the original design, hooks are not living in a support dir but instead in the hooks directory in the Hooks class. Since Cucumber will scan for the before hook before running any test it could live anywhere but for organisational purposes it is better to keep it clean and just put it into the hooks directory. The structure is the following:

  1. Cucumber scans for for the before hook.
  2. Jumps to where the new driver is initialized. In my case it is in the SessionContext class
  3. Jumps to where the new page is initialized. In my case it is in the SessionContext class
  4. In the SessionContext this will be a freely avaialble object with set driver and get driver methods.
  5. This is where the step definitions will get the driver and the pages.
  6. The constructor of the Pages class will create each individual class. This needs to be extended when a new page is added to the framework.

public Pages(WebDriver driver){ landingPage = new LandingPage(driver); loginPage = new LoginPage(driver);

}

The singleton patter is useful because exactly one object is needed to coordinate actions across the system.