Thursday, 13 August 2009

My Answer(s) posted on Code Project web site Part-1

In this Blog i would like to share my Answers which i have posted on Code project.
reference: http://www.codeproject.com/script/Forums/Messages.aspx?fmid=4764307
Question: Can WatiN accept inputs from an Excel sheet
Can WatiN accept inputs form an excel sheet. this functionality was there in watir.
Is this available in WatiN.

Jawed: yes it is.for that you need to write simple C# code to read from excel sheet.see below
some example from my side.

public class CommonClassAutomation
{
static string connectionString
= "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=
D:/test.xls;Extended Properties=Excel 8.0;";
public static string inputText= string.Empty;

public void TC_googletest()
{


//before calling instance of browser read input from excel sheet.
readExcelsheet();
//Open instance of IE browser.
IE ie=new IE();
//Open Google.com in browser
ie.goto("www.google.com");
/*Provide input text in to google search box. which you have already having from
excel sheet.*/
ie.TextField(Find.ByName("q")).TypeText(inputText);
//Click on Search button.
ie.Button(Find.ByName("btnG")).Click();
}
//Method To read data from Excel sheet
public void readExcelsheet()
{
DbProviderFactory factory = DbProviderFactories.GetFactory
("System.Data.OleDb");
int inputCount=0;
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT * FROM ["+ sheetName+"$]";
try
{
connection.Open();
using (DbDataReader dr = command.ExecuteReader())
{
if (dr.Read())
{
inputText= dr[1].ToString();
}
}
}
catch(Exception ex)
{
//To Do
}
}
}
}
}
Hope, it would be useful for you.
happy automation
Thanks,
Md.Jawed
Hi I'm working on WatiN tool. I've scenario where i need to check a checkbox and click on ok button in the popup window. i've used AttachtoIE method and used URL attribute to attach to the popup window. Now the problem is URL contains the ID value, which changes each time the popup appears.. so how to handle this or is there any other method other than AttachtoIE.
please give some suggestion thanking you.
Other Member: Yes I got it right, I ignored the query string part in the URL which used to change everytime.
for example,http://192.168.25.10:221/admin/UploadContent.aspx?opener=CustomContent_Add.aspxIn the above URL the second line is the query string which will be present after the question mark in the first line.so I used only the first line neglecting the second like this
IE popup = IE.AttachToIE(Find.ByUrl(Url, true);
this worked for me, hope this will help you
Jawed: Hi,@ninay_fz: nice reply
Just I want to add few points over here to make your piece of code to work with dynamic changing URL.In this situation is good practice to use Regular expression.
Add this name space to your code file.

using System.Text.RegularExpressions;

Use below code to generalize while accessing URL.
string Url= "http://192.168.25.10:221/admin/UploadContent.aspx";(or whatever string you want to put!!)
Regex reg = new Regex(Url);
IE popup = IE.AttachToIE(Find.ByUrl(reg , true));
Hope it would solve your problem.
Thanks,
Continue..............

2 comments:

Dooph said...

Have you used the Microsoft.Office.Interop namespace instead?

MD.JAWED said...

Yes Dooph,
yes i have used Microsoft.Office.Interop also for reading excel workbook.
But i would prefer to use OleDbConnection to open the file.cuz its easy to treat your excel book as your database(security and performance purpose). yeah the important point over here is that i m writing this code for Automation purpose, already i have used COM interop to open instance of browser. And once again using COM interop for Microsoft.Office.Interop is not good idea.
Pleas let me know your opinion and suggestion on the same. Thanks!!