Posts

Showing posts with the label C#

C# Delegates,Action,Func,Lamdas

In this blog, we will discuss Action delegate, Func delegate, and Lamdas. Let's start with a definition of delegate. Delegate: A delegate is a type that safely encapsulates a method, similar to a function pointer in C++.C# delegates are object-oriented, type-safe and secure. The type of delegate is defined by the name of the delegate. Action Delegate: - Action(Of T) delegate to pass a method as a parameter without explicitly declaring a custom delegate.We don't need to declare a delegate. The compiler is smart enough to figure out the proper types. The limitation is that corresponding method action must not return a value. Example to display Fahrenheit and Celsius using Action :-  public static void Main(string[] args) {            Action displayFarenheit = delegate(double celsius)             {                 Console.WriteLine((celsius * 9.0/5.0 ) + 32.0); ...

End to end testing using Protractor .net for Angular application

I really liked .net port (C#) of protractor which is built on top of selenium webdriver for angular based application . Source : https://github.com/bbaia/protractor-net  

In C# ,Compare two collections in unit test

In C# if we want to compare two collections and if elements in the two  collections are not in the same order then use  CollectionAssert.AreEquivalent   method Let's discuss in the example , as shown in the below code , expectedCollection items have Honda,Toyota,Ford and in the actualCollection items are in the different order and have Toyota,Honda,Ford then to compare two collection use CollectionAssert.AreEquivalent method. List expectedCollection = new List { "Honda", "Toyota", "Ford" }; List actualCollection = new List { "Toyota", "Honda", "Ford" }; CollectionAssert.AreEquivalent(Test1, Test2, "Collection Assert failed"); The difference between CollectionAssert.AreEqual and  CollectionAssert.AreEquivalent is that  in CollectionAssert.AreEqual method it will verify the collection items in the same order but in the CollectionAssert.AreEquivalent method can verify the collection items in any ...

C# Appium ScrollTo functionality

As on today, ScrollTo functionality in Appium (dot net binaries) is not working .Instead we need to use Swipe functionality if you want to scroll in the app. Let us discuss about swipe functionality in Appium. I used TouchAction to perform swipe.It needs x and y coordinates and the way i got those coordinates is explained in the sample code. new TouchAction (getDriver()) . press(startx, starty) . waitAction(duration) . moveTo(endx - startx, endy - starty) . release() . perform(); Sample Code: TouchAction act = new TouchAction(WebDriver); Size dimensions = WebDriver.Manage().Window.Size; double screenHeightStart = (dimensions.Height * 0.5); double screenHeightEnd = dimensions.Height * 0.2; act.Press(0, screenHeightStart).Wait(100).MoveTo(0 - 0, screenHeightEnd - screenHeightStart) .Release().Perform(); In the Appium documentation for scrolling ,suggested work around for c# is  Dictionary< string , string > scrollObject = new...

C# Appium Desired Capabilities settings

I will cover Appium setup in another blog . Once the Appium setup is done ,next step is to set DesiredCapabilities for the Appium webdriver. Below is the sample code in C#                 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.SetCapability(MobileCapabilityType.PlatformName, "iOS"); capabilities.SetCapability(MobileCapabilityType.PlatformVersion, "9.3"); capabilities.SetCapability(MobileCapabilityType.DeviceName, "iPhone 6"); capabilities.SetCapability(MobileCapabilityType.NewCommandTimeout,60*3); capabilities.SetCapability(IOSMobileCapabilityType.LaunchTimeout, 60*10); //replace with your app path capabilities.SetCapability(MobileCapabilityType.App, "IOS app path(*.app)");   AppiumDriver = new IOSDriver ( new Uri("http://172.*.*.*:4723/wd/hub" ), capabilities); AppiumDriver.Manage().T...

C# Selenium code to inject JQuery into wepage

 Below is the sample JQueryUtility class in C# selenium code to verify if jQuery is available on the webpage .If it is not available then inject jQuery into the webpage.      class jQueryUtility     {         private void InjectJqueryIfNeeded()         {             if (!jQueryLoaded())                 InjectJquery();         } //Verify if Jquery is loaded or not.         public Boolean jQueryLoaded()         {             Boolean loaded = false;             IJavaScriptExecutor js = WebDriver as IJavaScriptExecutor;             loaded = (bool)js.ExecuteScript("return window.jQuery ");             return loaded;         } //Inject J...

Part 2 series of calling java script code in C# Selenium

Part 2 series of calling java script code in C# Selenium .Below sample code is helpful in synchronization of page loads with Explicit Wait conditions.                          Document Ready State :-              Webdriver.Navigate().GoToUrl("http://www.google.com");             IJavaScriptExecutor js = Webdriver as IJavaScriptExecutor;             string readyState = (string) js.ExecuteScript("return  document.readyState ");             Console.WriteLine("Ready State is {0}",readyState);              Verify if Jquery is available on the webpage  :-             IJavaScriptExecutor js = Webdriver as IJavaScriptExecutor;              jQueryNotLoaded = (bool)js.ExecuteScript("ret...

Execute java script code in Selenium C#

 As we work on automating web pages using selenium , we would stumble up on using java script as a way to fix the clicking on the element or verifying the document ready state to make synchronization to work.  Below are some of the basic example of executing java script in C# selenium web driver.         Browser Title : -            Webdriver.Navigate().GoToUrl("http://www.google.com");             IJavaScriptExecutor js = Webdriver as IJavaScriptExecutor;             string title = (string) js.ExecuteScript("return document.title ");             Console.WriteLine("Title has:{0}",title);        Browser Url :-              Webdriver.Navigate().GoToUrl("http://www.google.com");             IJavaScriptExecutor js = Webdriver as IJavaScriptExec...

Automate HTTP POSTing JSON data using HTTPClient

As part of automating Integration Tests for WebAPI calls ,I used  HTTPClient, Nunit and  Json.NET . In this example i will demonstrate  automating POST WebAPI call ( http://google:6789/api/appointment/book)   and JSON data is posted to the WebAPI call. lets say appointmentId is returned as a response.         [ Test ]         public async Task BookAppointmentsWithValidTimeSlotTest()         {             int sampleLeadTime = 5;                          DateTime currentDateTime = DateTime .Today;             int appointmentMonth = currentDateTime.Month;             int appointmentYear = currentDateTime.Year;             int appointmentDay = currentDateTime.Day + sampleLeadTime ; ...

Automate HTTP Get with query parameters using HTTPClient

As part of automating Integration Tests for WebAPI calls ,I used  HTTPClient, Nunit and Json.NET . In this example  i will demonstrate  automating  GET WebAPI with query parameters like  http://www.google.com/calendar?zip=98007&service=1 and lets say the response is returned in a result JSON objects. Brief explanation of code : Query Parameters are built using  HttpUtility .ParseQueryString () and URI is built using UriBuilder(). The object (say  Calendar class) which we want to verify after de-serialization is inside   result  JSON objects (result : {} )   which is a key,value pair . To access value of the result JSON object we need to do something like data[result] as shown in below code. using System.Net.Http; using System.Web; using NUnit.Framework; using System.Net.Http.Headers; using Newtonsoft.Json; using Newtonsoft.Json.Linq; [ Test ]   public async Task GetCalend...

C# Selenium WebDriver Wait conditions

Thoughts on WebDriver Wait conditions -  When we are automating a web page ,we quickly come across a situation where we are navigating to a next page and want to access the first web element on the next page or when there is a dependency on two web elements ,some times you need to wait before accessing dependent web element.  In below section we will see how we can handle Wait conditions in Selenium WebDriver. In Selenium WebDriver , there are two different wait conditions. 1)Implicit wait condition 2)Explicit wait condition 1)Implicit wait condition : Implicit wait time is used when you want to configure the webdriver's wait time as a whole for the application under test. Sample code in C#: (highlighted portion in yellow is the code related to wait) Webdriver w ebDriverInstance = new  FirefoxDriver(); WebDriverInstance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); Assert.AreEqual("Blinds | Window Blinds and Shades | Custom Window...