Identifying web objects in TestComplete

TestComplete provides you with several search methods that let you find certain object on the tested web page. Following are the methods available in TestComplete to identify the web objects in the test page

1. Using Common Find Methods
2. Using Specific Find Method
3. Using XPath Expressions
4. Using CSS Selectors

Here we're talking about the method 2, Using Specific Find Method. Its syntax is
TestObj.NativeWebObject.Find(PropertyName, KeyValue)
TestObj - Page to be tested
Property Name - Property Name of the control
KeyValue - Property Value of the object

Lets look how we can do this, In previous post we've created the first script using TestComplete, We can add below task in the same script which help to learn NativeWebOject method

Task details
Task 1. Identify google search text box and enter keyword.
Task 2. Identify google search button and perform button click.

To identify the object properties, TestComplete provide the spy button which can be invoked by click the  Display Object Spy button on the Tools toolbarDrag the target glyph to the desired object.
Here first place on it search box and identify the property name and value, as shown below. Identify a unique property of both controls, here we're selecting ObjectIdentifier, 

Here is code snippet,
function Main() {
Browsers.Item("firefox").Run("https://www.goole.com");
page=Sys.Browser("firefox").Page("https://www.goole.com");
page.wait();

if(page.exists)
 {
   //Task 1
   var searchTxt = page.NativeWebObject.Find("ObjectIdentifier","lst_ib")
   searchTxt.setText("car")
   
   //Task 2
   var searchBtn = page.NativeWebObject.Find("ObjectIdentifier","btnK")
   searchBtn.Click()

   Log.Message("URL has been successfully loaded")
 }
else
 {
   Log.Message("Failed to load the URL")
 }
}
Check the Task 1, identified google search box using ObjectIdentfier property and its value and entered keyword with setText() method. In Task 2, identified in the same way and clicked on the button using click() method.

Creating first script in TestComplete

We've created TestComplete first project in previous post and lets create first script. Before that we can have an idea about scripting languages supported in TestComplete. Following languages are supported in TestComplete
  1. JavaScript
  2. JScript
  3. Python
  4. VBScript
  5. DelphiScript
  6. C#Script
  7. C++Script
In this tutorial, we've selected JavaScript, since it is very simple and easy to understand.

Hope you all created the project as per the previous postOkay.. Now we can automate a simple task in TestComplete using JavaScript

Task details
1. Launch web browser
2. Navigate to test url
3. Ensure that page has been loaded successfully, here is the code. Copy below code snippet the script work space and click on green run button.
function Main() {
Browsers.Item("firefox").Run("https://www.goole.com");
page=Sys.Browser("firefox").Page("https://www.goole.com");
page.wait();

if(page.exists)
 {
   Log.Message("URL has been successfully loaded")
 }
else
 {
   Log.Message("Failed to load the URL")
 }
}
You can see that Firefox browser get launched and navigated to google url mentioned in that code. If the page has been successfully loaded then page.exists should be 1. This condition has been considered the if clause. You can see that execution log in project pane

Best practices in JMeter

We need to follow best practices in JMeter before running our performance test suite. Performance testing is a must-do task for the application development or infrastructure benchmark. In order to achieve the best results take care below best practices

1. Master-slave setup of load test should use to get better and realistic results. Use cloud servers such AWS geo located servers are the best choice to measure the realistic performance data

2. Always use non-GUI mode to run the test suite and it reduces the useage of system resources.

3. Reduce the usage of listener and assertion modules, always check JMeter logs files to verify the results 
4. Limit the number of threads 

5. Maintain proper system resources for the performance test machine 

6. Clean the temporary files before execution.

7. Always implement realistic load conditions and repeat the test multiple times  

How to use assertions in JMeter

Assertion in JMeter is used to validate response of the request. You can use them to run against a sample to ensure returned values match expected results.Following are some commonly used assertion in JMeter:

1. Response Assertion
2. Duration Assertion
3. Size Assertion
4. XML Assertion
5. HTML Assertion

All assertions consume system resources like memory and CPU, it may adversely affect the performance values. Response Assertion and Duration Assertion are preferable choice as it consumes very less system  resources. Here we're talking about Response Assertion. Lets look into its details

Response Assertion
The most commonly used assertion is the Response Assertion. It checks whether web response contains, matches, or equals a specified pattern.Response Assertion can be added in following way Thread Group >> Assertions >> Response Assertion. We can add Response Assertion in parameterization and lets verify the url is valid or not. Select Response Code radio button and enter the web response to be verified as 200, refer below figure

jmeter-response-assertion

To verify the assertions results, we need to add Assertions Results in listener. It can add in a following way, Thread Group >> Listener >> Assertion Results. All requirements have been added and lets click on the green button and verify the results

jmeter-assertions-execution

Understand and analyze performance report in Jmeter

Listeners are the JMeter component that displays test results.Listeners capture the response coming back from Server while Jmeter runs.

In earlier post we've created the listener,  View Results Tree which shows a tree of all sample responses, allowing you to view the response for any sample. These are other kinds of listener available for JMeter as shown below
jmeter-listeners

Today we're discussing about summary report which is one of the easily digestible report in JMeter. We can add Summary Report in following way Thread Group >> Add >> Listener >> Summary Report. Lets add summary report in your test plan for parameterization and run it. You can see the results like as shown below. We can discuss about it each item one by one 

jmeter-summary-report-table

Average
It is the average time taken by all the samples to execute specific label

Min
The shortest time taken by a sample for specific label

Max
The longest time taken by a sample for specific label

Std. Dev
It gives you a good idea of the dispersion or variability of the measures to their mean value.If the deviation value is low compared to the mean value, it will indicate you that your measures are not dispersed.

Error%
Percentage of Failed requests per Label.

Throughput
Throughput is the number of request that are processed per time unit second by the server. This time is calculated from the start of first sample to the end of the last sample. Larger throughput is better.

Data transfer /Sec
This indicates the amount of data downloaded from server during the performance test execution. In short, it is the Throughput measured in Kilobytes per second.

Lets look into these values after your performance test and decide the performance your application.

How to do Parameterization in JMeter using CSV

Consider you've got a task like to verify the HTTP status of 1000 urls, either you can verify it in a manual way or use any tool. JMeter parameterization is the quickest way to get the results. Parameterization in JMeter is the process where test plans are executed with multiple sets of data. Here is your test urls

jmeter-url-list
As a first step, create test plan with Thread group, Sampler and Listener, follow the link to create test plan

Using CSV Data Set Config we can achieve parameterization, CSV Data Set Config is used to read all parameter values from a CSV File, store them in variables and use them as Test Data during Execution.

You can add CSV Data Set Config in a following way, Thread group-> Add->Config Element -> CSV Data Set Config. Now we've to create CSV file with parameter values as shown below and save it into any directory

jmeter-csv-data

As a next step we need to configure few values in CSV Data Set Config section. Lets configure it as shown in below figure

jmeter-csv-data-config

Next step is configuring url in sampler and map the csv variable in sampler variable section as shown in below figure 

jmeter-csv-data-sampler
Now we've configured all requirements of csv parameterization. Save the test plan and add proper thread users and lets click on the green play button. You can see that in listener as requests requests are being parameterized

Running first test in JMeter

Yes... you've created first test plan in JMeter, Lets look into its execution. As an example you need to load your web application with 5 users and see the results. Here we go.

As a first step we need to set 5 users in thread group, Click on the thread group and set value 5 in Number of Threads (users) field, as shown below

jmeter-thread-group

As a second step, We need set the url to be tested in sampler element. As shown below, first define the protocol whether it is http or https. Default value is http. Specify the url in the next field as shown in below.

sampler-configuration

Yes... now everything set. It is ready for execution. Before that, lets save the test plan to any directory and its file extension is .jmx file. Select the thread group and click on the green play button. You can see that JMeter is running and generated results in View Results Tree element. View Results Tree element lists all requests individually and display the web response in response data tab. 

jmeter-execution
Here you completed the execution of JMeter simple web test plan.

Build first test plan in JMeter

JMeter test plan is the root node and consists different elements such as threads, sampler, listener etc. On executing test plan each elements will be executed as its order. Test plan defines overall settings of the test you want to execute in JMeter

Here we're talking about simple web test plan which consists following elements. As a first step launch jmeter application, follow the link JMeter installation to learn about it

Thread group
A thread group is where you specify the number of users that you want to simulate. One thread is equal to one user. Thread group can be added in a following way
jmeter-thread-group
Sampler
Sampler is the main unit in JMeter. Sampler can a HTTP request, FTP request or JDBC request. JMeter executes the samplers and generate the results. In this post we're creating a HTTP sampler in a following way

jmeter-sampler
Listeners
Meter executes the samplers and writes down the results, such as: start time, duration, success, response messages etc. Listeners  can be used to view these results. There are different kinds of listeners are there like summary report, view results tree, graph results etc. Here we're creating view results tree listener in a following way
view-results-tree

Yes. you've created a HTTP simple test plan in JMeter. 

How to install JMeter

Apache JMeter is one of the leading open-source tool for load and performance testing. JMeter is a pure Java application. Here explains the steps required for installing JMeter in Windows machine

1. Install the latest 64-bit JRE or JDK - Link
2. Download latest JMeter binaries - Link. You can download either zip/tgz and extact the contents into the directory where you want JMeter to be installed.
3. Make sure that following environment variables have been set in your machine
 > JAVA_HOME environment points to the JRE or JDK installation folder
 > JAVA_HOME/bin folder has been added to the PATH environment variable

Next step is how to launch JMeter. Navigate to /bin folder and find JMeter.bat file and double click on it. You can see the JMeter GUI. 


jmeter-gui

This means that JMeter installation has been successfully completed. 

Verify broken links in TestComplete

TestComplete is a functional automated testing tool from SmartBear. Using TestComplete we can automate both desktop, web and mobile applications. It come up with rich features like record and playback, distributed testing, scripted testing etc.

Here we're talking about how to verify the broken links in a web page.

Function VerifyWebObject accepts two arguments,
1. link 
URL to be verified, You can call this function in an iterative model so that we can verify all the urls in a web page
2. expectedObjectData -
Empty value, this value compared with http response data. In switch case you can add other web status to fit your requirements
function VerifyWebObject(link, expectedObjectData)
{
  try
  {
    var httpObj = Sys.OleObject("MSXML2.XMLHTTP");
    httpObj.open("GET", link, true);
    httpObj.send();

    while (httpObj.readyState != 4)
    Delay(100);

    switch (httpObj.status)
    {
      case 200:
      case 302:

      if (httpObj.responseText != expectedObjectData)
      {
        Log.Message("The link is valid "+link);
        return true;
      }
      break;
      default:
       Log.Error("The link is invalid " + link);
        return false;
    }
      return true;
  }
  catch(e)
  {
    Log.Error(e.description+"link is "+link);
  }
}
Here is the Git link of above code.

How to run JMeter in non-GUI mode

Most of you've faced below OutOfMemoryError: Java heap space error during JMeter execution.
jmeter-memory-error

There are multiple ways to resolve it,

1. Increase the Java Heap Size in Jmeter.Bat file.
2. Increase the system RAM to your fit requirements.
3. Running JMeter in non-GUI mode. Yes, we're going to explain about non-GUI mode.

JMeter GUI is basically for recording,scripting and debugging purpose only. While running JMeter in GUI mode, produce high loads and consume more resources in the system which produces unreliable results. So highly recommending non-GUI mode for load testing. Here explains its detailed steps.

1.Take command line.
2.Navigate to JMeter 'bin' directory.
3.Enter below command in command line.
jmeter -n –t test.jmx -l testresults.jtl
-n  non-GUI mode
indicates that it is a non-GUI mode execution.
-t  test script name
We need to specify the name of test script file to be executed. Here test.jmx is the file.
-l  results file name
Name of the generated output file. In the above example testresults.jtl is the name of the output file.  Please find the below screenshot about non-GUI execution.

jmeter-non-gui-execution

Once the execution is over you can see the generated results file in Jmeter 'bin' folder.

Sending mail through PowerShell

Automated email notification is always worth during software automation process. Now a days continuous integration tools come with this feature. If you want to automate a small task with email notification then PowerShell is the best option.

PowerShell is a command-line tool from Microsoft. Using PowerShell we can automate the administrative tasks of windows. You can launch PowerShell from Windows program menu.

Here explains the code snippet for sending email
send-Mail #Function call

#function definition
function send-Mail {
###########Define Variables######## 
$fromaddress = "From address" 
$toaddress = "To Address" 
$Subject = "Subject" 
$attachment = "Attachment Path" 
$smtpserver = "SMTP Server Name" 
#################################### 
 
$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress) 
$message.IsBodyHtml = $True 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment) 
$message.Attachments.Add($attach) 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message) 
}
Modify variables based on your requirement and save it as PowerShell script. Call this script in your test automation suite to get the mail.

Capture web requests using Selenium

Selenium tool is basically using for automating web applications. During test suite execution it is good to capture the web requests so that we can easily figure out the issues observed in execution.

There are two ways to capture the network requests,

1. Through proxy - Capture all web requests through a proxy like Fiddler or Browsermob and parse the log later.
2. Using chrome driver - Enable performance logging in desired capabilities. You can capture performance log, timeline and page events.

Here its detailed steps, First we need to define performance logging in desired capabilities section.

Enable logging in desired capabilities
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
Now all network related logs are capturing and next step is the storing of log data. Here shows its code snippet for storing the data.

Collecting Logs
FileWriter fr=new FileWriter("C:\\data.txt");
BufferedWriter br=new BufferedWriter(fr);
LogEntries lo = driver.manage().logs().get(LogType.PERFORMANCE);
 for (LogEntry le : lo) {      
  br.write(le.getMessage());     
  br.newLine();       
 }
All collected data now available in data.txt file and you can process it based on your requirements.

AB - Performance Benchmark Tool

ApacheBench (ab) is a command tool for measuring the performance of HTTP web servers. ab tool is very basic but it will give you a easily digestible performance related metrics of a web server. It is a single threaded application and it could not provide the detailed performance reports like JMeter or LoadRunner. It can give you

1. At what point will the application break.
2. Maximum number of requests-per-second that my application can handle.
3. Average response time.

ab tool available in Apache MSI-package and grab ab.exe from the Program Files/Apachge Group/bin folder. It is a standalone application.
Here gives an example about ab tool simple command
ab -k -c 200 -n 100000 example.com
-k Keep Alive
Enable the HTTP KeepAlive feature, i.e., perform multiple requests within one HTTP session. It is recommended you do a benchmark with this since browsers are working in a similar way.
-c Concurrency
Number of multiple requests to perform at a time.
-n Requests 
Number of requests to perform for the bench marking session.

In above example 200 simultaneous connections until 100 thousand requests are met with keep alive header.

ab-performance-benchmark-tool