Friday, May 17, 2019

Introduction - NUnit



NUnit is a unit testing framework for .NET. It is the most used framework for writing unit test cases.

What is Unit testing :  


Every software is composed of various modules. Each module is composed of various classes. Classes composed of various functions. Function is the smallest unit of code in the application.

When we test individual function behavior without touching any other functions and determine whether it works exactly as per the requirements or not that is called Unit Testing.


Some of the advantages of Unit Testing:

  1. Defects found early in development life cycle
  2. Reliable Code
  3. Maintainable code
  4. Faster testing by only single click of action

 Test Runners :
it is a UI tool which actually run NUnit test cases and show the result of test cases whether they are passed or failed. We'll learn about test runners in Environment Setup in next post.

                                            Nunt  = Attributes + Assertion  

 

Unit has two different ways to run your tests.

 console runner : nunit-console.exe, is the fastest to launch, but is not interactive.
 gui runner :  nunit.exe, is a Windows Forms application that allows you to work selectively with your tests and provides graphical feedback

 

 

Tuesday, May 14, 2019

Short Cut Keys in Coded UI

The Keyboard class provides different methods with which you can send texts or key codes to the UI element currently selected. Some of these methods also allow the use of modifier keys like Ctrl or Alt.

Keyboard.SendKeys(“Message{ENTER}”);
Keyboard.SendKeys(“{F4}”, ModifierKeys.Alt);
Keyboard.SendKeys(“{F4}”, ModifierKeys.Ctrl);

Various others Shortcut Keys are as follows:-

Key Code
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC} (reserved for future use)
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
Keypad add {ADD}
Keypad subtract {SUBTRACT}
Keypad multiply {MULTIPLY}
Keypad divide {DIVIDE}
To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes.
Key Code
SHIFT +
CTRL ^
ALT %
To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".
To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means press the LEFT ARROW key 42 times; {h 10} means press H 10 times.



















































Coded ui tutorial

Monday, May 13, 2019

Challenges in Coded UI Test

VS 2010 Coded UI Test (Automation Testing) – Challenges



We started testing automation a month ago using VS 2010 and faced some challenges while implementing it. Automation testing is basically a record and playback. That means recording testing activities such as typing URL, clicking button, navigation etc and then verifying in the playback mode. This can be done in 2 ways Web UI Test and Coded UI Test. Coded UI Testing is better since it will record all actions including scripts (JavaScript/VB script).

There are few good articles in basic steps to test using Coded UI test and it’s good to start with. 
(http://www.dotnetfunda.com/articles/article1241-coded-ui-test-basic-walkthrough.aspx)
Those were all good till it’s applied in real time applications with lots of controls, popup windows, AJAX etc and soon found out that very little help is available online. The reason is people have just started using this instead of other automation tools like Winrunner. 

The basic idea behind this article is to list the challenges faced in which some of them are still vague.

1) Parameterising Radio-button list (iteration)

CUI Test was okay for controls like Text box, Dropdown, Buttons etc. In the background, the record engine considers each control as window and they will be added to the list of controls to be validated while playback.

The radio button list will be rendered as separate radio buttons (with separate ID) in the html and hence only selected option will be added to the playback control list. Now here’s the problem – there’s a RB list with 5 values and I am clicking 3rd option while recording. A parameter CSV file is created for iteration in which I want to select 4th value and this was not possible since its not available in the list of controls.

 2) Hyperlink with dynamic numbers (iteration)

There’s grid which shows list of records (say list of employees) and the 1st column is a hyperlink which shows employee ID to edit the record. The recording is performed by searching for employee, clicking a hyperlink; editing the employee and saving it (say record ID 10). Now iterate this test using CSV file with some other record ID (say 15). This was throwing error since the control ID will be stored based on the ID selected and it will not exist in the playback.

Solution – Permanent solution was not found. So did a workaround to add key press event for TAB in the code file.

SendKeys.sendWait(“{TAB}”);
SendKeys.sendWait(“{ENTER}”);

3) Controls with multiple values (Iteration)

We use CSV file with each values separated by ‘,’ for each controls and ‘Enter’ key for next iteration. This will work for controls which have single value (Textbox, Dropdown etc) and created a problem for controls like List-box and checkbox.

4) Action takes longer duration

Let’s take the same scenario listed in 2 and assume it takes 1-2 seconds to search for records. In the playback mode, it might take longer duration than the recording and second action might overlap (say editing). This will throw run-time error since the control is not ready yet (window not found exception)

Solution (available in MSDN) - There’s a property to wait for completion of each action and this might solve the issue (can set for entire test). But the playback becomes very slow and it was taking twice the time.

// Set the playback to wait for all threads to finish
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads; 
// Press the submit button
this.UIMap.ClickSubmit(); 
// Reset the playback to wait only for the UI thread to finish
Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.UIThreadOnly;

5) CUI Test in Virtual environment

We tried CUI Test in virtual VMWare machine with Widows 7 OS using VS 2010. But the actions are not even recorded in virtual environment though the system has all specified configuration. It was strange since the virtual machine should behave like physical theoretically. We could not find any online help why the actions were not recorded. 

Solution – Finally we solved the issue by migrating to virtual machines with writable storage :- C drive will not be reloaded on new session. But the bottleneck in this approach is that our company has to pay lot of extra money since writable VM storage is costlier than read-only one.

6) Recording more than 1 modal popup window

This was another issue. We have 2 modal window to perform some actions (say adding department, add employee) in one screen. The funny thing is, it will work for 1st window and throws error while playback for 2nd window. The playback will wait for a long time and throws exception saying the control is not found or cannot perform the action.

Solution (available in MSDN) – When UI Test framework searches for second window; it will search for cached reference and will fail. There’s a workaround to find the window

this.UIDialogABCDWindow.Find();

This code should be added in the code file (UIMAP.cs) before action on the second window (Close/OK)

7) Schedule CUI Test script

It’s not always possible and needed to use Visual Studio for running Coded UI Test code. In practical scenario, the test should run automatically and give results on a regular interval (say daily once). Coded UI test will generate a DLL as output and this should be used in the Scheduled task

Solution – There’s a command line option to run CUI test and this can be added in BAT file, which can be used in Scheduled task. 

Cd C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE (path of VS 2010)
mstest /testcontainer:D:\TestRun\CUTest.dll (path of CUI Test output Dll)

Note –  The above command can be used to execute all tests created in the project (sorted by name) and to specify particular test use"/testcontainer:D:\TestRun\CUTest.MyTest1". Command "mstest -help" will list all command line arguments. Also the screen should not be locked and screen saver should be turned off to run scheduled tests without error.

The End

Above are the challenges faced while using VS 2010 for automation testing.  If you have found a better solution or better ways to solve them, you are welcome to add comments which might help in future. Happy testing..!

Wednesday, May 1, 2019

Array Sorting

int[] arr = { 4, 1, 9, 7 };

            arr = (from a in arr orderby a ascending select a).ToArray();

           

            foreach (int i in arr)

            {

                Console.WriteLine(i);
            } 

Execution with Multiple Browsers (Cross Browser testing)

Friday, April 26, 2019

Why Thread.Sleep is bad Practice


  1. The thread time slice varies from OS to OS and processor to processor. This means that once your thread goes to sleep, it’s going to wait for its turn to come back into the scheduler. So it’s highly likely that the thread.sleep time means more than what you really intended. For example, with thread.sleep(1000), you intended 1,000 milliseconds, but it could potentially sleep for more than 1,000 milliseconds too as it waits for its turn in the scheduler.
  1. Each thread has its own use of CPU and virtual memory. That means our thread acquired its resources (say 1MB virtual memory, some CPU cycles and some context switching cycles), and is simply not using them.
  1. If it’s a foreground thread, it’s preventing the application from exiting as well.
  1. What about Thread.Sleep(0)? This tells the processor that I’m ready to lose the remainder of my time slice, and you may pick up the next thread from the queue. On one hand, it feels good and efficient. But on the other hand, you are also telling the processor that you are better at scheduling than the processor. (I don’t know the side effects this can cause, but I’m not smarter than the processor.)
We are talking about milliseconds when dealing with thread.sleep, but it’s important to understand that the more we use thread.sleep, the more we defer solving the real problem, which is how to handle async in modern web apps.