For something as simple as checking a checkbox you would think that all the different browser drivers would implement this in the same way. But sadly this is not the case. Firefox expects that you click on a checkbox to check or uncheck it whereas both the IEDriver and ChromeDriver expect you to type a space to do the same. This is all well and good for running a simple once off test, but we cant be expected to change code and re-build every time we want to test in a different browser.

Thankfully there is a solution at hand. Once we know which browser is currently running the test, we can decide which method to use. I have provided a C# and a Java code snippet below:

C#
[code]
IWebElement checkbox = driver.FindElement(By.Id(“ElementID”));

if (((RemoteWebDriver)driver).Capabilities.BrowserName == “firefox”)
{
// Firefox
checkbox.Click();
}
else
{
// Chrome and IE
checkbox.SendKeys(Keys.Space);
}
[/code]

Java
[code]
WebElement checkbox = driver.findElement(By.id(“idOfTheElement”);

if (driver.Capabilities.BrowserName.Equals(“firefox”))
{
checkbox.Click();
}
else
{
checkbox.SendKeys(Keys.Space);
}

[/code]

If you come across any other discrepancies between the different driver implementations let us know in the comments.

In Selenium IDE you could simply use getSelectedLabel | locator and that was your currently selected item in the drop down list. Its a bit more of a roundabout way with WedDriver as you can see from the method I have written below:
[code]
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

// …

public string getSelectedLabel(ddlDropListID)
{
string selected;
SelectElement selectOption = new SelectElement(ddlDropListID);
selected = selectOption.SelectedOption.Text;
return selected;
}
[/code]

View Details

In WebDriver there is no direct replacement of the Selenium IDE command selectFrame, however there is a fairly straightforward approximation by using the driver.SwitchTo() method. This method allows you to change the focus to different windows, alerts and frames, but we will be focussing on frames in this post. Below are examples on how to switch to a frame by using its ID, its parent frame, or the top level.

To select the frame by specifying its ID, replace [code] selectFrame | frmFrameID | [/code]
With:
[code]
IWebElement frmDashboard = null;
frmDashboard = driver.FindElement(By.Id(“frmDashboard”));
driver.SwitchTo().Frame(frmDashboard);
[/code]

To select the top level frame, replace [code] selectFrame | relative=top | [/code] with the following line
[code] driver.SwitchTo().DefaultContent();[/code]

And finally to select a frame by the current frame’s parent frame, replace [code]selectFrame | relative=up | [/code] with the following line
[code] driver.SwitchTo().ParentFrame();[/code]

View Details

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.

[code]
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);
}
}
}
[/code]

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:

View Details

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.
[code]
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(“–test-type”,”–start-maximized”);
driver = new ChromeDriver(chromeOptions);
[/code]

Deciding to write tests with WebDriver is only half the battle, you still need to pick a programming language, and possibly a test framework also. Most of the examples that I will be posting will be using C# and NUnit, but for the most part it should still be readable and easy to transfer to your chosen flavour. Many people use the Java programming language and JUnit or TestNG as the test framework.

But the choice is up to you, pick a flavour that you are comfortable with and run with it!

The reason I am starting this blog is to provide information for people who are new to WebDriver. When I started writing tests with WebDriver I found it very difficult to find information for beginners. Just the basics to begin with, how to get a test up and running before adding any of the fancy stuff.

There are plenty of resources out there who advanced practitioners who know what they are doing, but I found very little for those who are starting off at the beginning.

Hopefully this site will rectify that situation and I also hope that you find it a useful resource.