Thursday, June 23, 2011

Selenium - How to find string exists in the browser url or not ??





/**********************************************************
     Function Name      : LocationTextPresent
     Arguments             : text
     Description             : This function checks whether text is present in URL or not.
     Author                    : Sara ,Bangalore
/*********************************************************/
Selenium.prototype.getLocationTextPresent = function(text) {

     var element = this.browserbot.getCurrentWindow().location;

             var patternMatcher = new PatternMatcher(text);
    if (patternMatcher.strategy == PatternMatcher.strategies.glob) {
            if (text.indexOf("glob:")==0) {
                    text = text.substring("glob:".length); // strip off "glob:"
                }
        patternMatcher.matcher = new PatternMatcher.strategies.globContains(text);
    }
    else if (patternMatcher.strategy == PatternMatcher.strategies.exact) {
                text = text.substring("exact:".length); // strip off "exact:"
        return element.indexOf(text) != -1;
    }
    return patternMatcher.matches(element);
};

<tr>
            <td>verifyLocationTextPresent</td>
            <td>ruby</td>
            <td>true</td>
</tr>

Ex: 


<tr>
            <td>verifyLocationTextPresent</td>
            <td>sara</td>
            <td>true</td>
</tr>

Friday, June 17, 2011

Selenium IDE - How to read table & append the table content into a single variable


It is simple.  Try the below code:

 1. This function locates the table in a html page.
 2. Finds no. of rows in a table.
 3. Reads each row data & appends into single variable.
 4. Returns the variable.



Selenium.prototype.getSaraTableAppend = function(tableName) {

try
{

    var table = this.browserbot.findElement(tableName);
             var rows = table.rows.length;
  
                         var apptxt = 0;

                for (var i = 2; i < rows ; i++)
                {
                                                               
                        var celltxt  = getText(table.rows[i].cells[0]);
                        apptxt = apptxt + celltxt;
                                                                       
                }
               
               return apptxt;

}
catch (e)
{
        throw new SeleniumError("Threw an exception!" + e.message);
}

};

 Then call this function in Selenium IDE as,

<tr>
            <td>storeSaraTableAppend</td>
            <td>css=table</td>
            <td>Result</td>
</tr>



Ex:

  Table is as below :
123
456
789
Abc
efg

Results contains data as: 0123456789Abcefg

Selenium IDE - How to Read table content & store as a Array


It is simple.  Try the below code:

1. This function locates the table in a html page.
2. Finds no. of rows in a table.
3. Reads each row data & stores in an array.
4. Returns the data in an array.

Selenium.prototype.getSaraTable = function(tableName) {
try
{
    var table = this.browserbot.findElement(tableName);
            var rows = table.rows.length;
            var TblArr = [];
                for (var i = 0; i < rows ; i++)
                {
                        var celltxt  = getText(table.rows[i].cells[0]);
                        TblArr.push(celltxt);
            }
                return TblArr;
}
catch (e)
{
        throw new SeleniumError("Threw an exception!" + e.message);
}

};


 Then call this function in Selenium IDE as,


<tr>
            <td>storeSaraTable</td>
            <td>css=table</td>
            <td>Result</td>
</tr>


Ex:

  Table is as below :

123
456
789
Abc
efg






           Results contains data as: 123,456,789,Abc,efg



Thursday, June 16, 2011

Useful functions / Helpful Question & Answers about Selenium

How to do this in Selenium ??

Find below some of the useful functions or Q & A:



1. How to clear or delete the cookies -  >> more 

2. Selenium IDE - How to Read table content & store as a Array - >> more

3. Selenium IDE - How to read table & append the table content into a single variable - >> more 

4. Selenium - How to find a given string or text is present in URL or not - >> more