Pages

Search This Blog

Saturday, May 22, 2010

Selenium Regular Expression and pattern matching

Like locators, patterns are a type of parameter frequently required by Selenese commands. Examples
of commands which require patterns are verifyTextPresent, verifyTitle, verifyAlert, assertConfirmation,
verifyText, and verifyPrompt.Patterns allow one to describe, via the use of special characters, what text is expected rather
than having to specify that text exactly

  • Global Patterns

    * which translates to “match anything,” i.e., nothing, a single character, or many characters.
    [ ] (character class) which translates to “match any single character found inside the square
    brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters
    (which are contiguous in the ASCII character set). A few examples will make the functionality
    of a character class clear:
    [aeiou] matches any lowercase vowel
    [0-9] matches any digit
    [a-zA-Z0-9] matches any alphanumeric character
    In most other contexts, globbing includes a third special character, the ?. However, Selenium globbing
    patterns only support the asterisk and character class.
    To specify a globbing pattern parameter for a Selenese command, one can prefix the pattern with a glob:
    label. However, because globbing patterns are the default, one can also omit the label and specify just
    the pattern itself.
    Below is an example of two commands that use globbing patterns. The actual link text on the page
    being tested was “Film/Television Department”; by using a pattern rather than the exact text, the click
    command will work even if the link text is changed to “Film & Television Department” or “Film and
    Television Department”. The glob pattern’s asterisk will match “anything or nothing” between the word
    “Film” and the word “Television”.
    click link=glob:Film*Television Department
    verifyTitle glob:*Film*Television*
    The actual title of the page reached by clicking on the link was “De Anza Film And Television Department
    - Menu”. By using a pattern rather than the exact text, the verifyTitle will pass as long as
    the two words “Film” and “Television” appear (in that order) anywhere in the page’s title. For example,
    if the page’s owner should shorten the title to just “Film & Television Department,” the test would still
    pass. Using a pattern for both a link and a simple test that the link worked (such as the verifyTitle
    above does) can greatly reduce the maintenance for such test cases.

    Some examples for global pattern

    Lets say your element looks like

    <input class="foo" value="the text value" name="theText">

    verifyEquals("*text*", selenium.getValue("theText"));

    <input type="hidden" value="the hidden value" name="theHidden">

    verifyEquals("* hidden value", selenium.getValue("theHidden"));

    If you have drop down combo box then you can use global pattern matching .

    <select id="theSelect">
    <option id="o1" value="option1">first option</option>
    <option selected="selected" id="o2" value="option2">second option</option>
    <option id="o3" value="option3">third,,option</option>
    </select>

    select the second option and verify like this

    assertEquals("second *", selenium.getSelectedLabel("theSelect"));
    or,
    String[] arr = {"first*", "second*", "third*"};

    verifyEquals(arr, selenium.getSelectOptions("theSelect"));

  • The question mark ? indicates there is zero or one of the preceding element. For example, colo?r matches both "color" and "colour".

    If your element is like
    <input class="foo" value="the text value" name="theText">

    verifyEquals("?oo", selenium.getAttribute("theText@class"));


  • Regular expression
    Regular expression patterns are the most powerful of the three types of patterns that Selenese supports.
    Regular expressions are also supported by most high-level programming languages, many text editors and a host of tools, including the Linux/Unix command-line utilities grep, sed, and awk.

    regexp (wildcard) matching


    <input class="foo" value="the text value" name="theText">

    verifyEquals("regexp:^[a-z ]+$", selenium.getValue("theText"));

    <input type="hidden" value="the hidden value" name="theHidden">

    verifyEquals("regexp:dd", selenium.getValue("theHidden"));


    <select id="theSelect">
    <option id="o1" value="option1">first option</option>
    <option selected="selected" id="o2" value="option2">second option</option>
    <option id="o3" value="option3">third,,option</option>
    </select>

    assertEquals("regexp:second .*", selenium.getSelectedLabel("theSelect"));

  • verify Attributes using regular expression

    <input class="foo" value="the text value" name="theText">

    verifyEquals("regexp:^f", selenium.getAttribute("theText@class"));

    verifyEquals("regex:^[a-z ]+$", selenium.getValue("theText"));

    <select id="theSelect">
    <option id="o1" value="option1">first option</option>
    <option selected="selected" id="o2" value="option2">second option</option>
    <option id="o3" value="option3">third,,option</option>
    </select>

    assertEquals("exact:second option", selenium.getSelectedLabel("theSelect"));

    String[] arr = {"regexp:^first.*?", "second option", "third*"};
    verifyEquals(arr, selenium.getSelectOptions("theSelect"));
  • How to test element is editable in selenium

    We can verify weather an element is editable or not using isEditable selenium command.

    If your element is in below format
    
    <input value="black" name="plain_text">
    
    
    then we can use selenium isEditable to find this element is editable or not
    
    assertTrue(selenium.isEditable("plain_text"));
    
    <select name="plain_select"><option>Niraj</option></select>
    
    assertTrue(selenium.isEditable("plain_select"));
    
    <input disabled="yup" value="black" name="disabled_text">
    
    assertTrue(!selenium.isEditable("disabled_text"));
    
    <select disabled="yup" name="disabled_select"><option>Niraj</option></select>
    
    assertTrue(!selenium.isEditable("disabled_select"));
    
    

    How to test based on attributes of element using CSS

    We can test based on attributes of elements by using css. We can use regular expression in css attributes to identify the element.
    If your element is in this css format.
    
    <div id="css3test"><a name="foobar">foobar</a><a name="barfoo">barfoo</a><a id="foobar" name="foozoobar">foozoobar</a></div>
    
    
  • How to store the text of and element which css attributes start with some specific letters.

    Ex. in above element how to store the text whose element name start with "foo"
    Selenium IDE
    
    
    <tr>
    <td>storeText</td>
    <td>css=a[name^="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name^=\"foo\"]");
    
    
    This will store the "foobar" in str variable because element <a name="foobar">foobar</a> name start with "foo"

  • How to store the text of and element which css attributes ends with some specific letters.

    Ex. in above element how to store the text whose element name ends with "foo"

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name$="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name$=\"foo\"]");
    
    
    This will store the "barfoo" in str variable because element <a name="barfoo">barfoo</a> name ends with "foo"

  • How to store the text of and element which css attributes starts with any letters and ends with any letters but containing some specific letters in middle.

    Ex. in above element how to store the text whose element name ends with any letter and ends with any letter but containing some specific letters "zoo"

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="zoo"]</td>
    <td>str</td>
    </tr>
    
    

    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"zoo\"]");
    
    
    This will store the "foozoobar" in str variable because element <a name="foozoobar">foozoobar</a> name contains "zoo" in the middle.

    There is another method to store
    
    String str = selenium.getText("css=a:contains(\"zoo\")");
    
    

  • How to identify element using more than one css attributes.

    Lets say your element is in this format.
    
    <a alt="foo" class="a2" href="#id2" name="name1" id="id2">this is the <b>second</b> <span selenium:foo="bar">element</span></a>
    
    
    then if you want to store "this is the second element" in a variable called str the

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="name"][alt="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"name\"][alt=\"foo\"]");
    
    
    If you want to store only "element" from above element then use below code
    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="name"][alt="foo"]>span</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"name\"][alt=\"foo\"]>span");
    
    
  • How to test Pseudo-classes : Pseudo-elements and pseudo-classes

    Lets say your element looks like
    
    <div id="structuralPseudo">
    <span>span1</span>
    <span>span2</span>
    <span>span3</span>
    <span>span4</span>
    <div>div1</div>
    <div>div2</div>
    <div>div3</div>
    <div>div4</div>
    </div>
    
    
    to identify the element within the element we will use
    
    css=div#structuralPseudo :nth-child(n)
    
    

    1. To identify first element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(n)");
    
    
    this will store "span1" in str variable.

    2. To identify second element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(2n)");
    
    
    this will store "span2" in str variable.

    3. To identify third element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(3n)");
    
    
    this will store "span3" in str variable.
    ...
    ...
    ...

    8.. To identify the last element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(3n)");
    
    
    this will store "div4" in str variable.


    9. But you can access first and last child directly

    to access first element
    
    String str = selenium.getText("css=div#structuralPseudo :first-child");
    
    
    this will store "span1" in str variable
    To access last element
    
    String str = selenium.getText("css=div#structuralPseudo :last-child");
    
    
    this will store "div4" in str variable
    For more details about css selector please visit .

    http://www.w3.org/TR/CSS2/selector.html

    
    <input type="text" disabled="true" value="disabled" name="disabled">
    
    assertTrue(selenium.isElementPresent("css=input[type=\"text\"]:disabled"));
    
    <input type="checkbox" checked="true" value="checked" name="checked">
    
    assertTrue(selenium.isElementPresent("css=input[type=\"checkbox\"]:checked"));