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();

Monday 5 October 2020

Element recognition with cssSelector in Selenium Automation

 Now a days we are mostly preferring to use cssSelector than XPath. 

CSS selectors perform far better than Xpath and it is well documented in Selenium community. Here are some reasons,

Xpath engines are different in each browser, hence make them inconsistent

IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.

Xpath tend to become complex and hence make hard to read in my opinion

However there are some situations where, you need to use xpath, for example, searching for a parent element or searching element by its text (I wouldn't recommend the later).

If you are testing content then do not use selectors that are dependent on the content of the elements. That will be a maintenance nightmare for every locale. Try talking with developers and use techniques that they used to externalize the text in the application, like dictionaries or resource bundles etc. 

  • It’s faster than XPath.
  • It’s much easier to learn and implement.
  • You have a high chance of finding your elements.
  • It’s compatible with most browsers to date.

Overall there are two circumstances where XPath is markedly slower than cssSelector. But they are easily avoidable.

The performance difference is slightly in favor of css-selectors for non-IE browsers and slightly in favor of xpath for IE browsers.


Following are few options how to make customer CSS.

1. id--> htmltag#id , #id

2. class --> htmltag.classname, .classname, .c1.c2.c3, htmltag.c1.c2.c3...cn

3. parent>childtag


ul#categories

#username

input#username

input.form-control.private-form__control.login-email

input#username.form-control.private-form__control.login-email

input.login-email

.form-control.private-form__control.login-email


4. htmltag[id='value']

//div[@id='test']


input[id='username'] -- css with one attribute

//input[@id='username'] --xpath


input[id='username'][type='email'] -- css with two attributes

//input[@id='username' and @type='email'] -- xpath


5. contains the text in css:

input[id*='user']

input[id*='name']


id = 

test_123

test_345

test_456

input[id*=test_]


6. starting the text in css:

input[id^='user']


7. ending the text in css:

input[id$='name']


8. comma in css:

div.private-form__input-wrapper, input#username


9. first-of-type in css:

ul#categories>li:first-of-type


10. last-of-type in css:

ul#categories>li:last-of-type


11. nth-of-type

ul#categories>li:nth-of-type(1)

ul#categories>li:nth-of-type(14)

ul#categories>li:nth-of-type(3)

ul#categories>li:nth-of-type(n) -- all


12. sibling of element:

div.private-form__input-wrapper + div

div.private-form__input-wrapper+div.private-form__meta

ul#categories>li:nth-of-type(3)+li


13. not operator in css:

input.form-control.private-form__control:not(.login-password)

Sunday 4 October 2020

Area conversion

Note: Please don't use this, I am still working on this to develop the code

Please use following fields to convert your area from one unit to other unit to convert.


Enter Quantity Input
 

 

 

Wednesday 2 September 2020

How to enable Fn key in Windows 10 HP Laptop

 Sometimes it is hard to use Fn key and then use F1-F12 function keys. We have to hold Fn key first and then press F1-F12 keys to have hot keys enabled.

To avoid this issue we can enable Fn key bydefault by using following short cut.

Press Alt + Ctrl + fn + Shift button to enable or disable Fn key


Note: Please follow above sequence, otherwise the solution doesn't work.


This can be achieved through bios setup and enabling hot key mode. But I would recommend to use above short cut to achieve this quickly.


https://amzn.to/3lQnt52


https://amzn.to/355NxSR


https://amzn.to/341xFlg


https://amzn.to/3k58tQ9


https://amzn.to/31cS0Ch


https://amzn.to/2H8IfOf


https://amzn.to/355O7jv


https://amzn.to/341ZdXu


https://amzn.to/3j6r4tS


https://amzn.to/3j0zFhH


Baby Pampers