Save as PDF in Chrome | Testmate | Selenium

Since the print pane is a desktop window in all the browsers, Save as PDF can not be automated using Selenium. People try to use different chrome options, AutoIt or Robot class to handle such scenarios but that does not always work.

Such scenarios can be easily handled using Testmate and can be triggered from your existing automation via command line.

Here is the sample code for it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
''Declare all the Objects
oWindow = "Pane,Name::r|.*Google Chrome"
oCombo = "ComboBox,FrameworkId::Chrome"
oSaveBase = "Button,Name::Save"
oTextFileName = "Edit,Name::File name:"
oBtnSaveFile = "Window,Name::Save As>==>Button,Name::Save"
 
 
'Reset the root and set a RToot window
'This is optional method, it improves the execution speed
Call uia.ResetRoot()
Call uia.SetRoot(oWindow)
Call uia.SearchElementBySubtree()
 
'Select Value from dropdown
Call SelectValueFromDD(oCombo,"PDF")
Call uia.Click(oSaveBase)
 
'Wait for Save As dialog  upto 20 Seconds
bSync = uia.Exist(oTextFileName,20)
 
'Save PDF
Call uia.SetValue(oTextFileName,"d:\test.pdf")
Call uia.Click(oBtnSaveFile)
 
 
 
 
 
'##############################################################################
'##############################################################################
'########################Cuatom Methods################################
'##############################################################################
'##############################################################################

 
'Since this is a custom dropdown , we need to handle it differently
Function  SelectValueFromDD(Object,Value)
 
   Call uia.Click(Object)
   util.Wait(1)
   'iCount = uia.GetSubtreeCount("ComboBox,FrameworkId::Chrome")
    iCount = 5
 
 
   Call MoveupDD(iCount)
 
   For i =1 to iCount
 
      sValue = uia.GetLegacyProperty(Object,"Value")      
      Call util.PrintOut(Value)
      util.PrintOut(sValue)
      If Instr(sValue,Value)>0 Then
         Exit For
      End If
      native.KeyPress("0x28")
 
 
   Next
 
   Call uia.Click(Object)
 
End Function
 
 
Function MoveupDD(Iteration)
 
   For i = 0 to Iteration + 1
      native.KeyPress("0x26")
   Next
 
End Function
Tags:,

Add a Comment

Your email address will not be published. Required fields are marked *