This Blog gives good knowledge of core java concepts and selenium (Automation Testing Tool). It also includes some topics of MAVEN (Build Tool) , Jenkins (Continuous Integration Tool) and Cucumber (Behavior Driven Development)
Tuesday, 3 November 2015
Basic Selenium functions with C#
Basic Selenium functions with C#
Sample Script for Framework:
public OpenQA.Selenium.IWebDriver iWebDriver; public static int isloggedin = 0; public StreamWriter sw; public string desc; public string mode; public IWebDriver StartTest() { if (isloggedin == 0) { iWebDriver = new FirefoxDriver(); iWebDriver.Manage().Window.Maximize(); login(); isloggedin = 1; } return iWebDriver; } public int RandomNumber() { Random r = new Random(); return r.Next(1, 1000); } public void login() { iWebDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(300)); iWebDriver.Navigate().GoToUrl(dataFile("URL")); // Enter user name iWebDriver.FindElement(By.Id("txtEmail")).Clear(); iWebDriver.FindElement(By.Id("txtEmail")).SendKeys(dataFile("UserName")); System.Threading.Thread.Sleep(5); // Enter password iWebDriver.FindElement(By.Id("txtPassword")).Clear(); iWebDriver.FindElement(By.Id("txtPassword")).SendKeys(dataFile("Password")); // Click Submit button iWebDriver.FindElement(By.Id("lnkSubmit")).Click(); System.Threading.Thread.Sleep(5000); }
Sample Script for Framework:
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Support.UI; using System.Threading; using System.IO; namespace SeleniumTest { public class TC_001 : Logging { public String startTime; public String endTime; public String status; Logging c1 = new Logging(); public void CreateUser(IWebDriver WebDriver) { startTime = DateTime.Now.ToString(); if (excel(this.GetType().Name).Equals("Y")) { WebDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(420)); try { Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, "Starting Test"); WebDriver.Navigate().GoToUrl("Example Website"); WebDriver.FindElement(By.XPath("//a[contains(text(),'NEW CONTACT')]")).Click(); WebDriver.FindElement(By.Id("new-contact")).Click(); System.Threading.Thread.Sleep(5000); new SelectElement(WebDriver.FindElement(By.Id("newcontact-location"))).SelectByIndex(1); WebDriver.FindElement(By.XPath("//input[@title='FIRST NAME']")).Clear(); WebDriver.FindElement(By.XPath("//input[@title='FIRST NAME']")).SendKeys("Tony" + RandomNumber()); WebDriver.FindElement(By.XPath("//input[@title='LAST NAME']")).Clear(); WebDriver.FindElement(By.XPath("//input[@title='LAST NAME']")).SendKeys("Stark" + RandomNumber()); WebDriver.FindElement(By.XPath("//a[text()='Finish']")).Click(); System.Threading.Thread.Sleep(5000); WebDriver.FindElement(By.XPath("//a[text()='Customer Info']")).Click(); Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, "Customer Created Successfully"); System.Threading.Thread.Sleep(5000); status = "Pass"; } catch (Exception e) { endTime = DateTime.Now.ToString(); status = "Fail"; ErrorLog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, e.StackTrace.ToString()); } if (status.Equals("Pass")) { status = "Pass"; } else { status = "Fail"; } endTime = DateTime.Now.ToString(); Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, status); addSummary(startTime, endTime, description(this.GetType().Name), module(this.GetType().Name), this.GetType().Name, status); } else { endTime = DateTime.Now.ToString(); Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, "Not Executed"); addSummary(startTime, endTime, description(this.GetType().Name), module(this.GetType().Name), this.GetType().Name, "Not Executed"); } } } }
C# Important Functions:
Important namespaces used:
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; using Microsoft.Office.Interop.Excel; using System.Drawing; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Support.UI; using System.Threading; using System.Configuration; using System.Globalization;
Function to Create Result:
public void createResult(string StartTime) { Microsoft.Office.Interop.Excel.Application TestLog = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook TestLogFile = TestLog.Workbooks.Add(); Microsoft.Office.Interop.Excel.Worksheet Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Logs.Name = string.Format("Test Results"); Logs.Cells[1, 1] = "Test Execution Results – (ABC)"; Logs.Cells[1, 6] = string.Format("Start Time: {0}", StartTime.ToString()); Logs.Cells[3, 1] = "Test Module"; Logs.Cells[3, 2] = "Test Case Id"; Logs.Cells[3, 3] = "Test Case Description"; Logs.Cells[3, 4] = "Start Time"; Logs.Cells[3, 5] = "End Time"; Logs.Cells[3, 6] = "Result"; Microsoft.Office.Interop.Excel.Range R1 = Logs.get_Range("D1").EntireColumn; R1.NumberFormat = "MM/dd/yyyy hh:mm:ss AM/PM"; Microsoft.Office.Interop.Excel.Range R2 = Logs.get_Range("E1").EntireColumn; R2.NumberFormat = "MM/dd/yyyy hh:mm:ss AM/PM"; Logs.get_Range("A1", "E1").Interior.Color = System.Drawing.Color.SkyBlue; Logs.get_Range("A3", "F3").Interior.Color = System.Drawing.Color.Gainsboro; TestLogFile.SaveAs(@"C:\TestResults.xls"); TestLogFile.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(Logs); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLogFile); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLog); }
Function to add summary:
public void addSummary(string StartTime, string EndTime, string description,string TestPlanId, string TestCaseId, string Outcome) { Microsoft.Office.Interop.Excel.Application TestLog = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook TestLogFile = TestLog.Workbooks.Open(@"C:\TestResults.xls");//.Add(); Microsoft.Office.Interop.Excel.Worksheet Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range range = Logs.UsedRange; int colcount = range.Columns.Count; int rowcount = range.Rows.Count; Logs.Cells[rowcount + 1, 1] = TestPlanId; Logs.Cells[rowcount + 1, 2] = TestCaseId; Logs.Cells[rowcount + 1, 3] = description; Logs.Cells[rowcount + 1, 4] = string.Format("{0}", StartTime.ToString()); Logs.Cells[rowcount + 1, 5] = string.Format("{0}", EndTime.ToString()); Logs.Cells[rowcount + 1, 6] = Outcome; TestLogFile.Save(); TestLogFile.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(Logs); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLogFile); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLog); }
Function for Test Case End Time:
public void endTimefunction(string EndTime) { Microsoft.Office.Interop.Excel.Application TestLog = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook TestLogFile = TestLog.Workbooks.Open(@"C:\TestResults.xls");//.Add(); Microsoft.Office.Interop.Excel.Worksheet Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range range = Logs.UsedRange; Logs.Cells[2, 6] = string.Format("End Date Time: {0}", EndTime.ToString()); TestLogFile.Save(); TestLogFile.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(Logs); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLogFile); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLog); }
Function for Test Case Start Time:
public void startTimefunction(string startTime) { Microsoft.Office.Interop.Excel.Application TestLog = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook TestLogFile = TestLog.Workbooks.Open(@"C:\TestResults.xls");//.Add(); Microsoft.Office.Interop.Excel.Worksheet Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Logs = (Worksheet)TestLogFile.Worksheets.get_Item(1); Microsoft.Office.Interop.Excel.Range range = Logs.UsedRange; Logs.Cells[1, 6] = string.Format("Start Date Time: {0}", startTime.ToString()); TestLogFile.Save(); TestLogFile.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(Logs); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLogFile); System.Runtime.InteropServices.Marshal.ReleaseComObject(TestLog); }
Function for Execution Log:
public void Executionlog(string Time,string TestPlanId, string TestCaseId, string Message) { string filePath = @"C:\TestResults"; string file = string.Format(@"{0}\TP{1} - ExecutionLog {2}.txt", filePath, "Automation", DateTime.Now.ToString("MM-dd-yyyy")); FileInfo logFile = new FileInfo(file); StreamWriter log = logFile.AppendText(); string logMessage = string.Format(">> {0},TM: {1}, TC: {2}, {3} \n", Time,TestPlanId, TestCaseId, Message); log.WriteLine(logMessage); log.Flush(); log.Close(); }
Function for Error Log:
public void ErrorLog(string Time,string TestPlanId, string TestCaseId, string Message) { string filePath = @"C:TestResults"; string file = string.Format(@"{0}\TP{1}-TC{2} - ErrorLog {3}.txt", filePath, "Automation", TestCaseId, DateTime.Now.ToString("MM-dd-yyyy"));
FileInfo logFile = new FileInfo(file); StreamWriter log = logFile.AppendText(); string logMessage = string.Format(">> {0},TM: {1}, TC: {2} {3}\n",Time, TestPlanId, TestCaseId, Message); log.WriteLine(logMessage); log.Flush(); log.Close(); }
Function to read data from data file:
public string dataFile(string input) { Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; Microsoft.Office.Interop.Excel.Range xlrange; string URL="URL"; string UserName="UserName"; string Password="Password"; string result=""; int xlRowCnt = 2; xlApp = new Microsoft.Office.Interop.Excel.Application(); //Open Excel file xlWorkBook = xlApp.Workbooks.Open(@"C:\Data Sheet.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlrange = xlWorkSheet.UsedRange; //int abc = 1; if (input.Equals(URL)) { result = (string)(xlrange.Cells[xlRowCnt, 1] as Microsoft.Office.Interop.Excel.Range).Value2; } else if (input.Equals(UserName)) { result = (string)(xlrange.Cells[xlRowCnt, 2] as Microsoft.Office.Interop.Excel.Range).Value2; } else if (input.Equals(Password)) { result = (string)(xlrange.Cells[xlRowCnt, 3] as Microsoft.Office.Interop.Excel.Range).Value2; } xlWorkBook.Close(); return result; }
Function for Controller:
public string module(string TestCaseId) { Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; Microsoft.Office.Interop.Excel.Range xlrange; string xlString; string module = ""; int xlRowCnt = 0; xlApp = new Microsoft.Office.Interop.Excel.Application(); //Open Excel file xlWorkBook = xlApp.Workbooks.Open(@"C:\Controller.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlrange = xlWorkSheet.UsedRange; //int abc = 1; for (xlRowCnt = 1; xlRowCnt <= xlrange.Rows.Count; xlRowCnt++) { xlString = (string)(xlrange.Cells[xlRowCnt, 1] as Microsoft.Office.Interop.Excel.Range).Value2; if (xlString.Equals(TestCaseId)) { module = (string)(xlrange.Cells[xlRowCnt, 3] as Microsoft.Office.Interop.Excel.Range).Value2; } } xlWorkBook.Close(); return module; }
Function to read write from excel:
public string excel(string TestCaseId) { Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; Microsoft.Office.Interop.Excel.Range xlrange; string xlString; int xlRowCnt = 0; xlApp = new Microsoft.Office.Interop.Excel.Application(); //Open Excel file xlWorkBook = xlApp.Workbooks.Open(@"C:\Controller.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlrange = xlWorkSheet.UsedRange; for (xlRowCnt = 1; xlRowCnt <= xlrange.Rows.Count; xlRowCnt++) { xlString = (string)(xlrange.Cells[xlRowCnt, 1] as Microsoft.Office.Interop.Excel.Range).Value2; if (xlString.Equals(TestCaseId)) { mode = (string)(xlrange.Cells[xlRowCnt, 4] as Microsoft.Office.Interop.Excel.Range).Value2; //Console.WriteLine(mode); } } xlWorkBook.Close(); return mode; }
Function to add description to result file:
public string description(string TestCaseId) { Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; Microsoft.Office.Interop.Excel.Range xlrange; string xlString; string desc=""; int xlRowCnt = 0; xlApp = new Microsoft.Office.Interop.Excel.Application(); //Open Excel file xlWorkBook = xlApp.Workbooks.Open(@"C:\QQ Catalyst Project\Controller Sheet\Controller.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlrange = xlWorkSheet.UsedRange; for (xlRowCnt = 1; xlRowCnt <= xlrange.Rows.Count; xlRowCnt++) { xlString = (string)(xlrange.Cells[xlRowCnt, 1] as Microsoft.Office.Interop.Excel.Range).Value2; if (xlString.Equals(TestCaseId)) { desc = (string)(xlrange.Cells[xlRowCnt, 2] as Microsoft.Office.Interop.Excel.Range).Value2; } } xlWorkBook.Close(); return desc; }
end...
Subscribe to:
Posts (Atom)