Sunday 7 June 2015

Download, Open and Save PDF file in TestComplete

I had a new experience in TestComplete, I had gone through a Scenario,where I have to Upload, Download, Open and Saved a downloaded PDF file.

I found downloading, Opening and Saving PDF file is complex process in TestComplete.


When I Click on "Download as PDF" file it ask me for Open/Save/Cancel like below-


OR



I have found some steps/solutions to make it easy. I am sharing those-  

1- Browser Setting  :- We have to  Enable/Disabled the corresponding Add-on/Plugin in a Browser It is depend upon your need. 

In my Scenario, I have to download pdf file, Open downloaded file and Save file as PDF. therefore I have to do same setting at Adobe Reader and Browser level.

1.1 -  In Adobe Reader, I have to make setting "Display PDF in Browser" is active, for that we have to follow below steps.

       1- Open Adobe Reader
       2- Go To Edit Tab
       3- Click on Preference option 
       4- Select "Display PDF in Browser" and Save.


1.2- In Browser,  I have to make Adobe Reader as Enabled, for making Adobe Reader Enable, follow the steps - 

    1- Open IE Browser
    2- Go to Tool Menu
    3- Go to Manage Add-ons 
    4- Select "Toolbar and Extensions"  from "Add-ons Type" section
    5- Select "All add-ons"  from "Show" section
    6- Select Adobe PDF Reader and Set Status as Enabled



In this way we can Open our Downloaded PDF file directly into the browser but still it is not saved. Thing is that - 

* TestComplete can not able to reorganized Save button in PDF file.
* Above solution is machine dependent.(We can not run same test on another machine without above setting) 

2 Scripting - Scripting is one of the best solution for this problem. In Descriptive Programming we can solve this problem without any machine dependency. In TestComplete we can write Descriptive programming using VBScript. (I am using VBScript for samble).

2.1-  First of all, we are searching "Download as PDF" button on my web page, If Button is found then we will click on "Download as PDF" button.


Sample Code - 

Set btnDownloadPdf  = Sys.Browser("iexplore").Page("*").FindChild(Array("objectType", " value"),Array("SubmitButton"," Download as PDF"), 1000)

IF (btnDownloadPdf.Exists) Then
btnDownloadPdf.Click()
Delay(1000)
End IF

2.2- Once We click on Download Button - Pop up will come and ask for Open/Save/Cancel. again we will search open button on pop- up and If button will found we will click on Open Button - 


Sample Code- 


Set btnOpenPdf  = Sys.Browser("iexplore").Page("*").FindChild(Array("objectType", " value"),Array("Button","Open"), 1000)

IF (btnOpenPdf.Exists) Then
btnOpenPdf.Click()
Delay(1000)

End IF

2.3- Once we click on Open button, PDF file will get open.but TestComplete not able to identify save button from PDF file so we can't use descriptive programming to save file, here we can save our file using an alternative way  WScript.Shell Programming -

Sample Code -


  objShellpdfSave = CreateObject("WScript.Shell")
 objShellpdfSave.SendKeys("+^S")
 Delay(5000)
 objShellpdfSave.SendKeys (Path and File Name)
 Delay(1000)     ;   
 objShellpdfSave.Sendkeys(ENTER)


This code may help you to save all type of file in your system using TestComplete. :)