Skip to content

Commit aa62ad0

Browse files
authored
Add support file handler (#76)
* Add download files for chrome * Support linux path sep * add file upload
1 parent fd80339 commit aa62ad0

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ report.html
2323
/Examples/*.png
2424
*.pdf
2525
Examples/logs/
26+
Examples/tmp-download/test.csv
27+
Examples;tmp-download/test.csv

Examples/form-handler/download-file.robot

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
*** Settings ***
2+
Library OperatingSystem
23
Library PuppeteerLibrary
34
Test Setup Open browser to test page
45
Test Teardown Close All Browser
56
Suite Teardown Close Puppeteer
67

78
*** Variables ***
8-
${DEFAULT_BROWSER} webkit
9+
${DEFAULT_BROWSER} chrome
910
${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html
1011

1112

1213
*** Test Cases ***
13-
Download file
14-
[Tags] Ignore_chrome
14+
Download file
1515
${file path} = Download File id=download-file
1616
Should Not Be Empty ${file path} Download file failed
17+
Get File ${file path}
18+
19+
Upload file
20+
${file} = OperatingSystem.Join Path ${CURDIR} iframe.robot
21+
Upload File id=fileToUpload ${file}
1722

1823
*** Keywords ***
1924
Open browser to test page

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 13 ${No of h2}
15+
Should Be Equal As Numbers 14 ${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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ async def clear_element_text(self, locator: str):
1313
pass
1414

1515
@abstractmethod
16-
async def download_file(self, locator: str):
16+
async def download_file(self, locator: str, timeout=None):
17+
pass
18+
19+
@abstractmethod
20+
async def upload_file(self, locator: str, file_path: str):
1721
pass

PuppeteerLibrary/keywords/formelement.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ def clear_element_text(self, locator):
3838
self.loop.run_until_complete(self.get_async_keyword_group().clear_element_text(locator))
3939

4040
@keyword
41-
def download_file(self, locator):
42-
return self.loop.run_until_complete(self.get_async_keyword_group().download_file(locator))
41+
def download_file(self, locator, timeout=None):
42+
return self.loop.run_until_complete(self.get_async_keyword_group().download_file(locator, timeout))
43+
44+
@keyword
45+
def upload_file(self, locator, file_path):
46+
return self.loop.run_until_complete(self.get_async_keyword_group().upload_file(locator, file_path))

PuppeteerLibrary/playwright/async_keywords/playwright_formelement.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ async def input_text(self, locator: str, text: str, clear=True):
1616
async def clear_element_text(self, locator: str):
1717
await self._clear_input_text(locator)
1818

19-
async def download_file(self, locator: str):
19+
async def download_file(self, locator: str, timeout=None):
20+
timeout = self.timestr_to_secs_for_default_timeout(timeout)* 1000
2021
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+
tasks = self.library_ctx.get_current_page().click_with_selenium_locator(locator), page.waitForEvent('download', timeout=timeout)
2223
_, b = await asyncio.gather(*tasks)
2324
return await b.path()
2425

26+
async def upload_file(self, locator: str, file_path: str):
27+
handle = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator)
28+
await handle.setInputFiles(file_path)
29+
2530
async def _clear_input_text(self, selenium_locator):
2631
await self.library_ctx.get_current_page().click_with_selenium_locator(selenium_locator, {'clickCount': 3})
2732
await self.library_ctx.get_current_page().get_page().keyboard.press('Backspace')

PuppeteerLibrary/puppeteer/async_keywords/puppeteer_formelement.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import os
2+
import glob
3+
import shutil
4+
import time
15
from PuppeteerLibrary.ikeywords.iformelement_async import iFormElementAsync
26

37

@@ -14,9 +18,33 @@ async def input_text(self, locator: str, text: str, clear=True):
1418
async def clear_element_text(self, locator: str):
1519
await self._clear_input_text(locator)
1620

17-
async def download_file(self, locator: str):
18-
raise Exception("Sorry, keyword: download_file not support.")
19-
21+
async def download_file(self, locator: str, timeout=None):
22+
path = os.getcwd()+''+os.sep+'tmp-download'
23+
try:
24+
shutil.rmtree(path)
25+
except:
26+
self.info('Cannot cleanup the tmp download folder.')
27+
page = self.library_ctx.get_current_page().get_page()
28+
await page._client.send('Page.setDownloadBehavior', {
29+
'behavior': 'allow',
30+
'downloadPath': path
31+
})
32+
await self.library_ctx.get_current_page().click_with_selenium_locator(locator)
33+
timeout = self.timestr_to_secs_for_default_timeout(timeout)
34+
max_time = time.time() + timeout
35+
file = None
36+
while time.time() < max_time:
37+
time.sleep(1)
38+
files = glob.glob(path+''+os.sep+'*')
39+
if len(files) == 1:
40+
file = files[0]
41+
break
42+
return file
43+
44+
async def upload_file(self, locator: str, file_path: str):
45+
element = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator)
46+
return await element.uploadFile(file_path)
47+
2048
async def _clear_input_text(self, selenium_locator):
2149
await self.library_ctx.get_current_page().click_with_selenium_locator(selenium_locator, {'clickCount': 3})
2250
await self.library_ctx.get_current_page().get_page().keyboard.press('Backspace')

demoapp/html/basic-html-elements.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ <h2>Download file</h2>
322322
<hr>
323323
</div>
324324

325+
<div class="container">
326+
<div class="row" style="margin-top: 20px;">
327+
<h2>Upload file</h2>
328+
</div>
329+
<div class="row" style="margin-top: 20px;">
330+
<form action="#" method="post" enctype="multipart/form-data">
331+
Select image to upload:
332+
<br><br>
333+
<input type="file" name="fileToUpload" id="fileToUpload">
334+
<br><br>
335+
<input type="submit" value="Upload Image" name="submit">
336+
</form>
337+
</div>
338+
<hr>
339+
</div>
325340

326341
<br>
327342
<br>

0 commit comments

Comments
 (0)