Page Object Model

Page Objects are a way to encapsulate the technical details of a web page and the services it provides so that the developer of a test does not need to delve into the structure of the webpage. Essentially the Page Object acts as an interface between the web page and the testing of that page. The Page Object defines all the elements and actions provided by the web page and the test case uses these to execute the test(s). By clearly delineating the test code from the page objects, you will be able to use the same page objects in a variety of tests cases and achieve code re-use. Another major positive to this approach is that if the application being tested changes then only the Page Object definition needs to be changed in one place and all of the existing test should still work once that change had been made.

I will provide tangible examples later but I used C# to define the Page Object and its properties and methods. The test cases use these objects and methods with NUnit to provide Assertions. This is an important point; the tests, and not the Page Object, should be responsible for making assertions about the state of a page. The Page Object defines the properties and methods you need for creating a test and the test itself uses those properties and methods to make Assertions, allowing us to determine if a test has passed or failed.

The Code

I think the best way to learn a new concept is by getting your hands dirty and trying it out. In this example I will show how to use the Page Object Model to define a Login Page.
Below I will create a page object for Login and I will define some properties (username, password and login button) and a method to login. As the LoginUser() method will take us to a new page, it will return a HomePage object.

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using NUnit.Framework;

namespace PageObjectModel
{
  public class LoginPage
  {
      private IWebDriver driver;
   
      [FindsBy(How = How.Id, Using = "tbxUsername")]
      private IWebElement tbxUsername;

      [FindsBy(How = How.Id, Using = "tbxPassword")]
      private IWebElement tbxPassword;

      [FindsBy(How = How.Id, Using = "btnLogin")]
      private IWebElement btnLogin;

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

      public HomePage UserLogin(string user, string pass)
      {
        tbxUsername.SendKeys(user);
        tbxPassword.SendKeys(pass);
        btnLogin.Click();

        return new HomePage(driver);
      }
  }
}

So that’s a very simple Page Object setup, I’ll go through it in more details once we have the code for the test case that uses it:

Continue reading

Advertisements

How to execute JavaScript in your C# tests

In WebDriver you can call javascript functions programatically without having to click on a link with javascript code as the href attribute. This functionality uses the IJavaScriptExecutor interface. This allows you to execute javascript commands on your browser from your Selenium Webdriver test. Here is a short example to get the current URL:

IWebDriver driver;
// ... ... ... 

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string url = (string)js.ExecuteScript("return document.url");

Or you can use functions or methods to be called also

IWebDriver driver;
// ... ... ... 

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("OpenModalWindow('testfile.html')");

How to MouseOver in your C# tests

I have created a fairly simple to use C# method to perform a MouseOver operation, all you need to do is to specify the web element to MouseOver.

Add the following using statement first:

using OpenQA.Selenium.Interactions;

Then create the following method:

public static void MouseOver(IWebElement theElement)
{
	Actions builder = new Actions(driver);
	builder.MoveToElement(theElement).Build().Perform();
	Thread.Sleep(1500);
}

Its fairly easy to use and I have a sample of how to call it below:

IWebElement theMenuLink = driver.FindElement(By.Id("menu"));
MouseOver(theMenuLink);

Using ChromeDriver Options

There are a number of different ChromeDriver options (and for the other browsers too) that allow us to control how the browser starts up. In this example we see how Chrome is started when no options are selected:
ChromeDriver

Using the following options we can specify that Chrome start without the warning message and that it starts maximised.

 var chromeOptions = new ChromeOptions();
 chromeOptions.AddArguments("--test-type","--start-maximized");
 driver = new ChromeDriver(chromeOptions);