Global GUI map for automation with VS.NET
2008-10-22 15:59
701 查看
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:
view plaincopy to clipboardprint
using System;
using System.Collections.Generic;
using System.Text;
using Ranorex;
using Ranorex.Web;
namespace WebTestDont
{
class Program
{
static void Main(string[] args)
{
// Resource based identification of web elements
WebDocument exampleDoc = WebDocument.OpenDocument(”www.ranorex.com/web-testing-examples”, true);
// Find ‘Name’ textbox within form
WebElement name = exampleDoc.FindSingle(”//input[@id='testname']“);
Mouse.MoveToWebElement(name);
name.Value = “Mr. XYZ”;
// Find ‘Color’ drop down box within form
WebElement colorSelector = exampleDoc.FindSingle(”//select[@id='testcolor']“);
Mouse.MoveToWebElement(colorSelector);
colorSelector.Value = “blue”;
// … search for next elements …
}
}
}
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 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.
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.
view plaincopy to clipboardprint
using System;
using System.Collections.Generic;
using System.Text;
using Ranorex;
using Ranorex.Web;
namespace WebTest
{
// Using 'Properties' namespace to access
// Resource elements
using Properties;
class Program
{
[STAThread]
static void Main(string[] args)
{
// Resource based identification of web elements
WebDocument exampleDoc = WebDocument.OpenDocument(GuiMapping.WebTestUrl, true);
WebElement form = exampleDoc.FindSingle(GuiMapping.WebTestForm);
// Find ‘Name’ textbox within form
WebElement name = form.FindSingle(GuiMapping.WebTestName);
Mouse.MoveToWebElement(name);
name.Value = “Mr. XYZ”;
// Find ‘Color’ drop down box within form
WebElement colorSelector = form.FindSingle(GuiMapping.WebTestColors);
Mouse.MoveToWebElement(colorSelector);
colorSelector.Value = “blue”;
}
}
}
For that reason, outsourcing of searching criteria enhances readability and coding maintenance for future extensions.
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:
view plaincopy to clipboardprint
using System;
using System.Collections.Generic;
using System.Text;
using Ranorex;
using Ranorex.Web;
namespace WebTestDont
{
class Program
{
static void Main(string[] args)
{
// Resource based identification of web elements
WebDocument exampleDoc = WebDocument.OpenDocument(”www.ranorex.com/web-testing-examples”, true);
// Find ‘Name’ textbox within form
WebElement name = exampleDoc.FindSingle(”//input[@id='testname']“);
Mouse.MoveToWebElement(name);
name.Value = “Mr. XYZ”;
// Find ‘Color’ drop down box within form
WebElement colorSelector = exampleDoc.FindSingle(”//select[@id='testcolor']“);
Mouse.MoveToWebElement(colorSelector);
colorSelector.Value = “blue”;
// … search for next elements …
}
}
}
using System; using System.Collections.Generic; using System.Text; using Ranorex; using Ranorex.Web; namespace WebTestDont { class Program { static void Main(string[] args) { // Resource based identification of web elements WebDocument exampleDoc = WebDocument.OpenDocument(”www.ranorex.com/web-testing-examples”, true); // Find ‘Name’ textbox within form WebElement name = exampleDoc.FindSingle(”//input[@id='testname']“); Mouse.MoveToWebElement(name); name.Value = “Mr. XYZ”; // Find ‘Color’ drop down box within form WebElement colorSelector = exampleDoc.FindSingle(”//select[@id='testcolor']“); Mouse.MoveToWebElement(colorSelector); colorSelector.Value = “blue”; // … search for next elements … } } }
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 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.
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.
view plaincopy to clipboardprint
using System;
using System.Collections.Generic;
using System.Text;
using Ranorex;
using Ranorex.Web;
namespace WebTest
{
// Using 'Properties' namespace to access
// Resource elements
using Properties;
class Program
{
[STAThread]
static void Main(string[] args)
{
// Resource based identification of web elements
WebDocument exampleDoc = WebDocument.OpenDocument(GuiMapping.WebTestUrl, true);
WebElement form = exampleDoc.FindSingle(GuiMapping.WebTestForm);
// Find ‘Name’ textbox within form
WebElement name = form.FindSingle(GuiMapping.WebTestName);
Mouse.MoveToWebElement(name);
name.Value = “Mr. XYZ”;
// Find ‘Color’ drop down box within form
WebElement colorSelector = form.FindSingle(GuiMapping.WebTestColors);
Mouse.MoveToWebElement(colorSelector);
colorSelector.Value = “blue”;
}
}
}
using System; using System.Collections.Generic; using System.Text; using Ranorex; using Ranorex.Web; namespace WebTest { // Using 'Properties' namespace to access // Resource elements using Properties; class Program { [STAThread] static void Main(string[] args) { // Resource based identification of web elements WebDocument exampleDoc = WebDocument.OpenDocument(GuiMapping.WebTestUrl, true); WebElement form = exampleDoc.FindSingle(GuiMapping.WebTestForm); // Find ‘Name’ textbox within form WebElement name = form.FindSingle(GuiMapping.WebTestName); Mouse.MoveToWebElement(name); name.Value = “Mr. XYZ”; // Find ‘Color’ drop down box within form WebElement colorSelector = form.FindSingle(GuiMapping.WebTestColors); Mouse.MoveToWebElement(colorSelector); colorSelector.Value = “blue”; } } }
For that reason, outsourcing of searching criteria enhances readability and coding maintenance for future extensions.
相关文章推荐
- Web Automation with Python for .NET
- Using jQuery Mobile with MVC and Netduino for Home Automation
- ASP.NET操作EXCEL时出现的错误 Retrieving the COM class factory for component with CLSID
- 转贴: Controlling a Barcode Reader with POS for .NET
- PRB:WebGrid column headers do not align with their columns in NetAdvantage for .NET 2007 Volume 3
- 超郁闷,Together for VS.NET 2.0 安装不上啊
- Is VS.NET ready for enterprise? (6)
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- Get Started With Continuous Integration For Your .NET (C#) Projects - Build Your Project
- Creating CustomBinding for WCFBasicHTTP for SSL and BasicAuthentication in BizTalk 转载自:http://geekswithblogs.net/mipsen
- Using Let’s Encrypt for free SSL Certs with Netscaler
- No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClie
- .NET with Ruby and ASP.NET vs. Rails [转载]
- Office application does not quit after automation from Visual Studio .NET client(从VS.NET客户端自动化后,Office应用程序不退出)
- Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios
- Using StructureMap with the ASP.NET MVC framework
- How to create installation patches for VS.NET deployment projects
- When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
- Automation for the people: Speed deployment with automation
- Get Started With Continuous Integration For Your .NET (C#) Projects - Test Your Project