Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts

Tuesday, November 13, 2007

Selenium popup windows

Selecting a popup window

The selectWindow method actually supports selecting by the title even if it has accents.  If you have more than one popup window to deal with the same name, I guess you need to write some JavaScript to get around it.

Closing a popup window

Although there is a "close" command in Selenium, it does not appear to work within Internet Explorer.  So the work around to this is to use

runscript window.close()

Sunday, September 23, 2007

Generating a random SIN

The Canadian Social Insurance Number is something that would validate against the Luhn algorithm. Since SINs may be used as input data for some applications, I have translated the code that generates a number that passes the Luhn Algorithm in Wikipedia to something that can be put into into Selenium's user-extensions.js.

Selenium.prototype.getRandomSIN = function() {
    var length = 9;
    var digits = new Array()
    for(var i = 0; i < length - 1; ++i) {
        digits[i] = Math.floor(Math.random() * 9)
    }
    var sum = 0;
    var alt = true;
    for(var i = length - 2; i >= 0; --i) {
        if (alt) {
            var temp = digits[i];
            temp *= 2;
            if (temp > 9) {
                temp -= 9;
            }
            sum += temp;
        } else {
            sum += digits[i];
        }
        alt = !alt;
    }
    var modulo = sum % 10;
    if(modulo > 0) {
        digits[length-1] = 10 - modulo;
    }
    return digits.join("");
};

Wednesday, July 25, 2007

Finding the nth link in Selenium

One of the common things I do when testing is clicking on a link but it is not the first one with the text. Selenium only provides link= and if the recorder detects that you are not choosing the first link, it will put in an XPath expression that would not work if your links pass IDs (like in Curam). Fortunately though the extension system they provided we can add our own locators.

So I created a locator that allows me to put in links=Select[2] to choose the 3rd instance of the link Select (since it is zero base).

The code is pretty simple (I am testing the Insert Code plug-in for Windows Live Writer):

    PageBot.prototype.locateElementByLinks=
    function(text, inDocument) {
      var parsed = text.match(/^(.+)\[(\d+)\]$/)
      var textPortion = parsed[1]
      var index = parsed[2]
      var links =
        inDocument.getElementsByTagName("a");
      var c = 0;
      for (var i = 0; i < links.length; i++) {
        var text = links[i].innerText || links[i].text
        if (text == textPortion) {
          if (c == index) {
            return testElement
          }
          ++c
        }
      }
      return null;
    };

I made a slight modification to support Internet Explorer which does not support the "text" property.

Monday, June 25, 2007

Selenium Curam Logon

In order to make it easier for developers to write their Selenium tests, I created an extension to user-extensions.js in Selenium to do the logon for them if needed or go directly to the application home page. I determined that you are on runtime (hence logon is required) if you are in HTTPS. Anyway here is the code:

/**
 * doCuramLogon
 * @param username
 *    user name to logon as.  Defaults to "superuser"
 * @param pageID
 *    page ID to go to.  Defaults to "Application_home"
 */
Selenium.prototype.doCuramLogon =
  function(username, pageID) {
    if (!username) {
      username = "superuser";
    }
    if (!pageID) {
      pageID = "Application_home";
    }
    if (this.page().getCurrentWindow().protocol == 
        "https:") {
      this.doOpen("/Curam/j_security_check?" +
                  "j_username=" + username +
                  "&j_password=password");
    } else {
      this.doOpen("/Curam/en/" + pageID +
                  "Page.do");
    }
  };
The toughest part was figuring out how to do the logon. After Googling a bit I hit this page which specifies in a nutshell that Selenium user extensions cannot perform methods that would span more than one page. So this.doOpen would not pass the context correctly to the next commands. Hence j_username cannot be found whenever I do a doOpen(). The next tough part was trying to get this thing working in Selenium IDE which returns the location.protocol as "chrome:" One note, when using this, you have to specify curamLogonAndWait as the command to make sure it waits for the page to load.

Sunday, January 21, 2007

Selenium Lunch and Learn

I haven't done a lunch and learn in ages now. Wonder if I can do this on tomorrow teaching about Selenium. Well rather than looking at a blank PowerPoint document, I thought it might be better to hash out my thoughts in a blog entry and transfer them over to PowerPoint later so here goes. Selenium is a tool that can playback a script of commands directly on the browser. Selenium's core functionality is similar to that Rational Robot. The advantage for the test team is quite obvious, easy regressions through automated testing. For development and training, we can use this to help us automate the creation of our test cases that we lose we release a new build. A simple script would look like this...

open/logon.jsp
typej_usernamesuperuser
typej_passwordpassword
submitAndWait//form
This script performs a logon on the system. Show demo here Show to to record the simple script using Selenium IDE that does up to case creation. Show how to modify a script to have random numbers and put in the value as part of the client name. Show a full demo of with all the cases.

Thursday, December 07, 2006

Storing the current date in Selenium scripts

In my application I needed to put in the current date and it has to be in a specific format (yyyy-MMM-d) so taking the information I gathered from openacs.org/xowiki/en/Testing_with_Selenium I put in the following near the beginning of my Selenium script. The cool part here being the way I created an anonymous array that stores the text. Note the text between {} should all be in one line but is broken apart for clarity (and to make it fit in the window).

storeExpression
javascript{
var d = new Date();
d.getFullYear() + "-" +
[ "Jan", "Feb", "Mar",
  "Apr", "May", "Jun",
  "Jul", "Aug", "Sep",
  "Oct", "Nov", "Dec"]
[d.getMonth()] +
"-" + d.getDate()
}
today
And then to type it in a field.
type
__o3fwp.ACTION.dtls$dtls$acStaffingDate
${today}