Selenium 2.45 .Net Release Notes

Here are the release notes from the Selenium 2.45 .Net bindings, which were not available online at the time of the release

  • Supports native events for Firefox versions 31 (current ESR), and 24 (immediately previous ESR). Native event support has been discontinued for versions of Firefox later than 33
  • Removed automatic installation of SafariDriver extention for .NET. From this point forward, users are expected to manually install the SafariDriver extension into their Safari installation in order to drive the browser. This is due to Apple’s changes in architecture for Safari extensions.
  • Added initial implementation of .NET bindings OperaDriver. The .NET bindings will now support the Chromium-based Opera driver without requiring the use of the Java remote WebDriver server. This driver will work with Opera 26 and above, and requires the download of the Opera driver executable. Code cleanup and refactoring will take place under a separate commit. Note that there is still no support in the .NET bindings for the Presto-based Opera without using the remote server, nor is there likely to be.
  • Added option to not delete anonymous Firefox profile in .NET. This change adds an option to the .NET FirefoxProfile class so that the driver will not delete the anonymous profile created by the driver. Since the driver cannot and should not use an existing profile in situ because of the multiple instance automation case, this change means that modifications applied to the anonymous profile can be retained and used in future anonymous profiles. The implication is that the user can now make modifications to a profile, and retain those profile modifications (e.g., cookies) into other future profiles, simulating persistent changes over multiple browser launches. Fixes issue #7374.
  • Introduced type safe option in InternetExplorerOptions to set the capability to disable check of mime type of the document when setting cookies. When setting cookies, there is a check in the IE driver to validate that the page in the browser is, in fact, an HTML page. Despite the fact that omitting this check can cause unrecoverable crashes in the driver, there is demand for a mechanism to disable this check for older, legacy versions of Internet Explorer. Fixes issue #1227.

Don’t forget you can download Selenium from SeleniumHQ.org

Advertisements

Getting Browser Details in WebDriver

While not a major requirement getting the browser name and version could come in useful in test reports. Use the following code snippet to get the browser name and version:

browser

ICapabilities cap = ((RemoteWebDriver)driver).Capabilities;
string strBrowserName = cap.BrowserName;
string strBrowserVersion = cap.Version;
            
Console.WriteLine("Testing on Browser {0} v{1}", strBrowserName, strBrowserVersion);

Checking a checkbox with WebDriver

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#

IWebElement checkbox = driver.FindElement(By.Id("ElementID"));

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

Java

WebElement checkbox = driver.findElement(By.id("idOfTheElement");

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

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

How to select a value in a drop down list

This example uses C# language to illustrate the point, I will provide a Java version at a later stage.
To use drop down selects in WebDriver you have to define the SELECT object as well as the OPTION object. This may be due to the way these are coded in HTML (This example is taken from http://www.w3schools.com/tags/tag_select.asp with an added Id attribute in the <SELECT> tag ):

<select id="cartypes">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

So in WebDriver we have to define the SELECT element first, and then using this object we define the OPTION object. Once we have out option item defined we can select it by using the displayed text, its index or its value. There are separate methods for each of these; SelectByText(), SelectByIndex() and SelectByValue() respectively. Each of the the SelectBy examples below are selecting the same item “Mercedes” in the car types drop down list:

//SELECT element defined
var ddlCarTypes = driver.FindElement(By.Id("cartypes"));

// OPTION element defined in the car types drop down list
var selectOption = new SelectElement(ddlCarTypes);

// Select the same item in the list using Text, Index or Value:
selectOption.SelectByText("Mercedes");
selectOption.SelectByIndex(2);
selectOption.SelectByValue("mercedes");

Getting the selected item from a drop down list

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:

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;
}

Continue reading

Switching Frames in WebDriver

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

 selectFrame | frmFrameID | 

With:

IWebElement frmDashboard = null;
frmDashboard = driver.FindElement(By.Id("frmDashboard"));
driver.SwitchTo().Frame(frmDashboard);

To select the top level frame, replace

 selectFrame | relative=top | 

with the following line

 driver.SwitchTo().DefaultContent();

And finally to select a frame by the current frame’s parent frame, replace

selectFrame | relative=up | 

with the following line

 driver.SwitchTo().ParentFrame();

Continue reading

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