Selenium RC Test Framework using XML Data structure

In this example I will explain you how to create a Selenium RC test framework and have the test data including the xpaths in a XML data file. The scripts mentioned under are as following:
1. ObjectRef.cs file that has the test data and the xpaths defined, the code to read the XML Data file
2. FunctionReference.cs that has the TestInitialize and TestCleanup. The Test Data and Xpaths are in Data.xml.
3. Two test methods Google_AU and Google_US scripts (Google Search) – Google_AUTest.cs and Google_USTest.cs
Add System.Xml.dll to the reference library.

This example is in Visual Studio C#.

Code for ObjectRef.cs

[csharp]
using System.IO;
using System;
using System.Text;
using System.Diagnostics;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Selenium;
namespace GOOGLE
{
public class ObjectRef
{
//===========================================================================
// Define the test data
//===========================================================================
public string Google_au_url;
public string Googlesearch_au_tx;
public string SearchPagetitle_au_tx;
public string Google_us_url;
public string Googlesearch_tx;
public string SearchPagetitle_tx;
public string GoogleSearch_au_xp;
public string GoogleSearch_xp;
public string Google_textbox_xp;
public string Google_submit_xp;
//===========================================================================
// Define the xml data file path and other xml related objects
//===========================================================================
public ObjectRef DataFile;
public XmlDocument Doc;
public string FilePath;
public ObjectRef(string Path)
{
Doc = new XmlDocument();
FilePath = Path;
Doc.Load(Path);
}
public ObjectRef()
{ }
public string GetValue(string xPath)
{
string retVal = "";
XPathNodeIterator ni = GetValueNodeSet(xPath);
if (ni.Count > 0)
{
if (ni.Count > 1)
{
while (ni.MoveNext())
retVal = retVal + ni.Current.Value + ";";
}
else
{
ni.MoveNext();
retVal = ni.Current.Value;
}
}
return retVal;
}
private XPathNodeIterator GetValueNodeSet(string xPath)
{
XPathNodeIterator ni;
XPathNavigator nav;
nav = Doc.CreateNavigator();
ni = (XPathNodeIterator)nav.Evaluate(xPath);
return ni;
}
//===========================================================================
// Defining the Path of the xml data file
//===========================================================================
public static string ReadXMLDataFile()
{
String Path = Directory.GetCurrentDirectory();
Path = Directory.GetParent(Path).Parent.Parent.FullName + "\\Data.xml";
return Path;
}
//===========================================================================
// Reading the xml file – Test Data and Xpath values
//===========================================================================
public void GetXMLData()
{
ObjectRef DataFile = new ObjectRef(ReadXMLDataFile());
Google_au_url = DataFile.GetValue("//Google_AU/Google_url");
Googlesearch_au_tx = DataFile.GetValue("//Google_AU/Googlesearch_tx");
SearchPagetitle_au_tx = DataFile.GetValue("//Google_AU/SearchPagetitle_tx");
Google_us_url = DataFile.GetValue("//Google_US/Google_url");
Googlesearch_tx = DataFile.GetValue("//Google_US/Googlesearch_tx");
SearchPagetitle_tx = DataFile.GetValue("//Google_US/SearchPagetitle_tx");
GoogleSearch_au_xp = DataFile.GetValue("//Xpaths/GoogleSearch_au_xp");
GoogleSearch_xp = DataFile.GetValue("//Xpaths/GoogleSearch_xp");
Google_textbox_xp = DataFile.GetValue("//Xpaths/Google_textbox_xp");
Google_submit_xp = DataFile.GetValue("//Xpaths/Google_submit_xp");
}
}
}[/csharp]

Code for FunctionReference.cs

[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
namespace GOOGLE
{
[TestClass]
public class FunctionReference : ObjectRef
{
public ISelenium selenium;
public StringBuilder verificationErrors;
[TestInitialize()]
public void SetupTest()
{
GetXMLData();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", Google_au_url);
selenium.Start();
selenium.SetTimeout("1200000");
selenium.WindowMaximize();
verificationErrors = new StringBuilder();
}
[TestCleanup()]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
}
}[/csharp]

Code for Google_AUTest.cs
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
namespace GOOGLE
{
[TestClass]
public class Google_AUTest : FunctionReference
{
[TestMethod]
public void Google_AU()
{
string TestCase = "Google-AU";
selenium.Open(Google_au);
selenium.WaitForPageToLoad("20000");
selenium.WindowMaximize();
Console.WriteLine(TestCase + "Current location ->" + selenium.GetLocation());
selenium.Type(Google_textbox_xp, Googlesearch_au_tx);
selenium.Click(Google_submit_xp);
Thread.Sleep(10000);
if (selenium.IsElementPresent(GoogleSearch_au_xp))
{
Console.WriteLine(TestCase + "Selenium Wiki text is present on the search Page");
selenium.Click(GoogleSearch_au_xp);
selenium.WaitForPageToLoad("20000");
try
{
Assert.IsTrue(selenium.GetTitle().Equals(SearchPagetitle_au_tx));
Console.WriteLine(TestCase + "Selenium Wiki -> " + selenium.GetLocation() + " is opened with title ->" + selenium.GetTitle());
}
catch
{
Console.WriteLine(TestCase + "Selenium Wiki is not opened.Current page title ->" + selenium.GetTitle());
}
}
else
{
Console.WriteLine(TestCase + "Selenium Wiki text is NOT present on the search Page");
}
}
}
}
[/csharp]

Code for Google_USTest.cs
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;
namespace GOOGLE
{
[TestClass]
public class Google_USTest : FunctionReference
{
[TestMethod]
public void Google_US()
{
string TestCase = "Google-US";
selenium.Open(Google_url);
selenium.WaitForPageToLoad("20000");
selenium.WindowMaximize();
Console.WriteLine(TestCase + "Current location ->" + selenium.GetLocation());
selenium.Type(Google_textbox_xp,Googlesearch_tx);
selenium.Click(Google_submit_xp);
Thread.Sleep(10000);
if (selenium.IsElementPresent(GoogleSearch_xp))
{
Console.WriteLine(TestCase + "Selenium Remote-Control text is present on the search Page");
selenium.Click(GoogleSearch_xp);
selenium.WaitForPageToLoad("20000");
try
{
Assert.IsTrue(selenium.GetTitle().Equals(SearchPagetitle_tx));
Console.WriteLine(TestCase + "Selenium HQ ->" + selenium.GetLocation() + " is opened with title ->" + selenium.GetTitle());
}
catch
{
Console.WriteLine(TestCase + "Selenium HQ -> is not opened.Current page title ->" + selenium.GetTitle());
}
}
else
{
Console.WriteLine(TestCase + "Selenium Remote-Control text is NOT present on the search Page");
}
}
}
}
[/csharp]

Code Contributors: Bi Li & Rashmi Rajiv
If you found this solution helpful or have something extra to add, feel free to share it here by commenting below.

One thought on “Selenium RC Test Framework using XML Data structure

  1. sensible tester says:

    i want to simulate data coming from a service (connected to a plc)
    I want to send the data in specific xml format (xsd) for 4 different type of messages and in end putting that data into sqlserver DB.
    my problem is that in 1 single test using selenium code captured from ide, and then integrated in the java eclipse envir.
    i am trying to first upload the messages xml, into db and then verify the data on the gui.
    but dont know how to start. anyone any exampke fo such type of test ?
    how can i upload the xml file. i have some xml files that were used by qtp in previous tests.
    but i want to use selenium for this.

Leave a Reply

Your email address will not be published. Required fields are marked *