Monday, August 3, 2015

UFT Description and .GetChildObjects | Working with multiple objects with similar or the same set of properties


It is a best practice to make your automation script as Dynamic as possible. Here's a sample when dealing with search results or blog post or list of articles.

In this example, I use the google.com search results.

Scenario:
I'd like to automate the searching capability of google search and click the first search result (not the featured) no matter what title or text it has.

Figure 1.0 Search results in google.com

Tip: You can get the properties of the object either using object spy or developer tools in your browsers.

Figure 2.0 Decription and .ChildObjects
You can also do the loop statement to iterate all the present occurrences of the object.

   
Figure 2.1 Looping the objects' existence

Object indexes acts like arrays, it always starts with '0' index.

Note: You can always maximize your scripts to be suitable for similar layouts as long as the objects are still the same and uses the same hierarchy.
 

Wednesday, July 29, 2015

Visual Studio C# | Converting String to Integer


Values you enter on the text fields are considered strings or series of characters regardless if it is a number or integer.



The sample application above converts string to number and them up then displays it on the label component.

Code:
   The action was triggered upon clicking the 'Add' button.

note:
   variable 'total' is declared as global variable with double type.


Tip:
   set 'FullRowSelect' to true of the list view property so that when you click an item of the list view, it will highlight the entire row.



Visual Studio C# | Allow Integer Input Only


There are certain field that you only want the user to input integer or numbers but there is always that doubt that they will accidentally press a non-numeric characters.

The best way to catch that one is to forbid the user to input a letters, special characters and any other characters that is not needed for your specific field.

For example, you are doing a form for a product and part of it's fields is the 'Price'. We all know  that the price contains numbers and period ONLY.

Add a textfield on your form and named it 'txtPrice'.


Add the following code on your textfield's KeyPress event.



Explanation:

   e.KeyChar : This gets the current value of the key pressed in the keyboard.
   char.IsControl :  returns true if the user presses 'ctrl', 'alt' , etc.
   char.IsDigit : returns true if the user presses a number key.

Logic:

  If pressed key is NOT a control key AND NOT a digit AND NOT a period, then
  SET e.handled to true, which means to cancel the event or that your handling the event in your code instead.

 If pressed key is a period check if the textfields already contains a period, then
 don't let the characted to be pressed. (e.handled = true)


Sources:

  • http://www.homeandlearn.co.uk/csharp/csharp_s8p4.html
  • http://www.dotnetperls.com/char-isdigit
  • http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers

Monday, July 27, 2015

UFT GetROProperty and CheckProperty | Comparing Expected Text with Actual Text


There are times when you need to check if the Expected Text is displayed against Actual Text displayed on the actual applications.

For intance:

You need to check the exact page title description in the www.w3schools.com website.

Figure 1: w3schools.com homepage screenshot

Text that needs to check : 'The language for building web pages."

You need also to consider that this part is a content maintainable. Which means, it is possible that it will change it depends of the content owner.

The best way to do it is you have to place it on your data table.

There are different ways on how to check the text it depends on the availability of the object properties.


GetROProperty Method:

Object.GetROProperty is the property used to get the current property of the Object. This is easier to use if the Object has a unique property that we can identify easily (e.g. html id, class, etc.)


Example:

Let's assume that the page description of the w3schools.com homepage has a unique property.

Object Property (through Oject Spy)
      html id:=page-description

Data Table:
    



Code:
   


Explanation:
   
   Line # 4: Set the row of which row from the data table you want to get.
                   by default, the row setting is always 1.

   Line # 8 & #11: string variable and object variable declaration

   Line # 15 to # 21 :
     Since we already declare the object variable for page description, we can use the 'objPageDescription' variable.

   Check first the existence of the object using the code '.Exist(1)'. The integer parameter of the function 'Exist' represents the number of seconds it needs to wait before it returns a boolean value (true or false)

   In doing so, we are sure that during run time, the code will not encounter error because we made sure of it that we check its existence first before getting its property.

NOTE: This is the most common error issues encountered when using the GetROProperty function. It will throw an error if you try to GetROProperty of an object which is not there (or not exist).

   You can always use the GetROProperty function in printing the exact value on Actual Runtime as a Reporter value and description so that it will be printed in the Results Viewer.


CheckProperty Method:

But what if the the object doesn't have a unique property, which is the most common scenario.

You can always use the 'outertext' property and the datatable value you just declared as Expected Result text.

Example:
 
Object Spy:


Code:
   

Explanation:
   In this way, you can always use the 'outertext' property directly in declaring the object. (see line # 11).
   
   Same logic above, you still need to check the existence of the object before checking it's property.

   CheckProperty method has two parameters, Object.CheckProperty(propertyName,PropertyValue). It returns boolean value (true or false) if the specified property name and value matches the actual status of the Object.


I am not sure though on which of the two is the best method to use, but the two method above works for me depends on the availability of the properties that I need.
   

Sunday, July 26, 2015

Android Studio


Few months ago, I started my android development exercises and trying to understand and learn the android application. Then one of my officemates introduce me to this Google's new application for android development.

For those who didn't know, Eclipse is the most common android development tool which runs by java.

And now, Google releases the 'Android Studio' in May 2013.

I am trying to create my first android app and love to share it for you (beginners).


You download the application on their site.
https://developer.android.com/tools/revisions/studio.html



Friday, July 24, 2015

Visual Studio C# | Communicating with MS Access




One of the important thing when developing an application is to have a data storage where you can store all you important data whether it is from the user or for the user.

This post is all about connecting with MS Access as Database with Visual Studio C# and populate the retrieved data to the ListView component.


//initialize database connection
private OleDbConnection dbConn = new OleDbConnection();

//Connection string
string strConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Elfe Rain.Cisneros\Documents\Visual Studio 2013\Projects\Rental-System-Tool\Rental-System-Tool\bin\Debug\Rental.accdb;
Persist Security Info=False;";

//establish connection string
dbConn.ConnectionString = strConnString;

//Open database
dbConn.Open();

// establish query string
string strQuery = "Select * from Customer";

//Represents an SQL statement or stored procedure to execute against a data source.
OleDbCommand dbCommand = new OleDbCommand();

//Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
OleDbDataReader reader;

//Represents an item in a ListView control.
ListViewItem colItem;

// declaring a string of array with size 4
string[] colDataItem = new string[4];

// as a start, the listview is cleared.
listCustomer.Items.Clear();

            try //catch possible exception occurred.
            {
		//send connection and query
                dbCommand.Connection = dbConn;
                dbCommand.CommandText = strQuery;
                reader = dbCommand.ExecuteReader();
		
		// read all rows returned by the query
                while (reader.Read())
                {
                    colDataItem[0] = reader["CustomerID"].ToString();
                    colDataItem[1] = reader["LastName"].ToString();
                    colDataItem[2] = reader["FirstName"].ToString();
                    colDataItem[3] = reader["MiddleName"].ToString();

                    colItem = new ListViewItem(colDataItem);
                    listCustomer.Items.Add(colItem);
                    listCustomer.GridLines = true;


                } reader.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }

//close database connection
dbConn.Close();

The Result (listview populated):


It's up to your application flow / logic where to place the codes above. It depends on your needs and purposes.

Sources:
 Microsoft Library : https://msdn.microsoft.com/en-us/library 
 Connectoin String : http://www.connectionstrings.com/access/

Wednesday, July 22, 2015

UFT Datatables | Accessing Datatables ~ Tips and Tricks



I've been working with DataTables for quite long time now, and there are instances that you will think of the ways to maximize the use of those rows and columns of the table.

For a usual scenario, you use the DataTable for the sets of data that is used for each iteration of the code like for instance, User Profile, etc.

To have a quick overview of what I am talking about, here's the sample view of a usual usage of the DataTable.

Figure 1.0 Usual way of using DataTable
Assuming your Test Settings are set to 'Run on All Rows', even without looping your code, it will iterate each row until it reaches the last row.

Figure 2.0 File > Setting > Run


However, you can use the DataTable more than the usual scenario same as above. Let's just say, you are running a full website script (one-way) testing and there are a lot of texts involved - not just keywords or one word data.

Given the scenario above, here's what I do if multiple text are being checked and the component is content maintainable.

Figure 3.0 Other way of using DataTable

Note: Select the 'Run One Iteration only' in the Testing Setting when using this kind of approach.
Figure 4.0 : File > Settings > Run

In this way, you are the master of your datatable, wherein you will be the one to instruct your code on which row and column you will be needing in a certain checking you need.