Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

using using using using using using using

System; System.Text; System.Reflection; System.Threading; NUnit.Framework; Selenium; System.Text.RegularExpressions;

using excel = Microsoft.Office.Interop.Excel; using worksheet = Microsoft.Office.Interop.Excel.Worksheet; namespace ConsoleDataDriven { class Program { static void Main(string[] args) { NewTest nt = new NewTest(); nt.SetupTest(); } } public class NewTest { private ISelenium selenium; private StringBuilder verificationErrors; [SetUp] public void SetupTest() { selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http ://blyisdivg02.bly.csc.com/"); //CommandExecutor executor = new SeleneseCommandExecutor(selenium); //DesiredCapabilities dc = new DesiredCapabilities(); //driver = new RemoteWebDriver(executor, dc); selenium.Start(); verificationErrors = new StringBuilder(); TheNewTest(); } [TearDown] public void TeardownTest() { try { selenium.Stop(); } catch (Exception) { // Ignore errors if unable to close the browser } Assert.AreEqual("", verificationErrors.ToString()); } public string funct(string s) {

return "abc"; } public string VerifyValue(string fParam) { //fParam = "FuncName,FuncValue"; string[] words = fParam.Split(','); string IdName = words[0]; string IdValue = words[1]; if (IdValue == selenium.GetValue("id=" + IdName)) { return "Pass"; } else { return "Fail"; } //try //{ // //Boolean result; // Assert.AreEqual(IdValue, selenium.GetValue("id=" + IdName)); //} //catch (AssertionException e) //{ // verificationErrors.Append(e.Message); //} } public void writepass(string passtext) { System.IO.StreamWriter passmsg = new System.IO.StreamWriter("C:\\sel enium\\pass.txt", false); passmsg.WriteLine("[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + "Pass: " + passtext); //passmsg.WriteLine(passtext); passmsg.Close(); } public void writefail(string failtext) { System.IO.StreamWriter failmsg = new System.IO.StreamWriter("C:\\sel enium\\fail.txt", false); failmsg.WriteLine("[" + System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString() + "] " + "Fail: " + failtext); //failmsg.WriteLine(failtext); failmsg.Close(); } public void PageLoad() { selenium.WaitForPageToLoad("30000"); } public void FieldVisible(String FieldName) {

for (int second = 0; ; second++) { if (second >= 60) Assert.Fail("timeout"); try { if (selenium.IsVisible(FieldName)) break; } catch (Exception) { } Thread.Sleep(1000); } } public void TextPresent(String TextName) { for (int second = 0; ; second++) { if (second >= 60) Assert.Fail("timeout"); try { if (selenium.IsTextPresent(TextName)) break; } catch (Exception) { } Thread.Sleep(1000); } } public void Refresh(String FieldName) { for (int second = 0; ; second++) { if (second >= 60) Assert.Fail("timeout"); try { if (!selenium.IsVisible(FieldName)) break; } catch (Exception) { } Thread.Sleep(1000); } } [Test] public void TheNewTest() { string ResultSheetPath = @"c:\selenium\Result_MasterSheet.xls"; Microsoft.Office.Interop.Excel.ApplicationClass ResultSheetData = ne w excel.ApplicationClass(); excel.Workbook ResultworkBook = ResultSheetData.Workbooks.Open(Resul tSheetPath, 0, true, 5, "", "", true, excel.XlPlatform.xlWindows, "\t", false, f alse, 0, true, 1, 0); excel.Worksheet ResultworkSheet = (worksheet)ResultworkBook.Workshee ts.get_Item("ResultSheet");

//string ExcelDataPath = @"c:\selenium\GoogleTestData.xls"; string MasterSheetPath = @"c:\selenium\MasterSheet.xls";

Microsoft.Office.Interop.Excel.ApplicationClass MasterSheetData = ne w excel.ApplicationClass(); excel.Workbook TestworkBook = MasterSheetData.Workbooks.Open(MasterS heetPath, 0, true, 5, "", "", true, excel.XlPlatform.xlWindows, "\t", false, fal se, 0, true, 1, 0); excel.Worksheet TestworkSheet = (worksheet)TestworkBook.Worksheets.g et_Item("TestCases"); String TestCaseName = "A"; String ExecuteCheck = "B"; String TestCaseRow = "2"; Boolean Flag = false; //selenium.Open("/loginins/secbypass.aspx"); selenium.Open("/InsuredPortal/InsuredLogin.aspx"); while (Flag == false) { String TestCaseCell = TestCaseName + TestCaseRow; String ExecuteCheckCell = ExecuteCheck + TestCaseRow; System.String TestCase = ((excel.Range)TestworkSheet.get_Range(T estCaseCell, Type.Missing)).Value2 as string; System.String Execute = ((excel.Range)TestworkSheet.get_Range(Ex ecuteCheckCell, Type.Missing)).Value2 as string; string ExcelDataPath = ""; if (Execute == "Y") { //string ExcelDataPath = @"c:\selenium\GoogleTestData.xls"; ExcelDataPath = @"c:\selenium\" + TestCase + ".xls"; //selenium.Open("/loginins/secbypass.aspx"); Microsoft.Office.Interop.Excel.ApplicationClass ExcelData = new excel.ApplicationClass(); excel.Workbook DataworkBook = ExcelData.Workbooks.Open(Excel DataPath, 0, true, 5, "", "", true, excel.XlPlatform.xlWindows, "\t", false, fal se, 0, true, 1, 0); excel.Worksheet DataworkSheet = (worksheet)DataworkBook.Work sheets.get_Item("Main"); String String String String Flag = SerialNum = "A"; Page = "B"; Active = "C"; MainCellRow = "2"; false;

while (Flag == false)

{ String SerialCell = SerialNum + MainCellRow; String PageCell = Page + MainCellRow; String ActivityCell = Active + MainCellRow; System.String StringSerial = ((excel.Range)DataworkSheet .get_Range(SerialCell, Type.Missing)).Value2 as string; System.String StringPage = ((excel.Range)DataworkSheet.g et_Range(PageCell, Type.Missing)).Value2 as string; String StringActive; StringActive = ((excel.Range)DataworkSheet.get_Range(Act ivityCell, Type.Missing)).Value2.ToString(); if (StringSerial == "EOR") { Flag = true; break; } if (StringActive == "Y") { DataworkSheet = (worksheet)DataworkBook.Worksheets.g et_Item(StringPage);

String String String String Flag =

TypeCol = "A"; NameCol = "B"; ValueCol = "C"; CellRow = "2"; false;

while (Flag == false) { String TextResult = ""; String TypeCell = TypeCol + CellRow; String NameCell = NameCol + CellRow; String ValueCell = ValueCol + CellRow; System.String StringType = ((excel.Range)Datawor kSheet.get_Range(TypeCell, Type.Missing)).Value2 as string; System.String StringName = ((excel.Range)Datawor kSheet.get_Range(NameCell, Type.Missing)).Value2 as string; String StringValue; StringValue = ((excel.Range)DataworkSheet.get_Ra nge(ValueCell, Type.Missing)).Value2.ToString(); switch (StringType) { case "text": //Regex g = new Regex(StringName); //[a-zA-Z0-9]{6,50}/ //StringName = g + StringName; //g = StringName; selenium.Focus("id=" + StringName); selenium.Type("id=" + StringName, String Value); //selenium.Focus("id=" + StringName); break;

case "Type": selenium.TypeKeys("id=" + StringName, St ringValue); selenium.Focus("id=" + StringName); break; case "Down": selenium.MouseDownAt(StringName, ""); break; case "Select": selenium.Select("id=" + StringName, "lab el=" + StringValue); break; case "image": selenium.Focus(StringName); selenium.Click(StringName); //selenium.WaitForPageToLoad("30000"); break; case "link": selenium.Click("link=" + StringName); //selenium.WaitForPageToLoad("30000"); break; case "Function": //Get the method information using the m ethod info class MethodInfo mi = this.GetType().GetMethod (StringName); //Invoke the method // (null- no parameter for the method ca ll // or you can pass the array of paramete rs...) if (StringValue == "A") { mi.Invoke(this, null); } else { object[] parametersArray = new objec t[] { StringValue }; mi.Invoke(this, parametersArray); } //selenium.WaitForPageToLoad("30000"); break; case "Condition": //string[] words = StringValue.Split(',' ); //string IdName = words[0]; //string IdValue = words[1]; MethodInfo minfo = this.GetType().GetMet hod(StringName); if (StringValue != null) { object[] parametersArray = new objec t[] { StringValue }; object Result = minfo.Invoke(this, p arametersArray); TextResult = Result.ToString();

//string rslt = funct("as"); //minfo.Invoke(this, parametersArray ); }

((excel.Range)ResultworkSheet.get_Range( ExecuteCheckCell, Type.Missing)).Value2 = TextResult;

//ResultSheetData.SaveWorkspace(); //ResultSheetData.Quit(); //ResultSheetPath = null; //ResultSheetData = null; //ResultworkBook = null; //ResultworkSheet = null; TextResult = null; //System.String TestCase = ((excel.Range )TestworkSheet.get_Range(TestCaseCell, Type.Missing)).Value2 as string; //System.String Execute = ((excel.Range) TestworkSheet.get_Range(ExecuteCheckCell, Type.Missing)).Value2 as string; break; case "End": Flag = true; break; } //if (Flag == true) //{ // break; //} //selenium.Type(StringName, StringValue); int PageRow = int.Parse(CellRow); String PageRowNext = (PageRow + 1).ToString(); CellRow = PageRowNext; } } DataworkSheet = (worksheet)DataworkBook.Worksheets.get_I tem("Main"); Flag = false; //if (StringActive == "N") //{ //DataworkSheet = (worksheet)DataworkBook.Worksheets .get_Item("Main"); int Row = int.Parse(MainCellRow); String RowNext = (Row + 1).ToString(); MainCellRow = RowNext; //} }

} Flag = false; if (Execute == "END") { Flag = true; break; } //if (Execute == "N") //{ int TestRow = int.Parse(TestCaseRow); String TestRowNext = (TestRow + 1).ToString(); TestCaseRow = TestRowNext; //} } //if (selenium.IsTextPresent("Selenium eRemote-Control")) //{ // writepass("the text is present"); // // passmsg.Close(); //} //else //{ // writefail("the text is not present"); // //passmsg.Close(); //} ResultworkSheet.SaveAs(@"c:\selenium\Result_MasterSheet11.xls"); ResultworkBook.Close(); } } }

You might also like