Global GUI map for automation with VS.NET

Writing test code based on RanoreXPath is not a big challenge. In fact, it’s always the same procedure. First, find the element within a web page. After that, automate it (click, set value,…). Two simple steps. Nevertheless, the bigger your test code the more structured and well designed it should be.

RanoreXPath allows many different ways of searching for web elements within a web page. The use of Ranorex WebSpy could tempt someone to always copy & paste RanoreXPaths from WebSpy into the test automation code. What an easy job. But consider that searching elements from scratch permanently could be time consuming, especially if using optimized RanoreXPaths. Moreover, the resulting test automation code is not as readable as we would like to have it. Take a look at following code lines:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using Ranorex;   
  5. using Ranorex.Web;   
  6.   
  7. namespace WebTestDont   
  8. {   
  9.     class Program   
  10.     {   
  11.         static void Main(string[] args)   
  12.         {   
  13.             // Resource based identification of web elements   
  14.             WebDocument exampleDoc = WebDocument.OpenDocument(”www.ranorex.com/web-testing-examples”, true);   
  15.   
  16.             // Find ‘Name’ textbox within form   
  17.             WebElement name = exampleDoc.FindSingle(”//input[@id='testname']“);   
  18.             Mouse.MoveToWebElement(name);   
  19.             name.Value = “Mr. XYZ”;   
  20.   
  21.             // Find ‘Color’ drop down box within form   
  22.             WebElement colorSelector = exampleDoc.FindSingle(”//select[@id='testcolor']“);   
  23.             Mouse.MoveToWebElement(colorSelector);   
  24.             colorSelector.Value = “blue”;   
  25.   
  26.             // … search for next elements …   
  27.         }   
  28.     }   
  29. }  

I know, the few code lines above don’t give a good example for a long and unreadable test code. But what we can see, is that the example always starts the element search from scratch by calling the method ‘FindSingle’ from the ‘exampleDoc’ object. Another problem is the use of hard-coded RanoreXPath expressions. Due to changes within your AUT (application under test) you may need to update your code lines with new RanoreXPath expressions, to ensure your test is still workable .

For that reason it’s advisable to extract and separate your RanoreXPath search strings from your test codes. There are many different types to generate global mapping/resource information. The next section concentrates on the use of resource files provided by Microsoft Visual Studio.

Visual Studio resource table for GUI mapping

Visual Studio provides resource files for storing several types of objects (Images, Icons, Strings, …). Our use case only requires a resource table of type string. Simple add a new resource file called ‘GuiMapping’ to your Visual Studio project.

To add a resource file please

  • open your Visual Studio project properties and
  • add a new resource within the ‘Resource’ tab.

Resource based GUI mapping

The use of resource files extracts complex search strings from test code and concentrates searching information at one single location. The static ‘GuiMapping’ object is easy to use during coding and provides all necessary RanoreXPath expressions. In addition to that, the following implementation speeds up the search of web elements: In the first step we search for the web element of type ‘form’. After that, each search for web elements located within this form can be done by calling the method ‘FindSingle’ on the form object.

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4.   
  5. using Ranorex;   
  6. using Ranorex.Web;   
  7.   
  8. namespace WebTest   
  9. {   
  10.     // Using 'Properties' namespace to access   
  11.     // Resource elements   
  12.     using Properties;   
  13.   
  14.     class Program   
  15.     {   
  16.         [STAThread]   
  17.         static void Main(string[] args)   
  18.         {   
  19.             // Resource based identification of web elements   
  20.             WebDocument exampleDoc = WebDocument.OpenDocument(GuiMapping.WebTestUrl, true);   
  21.             WebElement form = exampleDoc.FindSingle(GuiMapping.WebTestForm);   
  22.   
  23.             // Find ‘Name’ textbox within form   
  24.             WebElement name = form.FindSingle(GuiMapping.WebTestName);   
  25.             Mouse.MoveToWebElement(name);   
  26.             name.Value = “Mr. XYZ”;   
  27.   
  28.             // Find ‘Color’ drop down box within form   
  29.             WebElement colorSelector = form.FindSingle(GuiMapping.WebTestColors);   
  30.             Mouse.MoveToWebElement(colorSelector);   
  31.             colorSelector.Value = “blue”;   
  32.         }   
  33.     }   
  34. }  

For that reason, outsourcing of searching criteria enhances readability and coding maintenance for future extensions.

posted on 2008-10-22 15:59  starspace  阅读(462)  评论(0编辑  收藏  举报

导航