Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Writing a custom accessor

Writing a custom accessor

Writing a custom accessor

There are three "kinds" of Selenium objects... Actions (click is a good example), Accessors (this is a good example) and Locators (xpath= is a good example).

You can extend all three of them... and end up by seriously cutting down the code you have to write in the tests (making it very englishy) and also making it easily configurable (and reusable).

I was recently asked how you can write a custom accessor. The following is a simple accessor (complete with supporting JSDoc - that is parsed by the Selenium IDE and shows as help).


 * This accessor expects a table row to be passed in as a locator, and compares
 * the contents of the first table cell of that row with the data passed in as
 * l_sExpectedData.
 * @param {string} l_sLocator is a valid xpath or locator to a table tr node.
 * @param {string} l_sExpectedData is the data to compare against.
 * @type {boolean}
 * @return True if the data matches, otherwise False.
Selenium.prototype.getFormRow(l_sLocator, l_sExpectedData)
    var l_bSuccess = false;
    var l_oFoundNode = this.page().findElement(l_sLocator);
    if (l_oFoundNode)
        var l_pRowCells = l_oFoundNode.getElementsByTagName('td');
        if (l_pRowCells.length > 0)
            var l_sActualData = l_pRowCells[0].innerHTML;
            if (l_sActualData == l_sExpectedData)
                l_bSuccess = true;
    return l_bSuccess;

Call this as (for example):
 > verifyFormRow
 > xpath=/::descendant/tr[1]
 > Some text

You can also use assertFormRow (and I think verifyNotFormRow and assertNotFormRow - but these may not work in the IDE).

If a method is created with getSomething(), then it will work with assertSomething() as well as verifySomething(). Using the string verify in the method name could then prove problematic (in terms of readability at least).

I found that once you start down the road of doing all this customisation, the IDE ceases to be of much use - since it only supports a subset of what you can do. I found I switched to using the Selenium Core and hand crafted the Selenium scripts in a text editor.


Jeff's Blog @ CodeRambler

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094: What is Javascript?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close