Thursday, October 18, 2018

Start Test Automation with MAQS Open Framework

We have discussed how to setup Visual Studio to work with MAQS (https://github.com/Magenic/MAQS) open framework in a previous post.(Functional Test Automation with MAQS Open Framework).Within this post let’s write a simple test using the MAQS.
First Create a MAQS project as described in previous post.You know one of the main advantages of MAQS framework is we do not need to spend time, to do package installation and configurations.After creating a project, you can find all required drivers and other packages are installed.You will only have to change configuration values according to your project requirements before start scripting.There would be an App.config file under Test project once the project is created for this purpose.
Page Object Model is used in the project created with MAQS.Page Object Model is an object design pattern where web pages are represented as classes. The elements of the page are represented as variables in the class.

Let’s try to write test script to test following scenario.

Scenario: Navigate to google search page And search for “Selenium”.Verify the search state and move to the search results page.Then move back to google search page and Click on “ I’m Feeling Luckey” button and verify doodle page is displayed.
In above scenario, we are trying to test google search page buttons behavior.
So, we need to provide the google search page URL value in the configuration file. .Let’s change configuration file values as shown in the following image
config

In the above scenario we identify three web pages to navigate by executing the test.We need to create three new classes to represent each web page
  • GoogleSearchPage.cs
  • SearchResultPage.cs
  • GoogleDoodlePage.cs
Let’s add new classes to PageModel.To add new class, right click on PageModel and select Add—>NewItem
Select Magenic’s Open Test and select Maqs Selenium Page Model Class.Give Name as “GoogleSearchPage” and click on Add.
Addclass
You can check the solution explorer and see newly added class is available in the PageModel folder.
Follow the same steps as above and add two more classes for SearchResultPage and GoogleDoodlePage.
You can find sample code of each page as follows.

GoogleSearchPage.cs
Following are the methods inside this class
  • IsPageLoaded()-Check search field is displayed to verify page is loaded
  • NavigateToSearchPage()-Navigate to google search page
  • PerformSearch()-Enter text and search
  • ClickOnFeelingLuckeyButton()-Click on feeling lucky button
using Magenic.Maqs.BaseSeleniumTest;
using Magenic.Maqs.BaseSeleniumTest.Extensions;
using Magenic.Maqs.Utilities.Helper;
using OpenQA.Selenium;

namespace PageModel
{
    /// <summary>
    /// Page object for GoogleSearchPage
    /// </summary>
    public class GoogleSearchPage
    {
        /// <summary>
        /// The page url
        /// </summary>
        private static readonly string PageUrl = SeleniumConfig.GetWebSiteBase();

        /// <summary>
        /// Selenium test object
        /// </summary>
        private SeleniumTestObject testObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="GoogleSearchPage" /> class.
        /// </summary>
        /// <param name="testObject">The selenium test object</param>
        public GoogleSearchPage(SeleniumTestObject testObject)
        {
            this.testObject = testObject;
        }

        /// <summary>
        /// Gets the google search text field element
        /// </summary>
        private LazyElement SearchTextField
        {
            get { return new LazyElement(this.testObject, By.CssSelector("#lst-ib"), "Google search field"); }
        }

        /// <summary>
        /// Gets the google search button element
        /// </summary>
        private LazyElement SearchButton
        {
            get { return new LazyElement(this.testObject, By.Name("btnK"), "Google search button"); }
        }

        /// <summary>
        /// Gets the google feeling luckey button element
        /// </summary>
        private LazyElement FeelingLuckeyButton
        {
            get { return new LazyElement(this.testObject, By.Name("btnI"), "Google feeling luckey button"); }
        }
        /// <summary>
        /// Check if the page has been loaded
        /// </summary>
        /// <returns>True if the page was loaded</returns>
        public bool IsPageLoaded()
        {
            return this.SearchTextField.Displayed;
        }

        ///<summary>
        ///Launch Driver and navigate to google search page
        /// </summary>
        public void NavigateToSearchPage()
        {
            this.testObject.WebDriver.Navigate().GoToUrl(PageUrl);
        }

        ///<summary>
        ///Enter value and search
        /// </summary>
        public void PerformSearch(string value)
        {
            this.SearchTextField.SendKeys(value);
            this.testObject.WebDriver.Wait().ForClickableElement(By.XPath("//div[@class='sbsb_a']/ul/li[1]")).Click();
        }

        ///<summary>
        ///Click on I'm Feeling Luckey button
        /// </summary>
        public void ClickOnFeelingLuckeyButton()
        {
            this.FeelingLuckeyButton.Click();
        }
    }

}
SearchResultsPage.cs

This class methods are,
  • IsPageLoaded()-Check google logo displayed to verify page is loaded
  • ClickonGoogleIcon()-Click on google logo to navigate back to search page
using Magenic.Maqs.BaseSeleniumTest;
using Magenic.Maqs.BaseSeleniumTest.Extensions;
using Magenic.Maqs.Utilities.Helper;
using OpenQA.Selenium;

namespace PageModel
{
    /// <summary>
    /// Page object for SearchResultPage
    /// </summary>
    public class SearchResultPage
    {
        /// <summary>
        /// The page url
        /// </summary>
        private static readonly string PageUrl = SeleniumConfig.GetWebSiteBase() + "PAGE.html";

        /// <summary>
        /// Selenium test object
        /// </summary>
        private SeleniumTestObject testObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="SearchResultPage" /> class.
        /// </summary>
        /// <param name="testObject">The selenium test object</param>
        public SearchResultPage(SeleniumTestObject testObject)
        {
            this.testObject = testObject;
        }

        /// <summary>
        /// Gets the google logo element
        /// </summary>
        private LazyElement GoogleLogo
        {
            get { return new LazyElement(this.testObject, By.CssSelector("#logo"), "Google logo"); }
        }

        /// <summary>
        /// Check if the page has been loaded
        /// </summary>
        /// <returns>True if the page was loaded</returns>
        public bool IsPageLoaded()
        {
            return this.GoogleLogo.Displayed;
        }

        /// <summary>
        /// Click on google logo
        /// </summary>
        public void ClickonGoogleIcon()
        {
            this.GoogleLogo.Click();
        }
    }

}

GoogleDoodlePage.cs
This class methods are,
  • IsPageLoaded()-Check Doodle logo displayed to verify page has loaded
  • VerifyPageTitle()-Verify page title
using Magenic.Maqs.BaseSeleniumTest;
using Magenic.Maqs.BaseSeleniumTest.Extensions;
using Magenic.Maqs.Utilities.Helper;
using OpenQA.Selenium;

namespace PageModel
{
    /// <summary>
    /// Page object for GoogleDoodlePage
    /// </summary>
    public class GoogleDoodlePage
    {
        /// <summary>
        /// The page url
        /// </summary>
        private static readonly string PageUrl = SeleniumConfig.GetWebSiteBase() + "/doodles/";

        /// <summary>
        /// Selenium test object
        /// </summary>
        private SeleniumTestObject testObject;

        /// <summary>
        /// Initializes a new instance of the <see cref="GoogleDoodlePage" /> class.
        /// </summary>
        /// <param name="testObject">The selenium test object</param>
        public GoogleDoodlePage(SeleniumTestObject testObject)
        {
            this.testObject = testObject;
        }

        /// <summary>
        /// Gets the doodle element
        /// </summary>
        private LazyElement DoodleLogo
        {
            get { return new LazyElement(this.testObject, By.CssSelector("#logo"), "Doodle page logo"); }
        }

        /// <summary>
        /// Check if the page has been loaded
        /// </summary>
        /// <returns>True if the page was loaded</returns>
        public bool IsPageLoaded()
        {
            return this.DoodleLogo.Displayed;      
        }

        /// <summary>
        /// Verify doodle page has been loaded
        /// </summary>
        /// <returns>True if the doodle page was loaded</returns>
        public bool VerifyPageTitle()
        {
           return  this.DoodleLogo.GetAttribute("title").Equals("Google Doodles");         
        }
    }

}
As we have added all necessary Page Model classes, we can add test class to execute the test.To add a test class ,right click on Tests and select Add—>NewItem
Select Magenic’s Open Test and select Maqs Seleium VS Test Class.Give a suitable name and click on Add.
testclass
After you have added the test class your solution explorer should look similar to the following image.
solutionexplorer
As the next step,we can write the test method in the  DemoTest.cs

DemoTest.cs


using Magenic.Maqs.BaseSeleniumTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PageModel;

// TODO: Add reference to object model
// using PageModel;

namespace Tests
{
    /// <summary>
    /// DemoTest test class
    /// </summary>
    [TestClass]
    public class DemoTest : BaseSeleniumTest
    {
        /// <summary>
        /// GoogleSearchPage
        /// </summary>
        [TestMethod]
        public void VerifyGoogleSearchPageButtons()
        {
            GoogleSearchPage googleSearchPage = new GoogleSearchPage(this.TestObject);
            googleSearchPage.NavigateToSearchPage();
            googleSearchPage.PerformSearch("Selenium");
            SearchResultPage searchResultPage = new SearchResultPage(this.TestObject);
            Assert.AreEqual(true,searchResultPage.IsPageLoaded());
            searchResultPage.ClickonGoogleIcon();
            Assert.AreEqual(true,googleSearchPage.IsPageLoaded());
            googleSearchPage.ClickOnFeelingLuckeyButton();
            GoogleDoodlePage googleDoodlePage = new GoogleDoodlePage(this.TestObject);
            Assert.AreEqual(true,googleDoodlePage.IsPageLoaded());
            Assert.AreEqual(true, googleDoodlePage.VerifyPageTitle());
        }
    }
}
Rebuild the project and open test explorer window in Visual Studio.You would be able to see the test method in the test explorer window.We can execute test by right click on test method name and select Run Selected Tests.
TestExplorer
When Run Selected Tests is clicked, chrome browser will open and execute the selected test method

2 comments:

  1. Hi, thanks for sharing.
    Do you know how to handle Windows Login (NTLM) using MAQS framework?

    thanks.

    ReplyDelete
    Replies
    1. I have done this a couple of ways.
      The easy way: AutoIt
      The hard, but self contained way: Open a section of the page that doesn't launch NTLM and get your token (jsessionid I think). Than use web services (something available in the enterprise version of MAQS) to get the token authenticated.

      Delete