Thursday, August 13, 2015

Lesson #18 -Selenium WebDriver – Design Pattern for Web Page Testing(C#)


Overview

This article is relevant to any automation framework that you choose to work with, And Selenium is a great example to demonstrate how to design your Web Page tests in the real world. 

First, until now I demonstrate multiple examples for a specific Web Page, that’s great, but what happened if we need to test a Web Application that has 100 different pages? Do we want to replicate our code for every element that we want to access? The answer is NO. 

You  should understand you can design your Web tests in the same way that you do it for any other application that involve an object oriented patterns (Classes, Object, Properties Etc.) and the benefits that come when using it (Efficient Code, Clear Structure, Easy to maintain and reduce code replication).

Page Object Model (POM)

As I explained at the beginning of this article, POM has many benefits, but the main thing that you need to remember is that without the ability to use it, you probably cannot work in the software industry has an automation engineer, POM is a general approach and doesn’t belong specifically to selenium.

The main idea that you need to understand is that when using the POM approach, we will start to see a more advanced and complex code, this code will include Classes, Layers and inheritance, and therefore you should pay attention and understand the logic behind the code examples.

The practical side

It’s now the time to use the POM approach in our code design, in my example I will use my blog, and demonstrate how to write a code that each page is reflected with a corresponding class that has a unique attributes and methods. 

Class No’1: About Page

class About_Page
    {
        //Creating a static web element that will be used by all methods (Default value is "Null")

        public static IWebElement General_Element_for_All_Functions = null;

        //Define a list of web Elements that we will use in our code


        //Syntax Validation (Main Grid Element)
        public static IWebElement Main_Grid_Text_Validations(IWebDriver FirefoxDriver)
        {
            General_Element_for_All_Functions = FirefoxDriver.FindElement(By.ClassName("post-outer"));
            return General_Element_for_All_Functions;
        }
    }

Class No’2: Home Page

class POM_Home_Page
    {

        //Creating a static web element that will be used by all methods (Default value is "Null")

        public static IWebElement General_Element_for_All_Functions = null;

        //Define a list of web Elements that we will use in our code

        //SearchBox - Text Field
        public static IWebElement SearchField(IWebDriver FirefoxDriver)
        {
            General_Element_for_All_Functions = FirefoxDriver.FindElement(By.XPath("//input[@autocomplete='off']"));       
            return General_Element_for_All_Functions;
        }

        //SearchBox - Seach Button
        public static IWebElement Seach_Button(IWebDriver FirefoxDriver)
        {
            General_Element_for_All_Functions = FirefoxDriver.FindElement(By.XPath("//input[@class='gsc-search-button']"));
            return General_Element_for_All_Functions;
        }

        //Blog Main Title
        public static IWebElement Main_Title_Verification(IWebDriver FirefoxDriver)
        {
            General_Element_for_All_Functions = FirefoxDriver.FindElement(By.XPath("//h1[@class='title']"));
            return General_Element_for_All_Functions;
        }

        //About Page
        public static IWebElement About_Page(IWebDriver FirefoxDriver)
        {
            General_Element_for_All_Functions = FirefoxDriver.FindElement(By.XPath("//a[contains(.,'About ')]"));
            return General_Element_for_All_Functions;
        }
    }

Main Code:

[TestMethod]
        public void POM_Example()
        {

            //Preperations :
            IWebDriver FirefoxInstance = new FirefoxDriver();
            FirefoxInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(100));
            FirefoxInstance.Navigate().GoToUrl("http://www.dtvisiontech.com");

            //Calling a method from the "POM_Home_Page" Class :
            POM_Home_Page.SearchField(FirefoxInstance).SendKeys("Search Query");
            POM_Home_Page.Seach_Button(FirefoxInstance).Click();
           

            //Calling a method from the "About_Page" Class :
            POM_Home_Page.About_Page(FirefoxInstance).Click();

            if (About_Page.Main_Grid_Text_Validations(FirefoxInstance).Text.Contains("daug333hter")==true)
            {
                DebuggClass.PrintToFile("Test Pass");  
            }
            else
            {
                DebuggClass.PrintToFile("Test Failed");  
            }
        }

1 comment:

  1. Nice blog of selenium Web Driver with c# language .I am doing with java .This blog is easy to understand & for beginners its good.Please share more blog in Selenium web Driver.

    ReplyDelete

My Presentations