Skip to main content

Data Driven Test using Selenium for Beginners

Here I have mentioned some ideas about how to do data driven test in selenium tool with selenium RC libraries.
Here is some sample code and you can pickup from here...

  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Linq;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
  8. using System.Threading;
  9. using NUnit.Framework;
  10. using Selenium;
  11. using System.Data;
  12. using System.Data.OleDb;
  13. using System.Windows.Forms;
  15. namespace SELENIUM
  16. {
  17.     [TestClass]
  18.     public class SEL_TEST2
  19.     {
  20.         private DataSet getData()
  21.         {
  22.             string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\Selenium\TESTDATA.xlsx';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
  23.             OleDbConnection conn = new OleDbConnection(connString);
  24.             OleDbDataAdapter adp1 = new OleDbDataAdapter("Select * from [GmailLogin$]", conn);
  25.             OleDbDataAdapter adp2 = new OleDbDataAdapter("Select * from [GmailSend$]", conn);
  27.             DataSet DS = new DataSet();
  28.             DataTable DT1 = new DataTable("TABLE1");
  29.             DT1.Columns.Add("UserName");
  30.             DT1.Columns.Add("Password");
  31.             DS.Tables.Add(DT1);
  32.             adp1.Fill(DS.Tables["TABLE1"]);
  34.             DataTable DT2 = new DataTable("TABLE2");
  35.             DT2.Columns.Add("ToAddress");
  36.             DT2.Columns.Add("Subject");
  37.             DT2.Columns.Add("MailBody");
  38.             DS.Tables.Add(DT2);
  39.             adp2.Fill(DS.Tables["TABLE2"]);
  41.             adp1.Dispose();
  42.             adp2.Dispose();
  43.             conn.Close();
  44.             return DS;
  45.         }
  47.         [TestMethod]
  48.         public void TestGmailSend()
  49.         {
  50.             ISelenium RC;
  51.             StringBuilder verificationErrors;
  52.             string[] Browsers = { "*iehta""*firefox""*googlechrome" };
  54.             DataSet TD = new DataSet();
  55.             TD = getData();
  57.             for (int b = 0; b < Browsers.Length; b++)                 // for each browser execute all
  58.             {
  59.                 RC = new DefaultSelenium("localhost"4444, Browsers[b]"");              
  60.                 RC.Start();
  61.                 verificationErrors = new StringBuilder();
  62.                 Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual("", verificationErrors.ToString());
  63.                 for (int a = 0; a < TD.Tables["TABLE1"].Rows.Count; a++)          // No of users - Email Accounts
  64.                 {
  65.                     RC.Open("");
  66.                     RC.Click("//a[text()=\"Gmail\"]");
  67.                     RC.WaitForPageToLoad("30000");
  68.                     RC.Type("//input[@name=\"Email\"]", Convert.ToString(TD.Tables["TABLE1"].Rows[a]["UserName"]));
  69.                     RC.Type("//input[@name=\"Passwd\"]", Convert.ToString(TD.Tables["TABLE1"].Rows[a]["Password"]));
  70.                     RC.Click("//input[@type=\"submit\" and @value=\"Sign in\"]");
  71.                     if (RC.IsElementPresent("link=Load Basic HTML"))
  72.                     {
  73.                         RC.Click("link=Load Basic HTML");
  74.                     }
  75.                     RC.WaitForPageToLoad("120000");                
  77.                     for (int i = 0; i < TD.Tables["TABLE2"].Rows.Count; i++)        // Test data iterations - Send Mail
  78.                     {
  79.                         //selenium.Open("");
  80.                         //selenium.Click("");
  82.                         RC.Click("link=Compose Mail");
  83.                         RC.WaitForPageToLoad("60000");
  84.                         RC.Type("//textarea[@id=\"to\"]", Convert.ToString(TD.Tables["TABLE2"].Rows[i]["ToAddress"]));
  85.                         RC.Type("//input[@name=\"subject\"]", Convert.ToString(TD.Tables["TABLE2"].Rows[i]["Subject"]));
  86.                         RC.Type("//textarea[@name=\"body\"]", Convert.ToString(TD.Tables["TABLE2"].Rows[i]["MailBody"]));
  87.                         RC.Click("//form[1]/table/tbody/tr/td/input[@name='nvp_bu_send']");
  88.                         RC.WaitForPageToLoad("30000");
  89.                     }
  90.                     RC.Click("link=Sign out");
  91.                     RC.WaitForPageToLoad("30000");
  92.                 }
  93.                 RC.Stop();
  94.                 Console.WriteLine("{1}: Passed"+ Browsers[b]);
  95.             }
  96.             getData().Dispose();
  97.             TD.Dispose();
  98.         }
  99.     }
  100. }

and finally my test data file looks like,
have 2 sheets in TestData.xlsx file ---  GmailLogin  and GmailSend
GmailLogin sheet contains 2 columns
GmailSend sheet contains 2 columns

These are the columns which are referred in above code to take test data from these mentioned sheets...

Hope these info will lead to start data driven selenium RC tests....

வாழ்க வளமுடன்....


Popular posts from this blog

Change IE Browser ZOOM settings

Lot of UI automation testers could have faced this problem as we could change the zoom settings while operating manually for our convenience and forgot to reset to 100%.
But our QTP and some other related tools would operate the browser perfectly if browser zoom is 100%. So wee need to change the zoom before start to run the scripts. Its better to have a code snippet in our framework to change this zoom setting right?
Here we go...

1. We can simply change the Registry values before Invoking IE
Function ChangeRegistry Dim objShell Set objShell = CreateObject("WScript.Shell")  objShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom\ZoomFactor", "100000", "REG_DWORD" Set objShell = Nothing EndFunction This option is very useful. But in real time, lot of customers could have restricted write access to windows registry. So we can try other options.

2. Use IE COM object and Change Settings
Function ChangeIEZoom Dim intZoomLevel, objIE  intZoo…

QTP - Object Identification - Prerequesties

While starting a new project or proof of concept or after new QTP installation, we would face the issue in identifying objects as QTP built objects like WinList, WebEdit, etc.; instead everything will be identified as WinObject.

Here are some steps to ensure you did the environment setup RIGHT before start automating.
Did you enabled required Add-ins only while launchingDid you selected "Record >> Record and Run Settings" - This should be based on what type of application you are working with
Windows app - Windows >> Record and run test on any windows applications. Web app - Web >> Record and run test on any open browser.
Did you tried to launch UFT/QTP and then AUT.If its web, ensure browser zoom level set to 100%Try disabling the protected mode in IE for web.Launch UFT and AUT using RUN AS ADMINISTRATORAlso see the Object Identification Methods if you are interested.

Run JavaScript from QTP

Yeah, You can run your pure JavaScript from QTP using RunScript method. Lot of times, we are in need of firing events or simulating actions on web page which is not supported by QTP. At that time, you can use your direct DOM methods and directly execute your script on the web page from QTP like,
Dim MyPage, SearchBox Set MyPage = Browser("title:=Google").Page("title:=Google") Set SearchBox = MyPage.RunScript("document.getElementsByName('q')(0);") SearchBox.Value="testing"
'if objects available in frames, Set SearchBox = MyPage.RunScript("document.frames(4).document.getElementsByName('q')(0);")
Set objFrame = Browser("title:=Google").Page("title:=Google").Frame("title:=something")
objFrame.RunScript("document.getElementsByName('q')(0);") Also, you can execute by obtaining actual browser window object if you have IE app like, Dim IEApp Set IEApp = CreateObject("Internet…