Foliotek Development Blog

Simplifying C# Selenium 2 Tests for ASP.NET WebForms

by John Pasquet

Our company uses Selenium to automate regression testing of important functionality on our products. One of the products uses ASP.NET Web Forms. Since the ID for controls gets changed from “txtName” to something like “ct100$contentMain$txtName”, we were using the xPath methods (ClickByXPath(), sendKeysByXPath(), anyByXPath(), etc).

The code looked like this:

Previous Test Code



Tester.Driver.clickByXPath("//input[contains(@id, 'btnSearch')]");
Tester.Driver.clickByXPath("//select[contains(@id, 'drpPrograms')]/option[contains(text(), '" + programName + "')]");
Tester.Driver.clickByXPath("//input[contains(@id, 'rblOption_2')]");
Tester.Driver.clickByXPath("//input[contains(@id, 'cbOption_1')]");
Tester.Driver.sendKeysByXPath("//input[contains(@id, 'txtName')]", name);

Tester.Driver.clickByLinkText("Next");

Writing xPath may be enjoyable for some, but for me it got a bit tedious. So, I created some wrapper methods to make this a lot easier to create and read.

New Test Code



ClickButton("btnSearch");
SelectDropDownOption("drpPrograms", optionName);
SelectRadio("rblOption_2");
SelectCheckBox("cbOption_1");
EnterTextBox("txtName", name);


Wrapper Methods



/// Select a RadioButton or an option on a RadioButtonList.
/// If optionText is specified for a RadioButtonList, this will select that option.  Otherwise it works for native RadioButtons
/// prefix = html filter e.g.  "p/" or "tr/td/"
public void SelectRadio(string radioButtonID, string optionText = "", string prefix = "")
{
    string path = "//" + prefix + "input[contains(@id, '" + radioButtonID + "')]";

    if (optionText != "")
        path += "/option[contains(text(), '" + optionText + "')]";

    ClickByXPath(path);
}

/// Select a CheckBox
/// prefix = html filter e.g.  "p/" or "tr/td/"
public void SelectCheckbox(string checkboxID, string prefix = "")
{
    string path = "//" + prefix + "input[contains(@id, '" + checkboxID + "')]";

    ClickByXPath(path);
}

/// Select an option on a DropDownList
/// prefix = html filter e.g.  "p/" or "tr/td/"
public void SelectDropDownOption(string dropdownID, string optionText = "", string prefix = "")
{
    string path = "//" + prefix + "select[contains(@id, '" + dropdownID + "')]";

    if (optionText != "")
        path += "/option[contains(text(), '" + optionText + "')]";

    ClickByXPath(path);
}

/// Enter text into a TextBox and optionally clear it out first.
public void EnterTextBox(string txtName, string value, bool clearField = false)
{
    Tester.Driver.sendKeysByXPath("//input[contains(@id,'" + txtName + "')]", value, clearField);
}

/// Confirm if a condition is true.  If it is, output the confirmationMessage.
public void ConfirmTrue(bool test, string confirmationMessage)
{
    Tester.Assert(test, confirmationMessage);
}

/// Test to see if a control exists on the page.
/// Control type = input, div, td or whatever
public bool ControlExists(string controlType, string controlID)
{
    string path = "//" + controlType + "[contains(@id,'" + controlID + "')]";
    return Tester.Driver.anyByXPath(path);
}

/// Click a Button
public void ClickButton(string buttonName, string prefix = "")
{
    string path = "//" + prefix + "input[contains(@id, '" + buttonName + "')]";

    ClickByXPath(path);
}

public void ClickByXPath(string xPath)
{
    Tester.Driver.ClickByXPath(xPath);
}

public void ClickByLinkText(string linkText)
{
    Tester.Driver.clickByLinkText(linkText);
}


For more posts, see Tips and Tricks or Functional Regression Testing

Tags: , , , , ,

« »

Leave a Reply