Creating an Automation Testing Framework With Selenium and SpecFlow

2 min


668
1 comment, 1 share, 668 points
How to Create an Automation Testing Framework With Selenium
How to Create an Automation Testing Framework With Selenium

How to Create an Automation Testing Framework With Selenium

 In this article you will learn How to Create an Automation Testing Framework With Selenium and SpecFlow in a page object model.

How to Create an Automation Testing Framework With Selenium

Bellow you can see a structure of a project and I will try to splice it and explain it by bits :

page_object_selenium_strcuture

AppSettings:

application settings

In Application settings folder we are trying to keep Browser setup and a settings file that will host couple of the details that we want to use in our project:

    public class Browser
    {
        private IWebDriver _driver;

        public IWebDriver LunchBrowser()
        {
            var browser = Zoopla.Default.Browser;
            if (browser.Contains("chrome")) _driver = new ChromeDriver();
            _driver.Manage().Window.Maximize();
            _driver.Navigate().GoToUrl(Zoopla.Default.BaseUrl);
            _driver.Manage().Cookies.DeleteAllCookies();
            _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
            return _driver;
        }

        public void CloseBrowser()
        {
            if (_driver != null) _driver.Quit();
        }
    }
Browser.cs

.settings file

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="ZooplaOnline.AppSettings" GeneratedClassName="Zoopla">
  <Profiles />
  <Settings>
    <Setting Name="BaseUrl" Type="System.String" Scope="Application">
      <Value Profile="(Default)">https://www.google.co.uk</Value>
    </Setting>
    <Setting Name="Browser" Type="System.String" Scope="User">
      <Value Profile="(Default)">chrome</Value>
    </Setting>
  </Settings>
</SettingsFile>
.settings file

Bdd Features:

bdd_feature

Feature: Navigate to Google

Scenario: As a user I should be able to navigate to google
          When I search for something
Bdd feature

Pages:

selenium_pages

Inside Pages folder we can keep all page objects see more details about page objects here.

Base Page:

As you can see in the code bellow inside the base page we have the initialisation of the elements and the HomePage object overloading the Driver.

    public class BasePage
    {
        public IWebDriver Driver;
		private HomePage _homePage;

        public BasePage(IWebDriver driver)
        {
            Driver = driver;
            PageFactory.InitElements(Driver, this);
        }

		public HomePage HomePage
        {
            get
            {
				if (_homePage == null) _homePage = new HomePage(Driver);
				return _homePage;
            }
        }
        
       
    }
BasePage.cs

Is quite obvious that HomePage.cs will extend BasePage  and we don’t have to call InitElements again.

    public class HomePage : BasePage
    {
		public HomePage(IWebDriver driver) : base(driver)
        {
        }

        [FindsBy(How = How.Name,Using = "q")]
        private IWebElement SearchFiled { get; set; }


		public HomePage SearchSomething(string query)
        {


            return this;
        }
			
    }
HomePage.cs

Steps definitions:

Into the steps definitions we add all the steps classes corresponding to the feature files.

cucumber_steps_definitons

    [Binding]
	public sealed class GoogleSearchSteps
    {
        private Helper _helper;
        private HomePage _homePage;
	
		public GoogleSearchSteps(Helper helper)
        {
            _helper = helper;
        }

		[When(@"When I Search For Something")]
		public void WhenISearchForSomething()
        {
			_homePage = _helper.BasePage.HomePage.SearchSomething();
        }

     


    }
StepsDefinitions

For additional details on SpecFlow step definitions see http://go.specflow.org/doc-stepdef

Hooks:

test-hooks

Inside the Helper class we add the BaseBage object and inside the constructor we pass the driver into the current context.

    public class Helper
    {
        private BasePage _basePage;

        public BasePage BasePage
        {
            get
            {
                if (_basePage == null) _basePage = new BasePage((IWebDriver)ScenarioContext.Current["driver"]);
                return _basePage;
            }
        }
    }
Helper

And finally the Hooks class where we are binding the browser initialisation , and After scenario statement.

    [Binding]
    public sealed class SpecflowHooks
    {
      
        private readonly Browser _browser;

        public SpecflowHooks(Browser browser)
        {
            _browser = browser;
        }

        [BeforeScenario]
        public void BeforeScenario()
        {
            ScenarioContext.Current["driver"]=_browser.LunchBrowser();
        }

        [AfterScenario]
        public void AfterScenario()
        {
            _browser.CloseBrowser();
        }
    }
HooksClass

For additional details on SpecFlow hooks see http://go.specflow.org/doc-hooks  

Packages file:

The packages.config content is just fro testing purpose but this is the basic one that it should give you the start with your project

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Selenium.Support" version="3.0.0" targetFramework="net452" />
  <package id="Selenium.WebDriver" version="3.0.0" targetFramework="net452" />
  <package id="Selenium.WebDriver.ChromeDriver" version="2.24.0.0" targetFramework="net452" />
  <package id="SpecFlow" version="2.1.0" targetFramework="net452" />
  <package id="SpecFlow.MsTest" version="2.1.0" targetFramework="net452" />
</packages>
packages.config

You can find the complete project in our Git repository.

Happy testing!


Like it? Share with your friends!

668
1 comment, 1 share, 668 points

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
0
wtf
geek geek
0
geek
WIN WIN
0
WIN
Love Love
1
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

One Comment

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Story