File tree Expand file tree Collapse file tree 8 files changed +106
-1
lines changed
Expand file tree Collapse file tree 8 files changed +106
-1
lines changed Original file line number Diff line number Diff line change @@ -21,3 +21,4 @@ example.png
2121log.html
2222report.html
2323/Examples /* .png
24+ * .pdf
Original file line number Diff line number Diff line change 1+ *** Settings ***
2+ Library PuppeteerLibrary
3+ Test Setup Open browser to test page
4+ Test Teardown Close Browser
5+
6+
7+ *** Variables ***
8+ ${HOME_PAGE_URL } http://127.0.0.1:7272
9+
10+
11+ *** Test Cases ***
12+ Generate pdf file
13+ Print as pdf
14+
15+ *** Keywords ***
16+ Open browser to test page
17+ ${HEADLESS } Get variable value ${HEADLESS } ${False }
18+ &{options } = create dictionary headless=${HEADLESS }
19+ Open browser ${HOME_PAGE_URL } options=${options }
20+
Original file line number Diff line number Diff line change 1818 JavascriptKeywordsAsync ,
1919 MockResponseKeywords ,
2020 MockResponseKeywordsAsync ,
21+ PDFKeywords ,
22+ PDFKeywordsAsync ,
2123 ScreenshotKeywords ,
2224 ScreenshotKeywordsAsync ,
2325 UtilityKeywords ,
@@ -102,6 +104,7 @@ def __init__(self):
102104 FormElementKeywords (self ),
103105 JavascriptKeywords (self ),
104106 MockResponseKeywords (self ),
107+ PDFKeywords (self ),
105108 ScreenshotKeywords (self ),
106109 UtilityKeywords (self ),
107110 WaitingKeywords (self )
@@ -115,6 +118,7 @@ def __init__(self):
115118 FormElementKeywordsAsync (self ),
116119 JavascriptKeywordsAsync (self ),
117120 MockResponseKeywordsAsync (self ),
121+ PDFKeywordsAsync (self ),
118122 ScreenshotKeywordsAsync (self ),
119123 WaitingKeywordsAsync (self )
120124 ]
Original file line number Diff line number Diff line change 1010from .javascript_async import JavascriptKeywordsAsync
1111from .mockresponse import MockResponseKeywords
1212from .mockresponse_async import MockResponseKeywordsAsync
13+ from .pdf import PDFKeywords
14+ from .pdf_async import PDFKeywordsAsync
1315from .screenshot import ScreenshotKeywords
1416from .screenshot_async import ScreenshotKeywordsAsync
1517from .utility import UtilityKeywords
Original file line number Diff line number Diff line change 1+ from PuppeteerLibrary .base .librarycomponent import LibraryComponent
2+ from PuppeteerLibrary .base .robotlibcore import keyword
3+ from PuppeteerLibrary .keywords .pdf_async import PDFKeywordsAsync , DEFAULT_FILENAME_PAGE
4+
5+
6+ class PDFKeywords (LibraryComponent ):
7+
8+ def __init__ (self , ctx ):
9+ super ().__init__ (ctx )
10+ self .async_func = PDFKeywordsAsync (self .ctx )
11+
12+ @keyword
13+ def print_as_pdf (self , filename = DEFAULT_FILENAME_PAGE ):
14+ """
15+ Print current web page as pdf file. This keyword only support with ``HEADLESS`` mode enable.
16+
17+ The ``filename`` argument specifies filename and path to save the file.
18+ Default valid is 'pdf-{index}.pdf'.
19+
20+ Example:
21+
22+ | &{options} = | create dictionary | headless=${False} |
23+ | Open browser | https://www.w3schools.com/html/html_forms.asp | options=${options} |
24+ | Print as PDF | | |
25+ | Print as PDF | custom-pdf-{index}.pdf | |
26+
27+ """
28+ return self .loop .run_until_complete (self .async_func .print_as_pdf_async (filename ))
Original file line number Diff line number Diff line change 1+ import os
2+ from PuppeteerLibrary .base .robotlibcore import keyword
3+ from PuppeteerLibrary .base .librarycomponent import LibraryComponent
4+
5+
6+ DEFAULT_FILENAME_PAGE = 'pdf-{index}.pdf'
7+
8+ class PDFKeywordsAsync (LibraryComponent ):
9+
10+ def __init__ (self , ctx ):
11+ self .ctx = ctx
12+ self .log_dir = os .curdir
13+
14+ @keyword
15+ async def print_as_pdf_async (self , filename ):
16+ path = self ._get_pdf_path (filename )
17+ await self .ctx .current_page .emulateMedia ('screen' )
18+ await self .ctx .current_page .pdf ({'path' : path })
19+ self .info ('Print as pdf: ' + path )
20+
21+ def _get_pdf_path (self , filename ):
22+ directory = self .log_dir
23+ filename = filename .replace ('/' , os .sep )
24+ index = 0
25+ while True :
26+ index += 1
27+ formatted = self ._format_path (filename , index )
28+ path = os .path .join (directory , formatted )
29+ if formatted == filename or not os .path .exists (path ):
30+ return path
31+
32+ def _format_path (self , file_path , index ):
33+ return file_path .format_map (_SafeFormatter (index = index ))
34+
35+ class _SafeFormatter (dict ):
36+
37+ def __missing__ (self , key ):
38+ return '{%s}' % key
Original file line number Diff line number Diff line change @@ -11,4 +11,16 @@ def __init__(self, ctx):
1111
1212 @keyword
1313 def capture_page_screenshot (self , filename = DEFAULT_FILENAME_PAGE ):
14+ """
15+ Capture current web page as image png file.
16+
17+ The ``filename`` argument specifies filename and path to save the file.
18+ Default valid is 'puppeteer-screenshot-{index}.png'.
19+
20+ Example:
21+
22+ | Capture page screenshot | |
23+ | Capture page screenshot | custom-{index}.png |
24+
25+ """
1426 return self .loop .run_until_complete (self .async_func .capture_page_screenshot_async (filename ))
Original file line number Diff line number Diff line change 33from PuppeteerLibrary .base .robotlibcore import keyword
44from PuppeteerLibrary .base .librarycomponent import LibraryComponent
55
6- DEFAULT_FILENAME_PAGE = 'selenium -screenshot-{index}.png'
6+ DEFAULT_FILENAME_PAGE = 'puppeteer -screenshot-{index}.png'
77
88class ScreenshotKeywordsAsync (LibraryComponent ):
99
You can’t perform that action at this time.
0 commit comments