Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ report.html
/Examples/*.png
*.pdf
Examples/logs/
Examples/tmp-download/test.csv
Examples;tmp-download/test.csv
11 changes: 8 additions & 3 deletions Examples/form-handler/download-file.robot
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
*** Settings ***
Library OperatingSystem
Library PuppeteerLibrary
Test Setup Open browser to test page
Test Teardown Close All Browser
Suite Teardown Close Puppeteer

*** Variables ***
${DEFAULT_BROWSER} webkit
${DEFAULT_BROWSER} chrome
${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html


*** Test Cases ***
Download file
[Tags] Ignore_chrome
Download file
${file path} = Download File id=download-file
Should Not Be Empty ${file path} Download file failed
Get File ${file path}

Upload file
${file} = OperatingSystem.Join Path ${CURDIR} iframe.robot
Upload File id=fileToUpload ${file}

*** Keywords ***
Open browser to test page
Expand Down
2 changes: 1 addition & 1 deletion Examples/form-handler/element-properties.robot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html
*** Test Cases ***
Count element
${No of h2} = Get Element Count css=h2
Should Be Equal As Numbers 13 ${No of h2}
Should Be Equal As Numbers 14 ${No of h2}

Get element attribute
${type value} = Get Element Attribute id=alert_confirm type
Expand Down
6 changes: 5 additions & 1 deletion PuppeteerLibrary/ikeywords/iformelement_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ async def clear_element_text(self, locator: str):
pass

@abstractmethod
async def download_file(self, locator: str):
async def download_file(self, locator: str, timeout=None):
pass

@abstractmethod
async def upload_file(self, locator: str, file_path: str):
pass
8 changes: 6 additions & 2 deletions PuppeteerLibrary/keywords/formelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ def clear_element_text(self, locator):
self.loop.run_until_complete(self.get_async_keyword_group().clear_element_text(locator))

@keyword
def download_file(self, locator):
return self.loop.run_until_complete(self.get_async_keyword_group().download_file(locator))
def download_file(self, locator, timeout=None):
return self.loop.run_until_complete(self.get_async_keyword_group().download_file(locator, timeout))

@keyword
def upload_file(self, locator, file_path):
return self.loop.run_until_complete(self.get_async_keyword_group().upload_file(locator, file_path))
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ async def input_text(self, locator: str, text: str, clear=True):
async def clear_element_text(self, locator: str):
await self._clear_input_text(locator)

async def download_file(self, locator: str):
async def download_file(self, locator: str, timeout=None):
timeout = self.timestr_to_secs_for_default_timeout(timeout)* 1000
page = self.library_ctx.get_current_page().get_page()
tasks = self.library_ctx.get_current_page().click_with_selenium_locator(locator), page.waitForEvent('download')
tasks = self.library_ctx.get_current_page().click_with_selenium_locator(locator), page.waitForEvent('download', timeout=timeout)
_, b = await asyncio.gather(*tasks)
return await b.path()

async def upload_file(self, locator: str, file_path: str):
handle = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator)
await handle.setInputFiles(file_path)

async def _clear_input_text(self, selenium_locator):
await self.library_ctx.get_current_page().click_with_selenium_locator(selenium_locator, {'clickCount': 3})
await self.library_ctx.get_current_page().get_page().keyboard.press('Backspace')
34 changes: 31 additions & 3 deletions PuppeteerLibrary/puppeteer/async_keywords/puppeteer_formelement.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
import glob
import shutil
import time
from PuppeteerLibrary.ikeywords.iformelement_async import iFormElementAsync


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

async def download_file(self, locator: str):
raise Exception("Sorry, keyword: download_file not support.")

async def download_file(self, locator: str, timeout=None):
path = os.getcwd()+''+os.sep+'tmp-download'
try:
shutil.rmtree(path)
except:
self.info('Cannot cleanup the tmp download folder.')
page = self.library_ctx.get_current_page().get_page()
await page._client.send('Page.setDownloadBehavior', {
'behavior': 'allow',
'downloadPath': path
})
await self.library_ctx.get_current_page().click_with_selenium_locator(locator)
timeout = self.timestr_to_secs_for_default_timeout(timeout)
max_time = time.time() + timeout
file = None
while time.time() < max_time:
time.sleep(1)
files = glob.glob(path+''+os.sep+'*')
if len(files) == 1:
file = files[0]
break
return file

async def upload_file(self, locator: str, file_path: str):
element = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator)
return await element.uploadFile(file_path)

async def _clear_input_text(self, selenium_locator):
await self.library_ctx.get_current_page().click_with_selenium_locator(selenium_locator, {'clickCount': 3})
await self.library_ctx.get_current_page().get_page().keyboard.press('Backspace')
15 changes: 15 additions & 0 deletions demoapp/html/basic-html-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ <h2>Download file</h2>
<hr>
</div>

<div class="container">
<div class="row" style="margin-top: 20px;">
<h2>Upload file</h2>
</div>
<div class="row" style="margin-top: 20px;">
<form action="#" method="post" enctype="multipart/form-data">
Select image to upload:
<br><br>
<input type="file" name="fileToUpload" id="fileToUpload">
<br><br>
<input type="submit" value="Upload Image" name="submit">
</form>
</div>
<hr>
</div>

<br>
<br>
Expand Down