Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Automation of End-to-End GUI Testing

DZone's Guide to

Automation of End-to-End GUI Testing

This article shows an architecture and some sample coding about end-to-end GUI testing on HP’s Unified Functional Testing (UFT) tool.

· DevOps Zone
Free Resource

“Automated Testing: The Glue That Holds DevOps Together” to learn about the key role automated testing plays in a DevOps workflow, brought to you in partnership with Sauce Labs.

Automation is an important step in modern test scenarios. HP’s UFT is a quite handy tool to test your GUIs, APIs, and multi-tier applications. It provides automated testing solutions for ongoing changes due to technology and processes. It significantly increases software quality even in complex, unstable environments while reducing the cost of the test process. It’s even possible to integrate it with HP’s ALM tool to increase collaboration between developers and testers for more productivity.

Benefits of Test Automation With UFT

  • Allows you to automate test scenarios of GUIs, APIs, and multi-layer applications.

  • Offers powerful tools with a great UI experience.

  • Provides the opportunity to test new technologies with UFT insight technology.

  • Allows you to easily convert manual tests into automated ones

  • Improves test management infrastructure with its powerful integration with HP’s ALM. 

Making an automated test not only saves time but also helps provide a detailed report of successful and failed cases.

In this article, we are going explain an example test scenario in which a campaign will be activated on a GSM line via a web application. We are going to use HP’s UFT (version 12.52) tool for the automation of this test scenario.

The Test Scenario

New Solution and New Test Structure

Create a new solution under File > New > New Solution and give it a proper name for this automation. A solution can contain multiple tests in itself. So, create a test under File > New > Test and give it a proper name.

Action Structure

Actions are abstract layers where you can invoke functions. You can create multiple actions as separate steps in your test. Actions can belong to a single test or they can be called by multiple tests if you declare and implement them as external actions.

You can add an action by clicking right button on the test and then selecting Add > Call To New Action. To add an external action, right click on a test and then select Add > Call To Existing Action. You cannot edit an external action. It is only possible to edit an external action by changing it under its original test. If you are planning to use an external action, make sure that it represents a generic structure for all tests. In our example, we created an external test CAMP_COMMON and we put all our external actions under this test.

Function Structure

Functions are actually code blocks that are called in tests via actions. A function that is called via an action should contain codes related to that action. Functions can be added in a test and can be used in that test or in other tests, like actions. If a function is used in multiple tests, then you should remember that a change in a test will affect all others. You shouldn’t update a function without an impact analysis. 

As a standard, functions are located under C:\Users\XXXX\Documents\Unified Functional Testing. To add a function, right click on a test and select Add > Associate Function Library. You can add and implement all the functions that you need to call for that test. 

Below, you can find some sample codes which activates a campaign on a GSM line.

Actions and Functions for Our Scenario

login_action
gsm_search_action
main_action
form_action
logout_action
fn_login.qfl
fn_navigate.qfl
fn_main.qfl
fn_gsm_search.qfl
fn_confirmationform.qfl
fn_equipmentform.qfl
fn_contractform.qfl
fn_logout.qfl
fn_set_page.qfl
fn_exist.qfl

Content of login_action

SystemUtil.Run "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Const PRP="XXXX" (URL of the web application)
Environment.Value("imageCounter")
fn_navigate(PRP)
fn_login()

The  fn_navigate function sets the URL of the web application in the browser. The fn_login function makes the login operation.

As seen above, we call fn_navigate and fn_login functions are used in login_action.

Content of fn_navigate

Function fn_navigate(url)
Browser("XXXXXXX").Navigate url
Browser("XXXXXXX").Page("XXXXXXX").Sync
End Function

The XXXXX variable, which is passed to the Browser function above, is the type of the browser that will be used during web application tests.

Content of fn_set_page

Dim latestBrowser
Dim latestBrowser2
Dim latestBrowser3
Dim latestBrowser4
Dim latestBrowser5
Dim latestBrowser6
Dim latestBrowser7

Function fn_set_page()
Set ("XXXXXXX").Page("XXXXXXX")
Set ("XXXXXXX").Page("XXXXXXX_2")
Set ("XXXXXXX").Page("XXXXXXX_3")
Set ("XXXXXXX").Page("XXXXXXX_4")
Set ("XXXXXXX").Page("XXXXXXX_5")
Set ("XXXXXXX").Page("XXXXXXX_6")
Set ("XXXXXXX").Page("XXXXXXX_7")
End Function

The fn_set_page function lists the pages that are used in this test. In order to not repeat the page definition in each function, we created the fn_set_page function and called it at the beginning of each function.

Content of fn_login

Function fn_login()
fn_set_page()
  If InStr(Browser("XXXXXXX").Page("XXXXXXX").getROProperty("url"), "XXXX.xhtml") < 1 Then
                latestBrowser.RunScript("document.getElementById('username-input').")
                latestBrowser.RunScript("document.getElementById('password-input').")
                fn_exist latestBrowser.WebElement("xpath:=//*[@]"),"Click",""
                latestBrowser.Sync
                End If
                latestBrowser.WebElement("xpath:=//*[@]").Click
    latestBrowser.RunScript("document.getElementById('XXXX-select-one').")
                wait(2) 
                latestBrowser.WebElement("xpath:=//LI[2]/A[1]/I[1]").Click
                wait(2) 
                latestBrowser2.WebEdit("YYYYYY").Set "XXXXX.XXXXX"
                latestBrowser2.RunScript("jsf.ajax.request('YYYYYY',event,{execute:'YYYYYY ',render:'YYYYYY ','tfBlockType':'blockElement','tfBlockElement':'#YYYYYY-table tbody','javax.faces.behavior.event':'rowSelect'});")
                wait(2)
                latestBrowser2.WebElement("xpath:=//TABLE[@]/TBODY[1]/TR[1]/TD[1]/LABEL[1]/SPAN[1]").Click
                latestBrowser2.WebElement("xpath:=//BUTTON[@]").WaitProperty "visible", "True", 30000
                latestBrowser2.WebElement("xpath:=//BUTTON[@]").WaitProperty "disabled", "0", 30000
                latestBrowser2.WebElement("xpath:=//BUTTON[@]").Click
                latestBrowser2.Sync
                Do
                wait(1)
                Loop While latestBrowser.RunScript("document.readyState") <> "complete"
                latestBrowser.WebElement("xpath:=//A[@]").Click
                latestBrowser.Sync
End Function

 Content of gsm_search_action

fn_gsm_search()

After login_action, we need to search for the GSM line. gsm_search_action calls do the fn_gsm_search function (see details below).

Content of fn_gsm_search

Function fn_gsm_search()
Dim msisdn
Dim searchBoxSetScript
Dim searchBoxSetScript1
msisdn = getTestSubscriber()
fn_set_page()
searchBoxSetScript = "document.getElementById('search-content-box')."
latestBrowser3.RunScript(searchBoxSetScript)
latestBrowser3.Link("GSM No").Click
latestBrowser6.WebEdit("searchValue").Set "" & msisdn & ""
latestBrowser6.WebButton("Ara").Click
latestBrowser6.Sync
If Browser("XXXXXXX").Page("XXXXXXX_3").Link("Campaign Activation").Exist(120) Then
Browser("XXXXXXX").Page("XXXXXXX_3").Link("Campaign Activation").Click
Reporter.ReportEvent micPass, "Campaign Activation page is found", " Campaign Activation page is found"
Else
                Reporter.ReportEvent micFail,"Campaign Activation page is not found...","Campaign Activation page is not found.."
End If
End Function

Content of main_action

fn_main()

main_action opens the related campaign web page and starts the activation of the process.

Content of fn_ main

Function fn_main()
fn_set_page()
If latestBrowser3.WebEdit("itKeywordFilter-input").Exist(120) Then
latestBrowser3.RunScript("document.getElementById('itKeywordFilter-input').")
Else
                Reporter.ReportEvent micFail,"Campaign Search Textbox is not found...","Campaign Search Textbox is not found..."
               ExitTest
End if
wait(2)
latestBrowser3.RunScript("jsf.ajax.request('btnFilterOffers',event,{execute:'itKeywordFilter somContractType somSuggestionType ',render:'ccCampaignsTable ','tfBlockType':'blockScreen','javax.faces.behavior.event':'action'});")
wait(60)
latestBrowser3.RunScript("document.getElementById('btnShowSelectedCampaign').click()")
latestBrowser3.Sync
latestBrowser3.WebElement("xpath:=//*[@]/tbody/tr[1]/td[1]/label/span").Click
latestBrowser3.RunScript("document.getElementById('btnOfferSave').click()")
End Function

Once the campaign page is opened and the activation is started, a user needs to fill in three more forms in order to successfully complete the activation process. These forms (in order) are Confirmation Form, Contract Form, and Equipment Form. We define another action, form_action, to call these forms.

Content of form_action

DataTable.AddSheet("parametre")
DataTable.ImportSheet "[ALM\Resources] Resources\XXXXX\uft_parameters", "Page1", "parameter"
Dim EQUIPMENT
Dim TARIFF
.GetSheet("parameter").GetParameter("parameter").ValueByRow(1)
.GetSheet("parameter").GetParameter("parameter2").ValueByRow(1)
fn_confirmation_form()
fn_contract_form EQUIPMENT,TARIFF
fn_equipment_form() 

We read function parameters from an excel via the DataTable function. These parameters then passed to the fn_contract_form function.

Content of fn_confirmation_form

Function fn_confirmation_form()
fn_set_page()
if latestBrowser6.WebElement("xpath:=//*[@]/fieldset/div/ol/li/label/span[text()= Confirmation GSM:']").Exist(120) Then
Reporter.ReportEvent micPass, "Confirmation Gsm text field is found", "Confirmation Gsm text field is found..!!!"
Else
                Reporter.ReportEvent micFail,"Confirmation Gsm text field is not found...","Confirmation Gsm text field is not found..."
End If
latestBrowser6.Link("Please select").DoubleClick
latestBrowser6.WebElement("xpath:=//*[@]/fieldset/div/ol/li/label/span[text()= 'to be confirmed GSM No:']").Click
latestBrowser6.WebElement("xpath:=//*[@]/a").Click
wait(2)
latestBrowser6.WebElement("xpath:=//*[@]/li/label[2]/span[text()= 'Document confirmation']").Click
wait(2)
latestBrowser6.RunScript("document.getElementById('otpConfirm:printDocumentButton').click()")
wait(2)
latestBrowser6.RunScript("jsf.ajax.request('otpConfirm:printDocumentButton',event,{execute:'@this ',render:'otpConfirm:buttonRow ','tfBlockType':'headerAnimation','javax.faces.behavior.event':'action'});")
wait(2)
latestBrowser6.WebElement("WebElement_4").Click
latestBrowser7.WebElement("xpath:=//*[@]").Click
latestBrowser7.Sync
End Function

Content of fn_contract_form

Function fn_contract_form(EQUIPMENT,TARIFF)
fn_set_page()
If latestBrowser7.WebElement("xpath:=//*[@]").Exist(120) Then
                Reporter.ReportEvent micPass, "Contract Page is found", "Decouple Offers Panel is found..!!!"
Else
                Reporter.ReportEvent micFail,"Decouple Offers Panel is not found...","Decouple Offers Panel is not found..."
                ExitTest
End If
wait(2)
latestBrowser7.WebElement("xpath:=//*[@]/div[2]/label/span[text()= 'Duration']").Click
latestBrowser7.WebElement("xpath:=//*[@]/a").Click
wait(3)
'Browser("XXXXXXX").Page("MAYA Responsive_7").RunScript("jsf.ajax.request('j_id_3h:0:offer_',event,{execute:'@this ',render:'ccDecoupleOffersWindowFields ','tfBlockType':'blockScreen','javax.faces.behavior.event':'change'});")
If latestBrowser7.WebElement("xpath:=//*[@]//span[text()=  '" &EQUIPMENT&"' ]").Exist(30) Then
wait(1)
                latestBrowser7.WebElement("xpath:=//*[@]//span[text()=  '" &EQUIPMENT&"' ]").Click
Else
                Reporter.ReportEvent micFail , "EQUIPMENT is not found!", "EQUIPMENT is not found!"
                ExitTest
End If
wait(3)
If  latestBrowser7.WebElement("xpath:=//*[@]//span[text()=  '" &TARIFF&"'  ]").Exist(30) Then
wait(1)
  latestBrowser7.WebElement("xpath:=//*[@]//span[text()=  '" &TARIFF&"'  ]").Click
Else
                Reporter.ReportEvent micFail , "Tariff is not found!", "Tariff is not found!"
                ExitTest
End If
wait(1)
latestBrowser7.RunScript("document.getElementById('btnDetailOfferSave').click()")
End Function

Content of fn_equipment_form

Function fn_equipment_form()
fn_set_page()
wait(1)
fn_exist latestBrowser6.Link("Please select"),"DoubleClick",""
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/div/div/div/fieldset/div/ol/li[3]/label/span[text()= 'EQUIPMENT COLOR']"),"DoubleClick",""
wait(1)
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/a"),"Click",""
wait(2)
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/div/div/div/fieldset/div/ol/li[4]/label/span[text()= 'DISTRIBUTION CODE']"),"DoubleClick",""
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/a"),"Click",""
wait(5)
If  latestBrowser6.WebElement("xpath:=//div[@]//p[contains(text(), 'Vision')]").Exist(15)  Then
                Reporter.ReportEvent micPass, "A notification is found for distributor", "A notification is found for distributor..!!!"
                  'Browser("XXXXXXX").Page("MAYA Responsive_6").WebElement("WebElement").Click 
End If
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/div/div/div/fieldset/div/ol/li[10]/label/span[text()= 'Ship equipment to customeri?']"),"DoubleClick",""
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/a"),"Click",""
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/div/div/div/fieldset/div/ol/li[11]/label/span[text()= Activate Contract Immediately']"),"DoubleClick",""
fn_exist latestBrowser6.WebElement("xpath:=//*[@]/a"),"Click",""
wait(2)
fn_exist latestBrowser6.WebEdit("pseMainPanel_mainTabView:TXT_1"),"Set","1111111111"
fn_exist latestBrowser6.WebEdit("pseMainPanel_mainTabView:TXT_2"),"Set","Regression  Test"
fn_exist latestBrowser6.WebEdit("pseMainPanel_mainTabView:TXT_3"),"Set","Regression  Test"
fn_exist latestBrowser6.WebEdit("pseMainPanel_mainTabView:TXT_4"),"Set","11111"
latestBrowser7.RunScript("document.getElementById('pseMainPanel_mainTabView:oblgSubmitBtn').click()")
wait(1)
fn_exist latestBrowser6.Link("Ok"),"Click",""
fn_exist latestBrowser6.Link("Ok"),"Click",""
if latestBrowser7.WebElement("xpath:=//*[@]/div/ol/li/span[text()= 'Activation is successful...']").Exist(15) then
Reporter.ReportEvent micPass , "Activation is successful...", "Activation is successful..."
                               latestBrowser2.RunScript("document.getElementById('resultButton').click()")    
Else
                Reporter.ReportEvent micFail,"Activation is failed...","Activation is failed..."
End if
End Function

This function concludes the campaign activation process. 

Content of logout_action

Const EXT="XXXXXXXXXXXXXXXXX" [Your logout URL]
fn_navigate(EXT)

The final step of the test is to log out of the web application. For this, we created logout_action. You need to provide your logout URL to this action; then, it simply calls the fn_navigate function. 

Content of fn_exist

Function fn_exist(ex,ex1,ex2)
If ex.Exist(15) Then
  If ex1="Click" Then
                ex.Click
  ElseIf ex1="DoubleClick" Then
                ex.DoubleClick
  ElseIf ex1="Set" Then
                ex.Set ex2
  End If
End If
End Function

You have seen the fn_exists function called in other functions. Basically, this function does clicks, double clicks, or sets, depending on the existing result. 

Once you do the coding, save your solution and select Run under Menu to simulate your test scenario. It will be performed in your selected browser.

Learn about the importance of automated testing as part of a healthy DevOps practice, brought to you in partnership with Sauce Labs.

Topics:
test automation ,uft ,devops ,gui testing ,end to end testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}