Skip to content

Commit d0aea4d

Browse files
authored
Playwright support download file (#73)
* Playwright Add download keywords * Add demo download file * Fix download attribute * Fix demo
1 parent d57d9ef commit d0aea4d

File tree

9 files changed

+59
-7
lines changed

9 files changed

+59
-7
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
*** Settings ***
2+
Library PuppeteerLibrary
3+
Test Setup Open browser to test page
4+
Test Teardown Close All Browser
5+
Suite Teardown Close Puppeteer
6+
7+
*** Variables ***
8+
${DEFAULT_BROWSER} webkit
9+
${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html
10+
11+
12+
*** Test Cases ***
13+
Download file
14+
[Tags] Ignore_chrome
15+
${file path} = Download File id=download-file
16+
Should Not Be Empty ${file path} Download file failed
17+
18+
*** Keywords ***
19+
Open browser to test page
20+
${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER}
21+
${HEADLESS} Get variable value ${HEADLESS} ${False}
22+
&{options} = create dictionary headless=${HEADLESS}
23+
Open browser ${HOME_PAGE_URL} browser=${BROWSER} options=${options}

Examples/form-handler/element-properties.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html
1212
*** Test Cases ***
1313
Count element
1414
${No of h2} = Get Element Count css=h2
15-
Should Be Equal As Numbers 12 ${No of h2}
15+
Should Be Equal As Numbers 13 ${No of h2}
1616

1717
Get element attribute
1818
${type value} = Get Element Attribute id=alert_confirm type

PuppeteerLibrary/ikeywords/iformelement_async.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ async def input_text(self, locator: str, text: str, clear=True):
1111
@abstractmethod
1212
async def clear_element_text(self, locator: str):
1313
pass
14-
14+
15+
@abstractmethod
16+
async def download_file(self, locator: str):
17+
pass

PuppeteerLibrary/keywords/formelement.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from PuppeteerLibrary.base.robotlibcore import keyword
23
from PuppeteerLibrary.base.librarycomponent import LibraryComponent
34
from PuppeteerLibrary.ikeywords.iformelement_async import iFormElementAsync
@@ -36,3 +37,6 @@ def clear_element_text(self, locator):
3637
"""
3738
self.loop.run_until_complete(self.get_async_keyword_group().clear_element_text(locator))
3839

40+
@keyword
41+
def download_file(self, locator):
42+
return self.loop.run_until_complete(self.get_async_keyword_group().download_file(locator))

PuppeteerLibrary/playwright/async_keywords/playwright_formelement.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from PuppeteerLibrary.ikeywords.iformelement_async import iFormElementAsync
23
from PuppeteerLibrary.locators.SelectorAbstraction import SelectorAbstraction
34

@@ -15,8 +16,12 @@ async def input_text(self, locator: str, text: str, clear=True):
1516
async def clear_element_text(self, locator: str):
1617
await self._clear_input_text(locator)
1718

19+
async def download_file(self, locator: str):
20+
page = self.library_ctx.get_current_page().get_page()
21+
tasks = self.library_ctx.get_current_page().click_with_selenium_locator(locator), page.waitForEvent('download')
22+
_, b = await asyncio.gather(*tasks)
23+
return await b.path()
24+
1825
async def _clear_input_text(self, selenium_locator):
1926
await self.library_ctx.get_current_page().click_with_selenium_locator(selenium_locator, {'clickCount': 3})
2027
await self.library_ctx.get_current_page().get_page().keyboard.press('Backspace')
21-
22-

PuppeteerLibrary/playwright/playwright_context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ async def start_server(self, options: dict=None):
3838
if self.browser_type == "webkit":
3939
self.browser = await self.playwright.webkit.launch(headless=False)
4040
elif self.browser_type == "firefox":
41-
self.browser = await self.playwright.firefox.launch(headless=False)
41+
self.browser = await self.playwright.firefox.launch(headless=False)
42+
self.browser.acceptDownloads = True
4243

4344
async def stop_server(self):
4445
await self.playwright.stop()
@@ -50,7 +51,9 @@ def is_server_started(self) -> bool:
5051
return False
5152

5253
async def create_new_page(self, options: dict=None) -> BasePage:
53-
device_options = {}
54+
device_options = {
55+
'acceptDownloads': True
56+
}
5457
if 'emulate' in options:
5558
device_options = self.playwright.devices[options['emulate']]
5659
new_page = await self.browser.newPage(**device_options)

PuppeteerLibrary/puppeteer/async_keywords/puppeteer_formelement.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from robot.libraries.BuiltIn import BuiltIn
21
from PuppeteerLibrary.ikeywords.iformelement_async import iFormElementAsync
32

43

@@ -14,6 +13,9 @@ async def input_text(self, locator: str, text: str, clear=True):
1413

1514
async def clear_element_text(self, locator: str):
1615
await self._clear_input_text(locator)
16+
17+
async def download_file(self, locator: str):
18+
raise Exception("Sorry, keyword: download_file not support.")
1719

1820
async def _clear_input_text(self, selenium_locator):
1921
await self.library_ctx.get_current_page().click_with_selenium_locator(selenium_locator, {'clickCount': 3})

demoapp/html/basic-html-elements.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ <h2>Navigate Page</h2>
312312
<hr>
313313
</div>
314314

315+
<div class="container">
316+
<div class="row" style="margin-top: 20px;">
317+
<h2>Download file</h2>
318+
</div>
319+
<div class="row" style="margin-top: 20px;">
320+
<a id="download-file" href="files/test.csv" download>download</a>
321+
</div>
322+
<hr>
323+
</div>
324+
325+
315326
<br>
316327
<br>
317328
<br>

demoapp/html/files/test.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
t1,t2,t3

0 commit comments

Comments
 (0)