Sunday 1 August 2021

Type Conversion(Casting) in C#

Type Conversion(Casting) in C# is converting one type of data to another type. It is also known as Type Casting. Type casting has two forms − 

Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.

Explicit type conversion − These conversions are done explicitly by users using the pre-defined functions. 
Explicit conversions require a cast operator. 

Example: 
double d = 5673.74; 
 int i; 
 // cast double to int. 
 i = (int)d;

few more keywords for type casting:
ToBoolean()
ToString()
ToInt32()
ToDouble()
ToDateTime()

RestSharp - Testing a RestFul API service endpoint

Following is the sample run with basic authentication option we have to test RestFul API service endpoint. 

////Json response with simple structure
        [Test]
        public void TestMethod2()
        {
            var client = new RestClient("https://XXXXXX/");

            var request = new RestRequest("api/v4/stb/addition/options/{CustomerId}", Method.GET);
            request.AddHeader("Authorization", "Basic R0xPUFRFU1RcRy5QQUxMQTpUbW9iaWxlNjY3OCE=");
            request.AddUrlSegment("CustomerId", "XLZ20261");

            var response = client.Execute(request);

            //Lib 1 - Dictionary based response
            //var deserialize = new JsonDeserializer();
            //var output = deserialize.Deserialize<Dictionary<string, string>>(response);
            //var result = output["author"];

            //Lib 2 - JSON based response
            JObject obs = JObject.Parse(response.Content);
            Assert.That((int)obs["StbsLeft"], Is.EqualTo(2), "Stbs left are not correct");
        }


////Json response with complex structure
        [Test]
        public void TestMethod3()
        {
            var client = new RestClient("https://XXXXXXX/");

            var request = new RestRequest("api/v4/stb/addition/options/{CustomerId}", Method.GET);
            request.AddHeader("Authorization", "Basic R0xPUFRFU1RcRy5QQUxMQTpUbW9iaWxlNjY3OCE=");
            request.AddHeader("Content-Type", "application/json");
            request.AddUrlSegment("CustomerId", "XLZ20261");

            var response = client.Execute(request);


            //Lib 2 - JSON based response
            JObject obs = JObject.Parse(response.Content);

// below Json response is a complex hierarchy structure, we have to take the path based on the structure
            Assert.That(obs["StbProducts"]["$values"][0]["name"].ToString(), Is.EqualTo("ABCD"), "Stb displayed is not correct");
        }

Visual Studio NUnit tests are not running from Test Explorer but running from ReSharper

You might have noticed NUnit tests you have created are not running from Test Explorer but those are running fine from ReSharper. In order to run NUnit tests, we should have NUnit3TestAdapter nuget package to be installed to run from TestExplore. Please make sure to have that NuGet package installed before running the scripts.

Tuesday 8 June 2021

Multiple ways of clicking on Element with Selenium

We might be suprising sometimes while executing selenium script, though element is getting recognized, it is not clicking on the element. There could be various reasons for that 1. the element is rendering in the DOM which is not loaded but it is availalbe on the page 2. Element is available but it's href link made with # at the end, therefore it won't navigate any reference link But for the 1st reason we can use alternative options and make our selenium script working. Basically there 4 ways available to click on element with selenium. Following are the ways.


1. .Click

2. ((JavascriptExecutor) driver).executeScript("arguments[0].click();", yourelement);

3. yourelement.sendKeys(Keys.RETURN) or .sendKeys(Keys.ENTER)

4. Actions class: Actions actions = new Actions(driver); actions.click(signOnImage).perform();