Friday 21 June 2013

Adding Codes to an Existing JavaScript Function

Below is a script to add codes (prepend or append) to an existing JavaScript function:
myFunction = (function () {
 // check whether myFunction already exists
 if (typeof myFunction == 'function') {
  var existingFunction = myFunction;
 }
 return function () {
  // some codes before existing function
  // . . .
 
  if (typeof existingFunction != 'undefined') {
   existingFunction.apply(this, arguments); 
   // by using 'apply' we can pass one or more arguments that is initially passed to the original function
  }
  
  // some codes after existing function
  // . . .
 };
} ());  

Friday 7 June 2013

Some WatiN Examples

Below are some examples of WatiN codes for testing:
// find div by id
var div = browser.Div("divId");

// find div by class
var div = browser.Div(Find.ByClass("divClass"));

// click a div
browser.Div("divId").Click();

// click a button
browser.Button("buttonId").Click();

// get <li></li> elements inside a div
var lisCollection= browser.Div("divId").ElementsWithTag("li");
// click the first <li></li>
lisCollection.FirstOrDefault().Click();
// click the one contains a particular inner text
lisCollection.Where(li => li.Text.Contains("some text")).FirstOrDefault().Click();

// get <a></a> elements inside a div
var links = browser.Links.Filter(a => a.Parent.Parent.Parent.ClassName == "divClass");
var links = browser.Links.Filter(a => a.Parent.Parent.Parent.Parent.Id == "divId");

// select from a select dropdown
browser.SelectList("selectId").Option("option text").Select();

// assert a div exists
Assert.IsTrue(browser.Div("divId").Exists);

// assert header(s) with a particular text exists
Assert.IsTrue(browser.ElementsWithTag("h1").Any(x => x.Text == "heading text"));

// assert only one header with a particular text exists
Assert.IsTrue(browser.ElementsWithTag("h3").Where(x => x.Text == "heading text").Count() == 1);

// assert a particular text exists inside a table td 
Assert.IsTrue(browser.Table("tableId").TableCells.Any(x => x.Text == "text inside td"));