Skip to main content

Data Driven Test using Selenium for Beginners

Guys,
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;
  14.  
  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);
  26.          
  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"]);
  33.  
  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"]);
  40.          
  41.             adp1.Dispose();
  42.             adp2.Dispose();
  43.             conn.Close();
  44.             return DS;
  45.         }
  46.  
  47.         [TestMethod]
  48.         public void TestGmailSend()
  49.         {
  50.             ISelenium RC;
  51.             StringBuilder verificationErrors;
  52.             string[] Browsers = { "*iehta""*firefox""*googlechrome" };
  53.          
  54.             DataSet TD = new DataSet();
  55.             TD = getData();
  56.  
  57.             for (int b = 0; b < Browsers.Length; b++)                 // for each browser execute all
  58.             {
  59.                 RC = new DefaultSelenium("localhost"4444, Browsers[b]"http://www.google.co.in/");              
  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");                
  76.  
  77.                     for (int i = 0; i < TD.Tables["TABLE2"].Rows.Count; i++)        // Test data iterations - Send Mail
  78.                     {
  79.                         //selenium.Open("https://mail.google.com/mail/?hl=en&shva=1#compose");
  80.                         //selenium.Click("");
  81.                      
  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
UserName
Password
GmailSend sheet contains 2 columns
ToAddress
Subject
MailBody

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....

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

Comments

Popular posts from this blog

Some good Resources / Blogs / Sites for Selenium Users

Here I have listed out some good blogs and sites by extensive selenium automation users. Hope these will help you a lot. http://automationtricks.blogspot.com  - by NirajKumar http://www.theautomatedtester.co.uk/ http://testerinyou.blogspot.com   - by Naga/Mathu http://seleniumready.blogspot.com  - by Farheen Khan http://seleniumdeal.blogspot.com/  - Amit Vibhuti http://seleniumexamples.com/blog Sauce Labs and BrowserMob are companies doing cloud and extensive selenium automation services, products, etc http://saucelabs.com/blog http://blog.browsermob.com http://testingbot.com/ Cedric Beust -  creator of the TestNG Java testing framework. http://beust.com/weblog/ http://blog.reallysimplethoughts.com/  - by Samit Badle, Created many Selenium IDE Plug-Ins Available Colud Testing: 1. SauceLabs 2. Soasta 3. BrowserMob 4. CloudTesting.com  etc. Selenium Testing Products: 1. Twist by ThoughtWorks 2.  TestMaker by...

UFT - Take full page screenshot by scrolling the page

'######################################################################################## 'This is navigate through the full page and taking individual screenshot of visible area '######################################################################################## Function TakeScreenshot Dim intScrolls, intScroll, strScrollPos Set pgApp = Browser ( " " ) .Page ( " " ) intScrolls = Round ( pgApp . RunScript ( " document.documentElement.scrollHeight / (screen.height) " ) , 2 ) If intScrolls < 1 Then intScrolls = - 1 pgApp . RunScript " window.scrollTo(0, 0); " Wait 1 Browser ( " " ) .CaptureBitmap " C:\screenshot0.png " , True For intScroll = 0 To intScrolls If Environment . Value ( " Browser " ) = " CHROME " Then strScrollPos = " scrollY " Else strScrollPos = " document.documentElement.scrollTop " End If If p...

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 End   Function This option is very useful. But in real time, lot of customers could have restricted write access to windows registry. So we can try othe...