Skip to content

Commit 04cd9a8

Browse files
authored
Add print pdf keyword (#35)
* init pdf keyword * Init default pdf path * Add support pdf keywords and docs * Update screenshot example keywords
1 parent aaf0b55 commit 04cd9a8

File tree

8 files changed

+106
-1
lines changed

8 files changed

+106
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ example.png
2121
log.html
2222
report.html
2323
/Examples/*.png
24+
*.pdf

Examples/generate-pdf-demo.robot

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+

PuppeteerLibrary/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
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
]

PuppeteerLibrary/keywords/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from .javascript_async import JavascriptKeywordsAsync
1111
from .mockresponse import MockResponseKeywords
1212
from .mockresponse_async import MockResponseKeywordsAsync
13+
from .pdf import PDFKeywords
14+
from .pdf_async import PDFKeywordsAsync
1315
from .screenshot import ScreenshotKeywords
1416
from .screenshot_async import ScreenshotKeywordsAsync
1517
from .utility import UtilityKeywords

PuppeteerLibrary/keywords/pdf.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

PuppeteerLibrary/keywords/screenshot.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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))

PuppeteerLibrary/keywords/screenshot_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from PuppeteerLibrary.base.robotlibcore import keyword
44
from PuppeteerLibrary.base.librarycomponent import LibraryComponent
55

6-
DEFAULT_FILENAME_PAGE = 'selenium-screenshot-{index}.png'
6+
DEFAULT_FILENAME_PAGE = 'puppeteer-screenshot-{index}.png'
77

88
class ScreenshotKeywordsAsync(LibraryComponent):
99

0 commit comments

Comments
 (0)