Skip to content

Commit abfebc8

Browse files
authored
Add element upload file (#38)
* Add element upload file * Enhance wait for new window open keyword * Fix unstable wait for new window open * Fix script failed based on update keyword
1 parent 8e45e81 commit abfebc8

File tree

10 files changed

+77
-16
lines changed

10 files changed

+77
-16
lines changed

Examples/browser-demo.robot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Example switch window and check window title
1111
Maximize Browser Window
1212
${title} = Get title
1313
${location} = Get location
14+
${window count} = Get Window Count
1415
Run Async Keywords
15-
... Wait for new window open AND
16+
... Wait for new window open ${window count} 5s AND
1617
... Click Element id=readdocs
1718
Switch Window NEW
1819
${Title} = Get Title

Examples/file-upload.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/file-upload.html
9+
10+
11+
*** Test Cases ***
12+
Upload file demo
13+
Upload file id=fileToUpload ${CURDIR}/quick-start.robot
14+
15+
16+
*** Keywords ***
17+
Open browser to test page
18+
${HEADLESS} Get variable value ${HEADLESS} ${False}
19+
&{options} = create dictionary headless=${HEADLESS}
20+
Open browser ${HOME_PAGE_URL} options=${options}

Examples/generate-pdf-demo.robot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ${HOME_PAGE_URL} http://127.0.0.1:7272
1010

1111
*** Test Cases ***
1212
Generate pdf file
13+
[Documentation] Only support on headless mode
1314
Print as pdf
1415

1516
*** Keywords ***

Examples/timeout-demo.robot

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ ${HOME_PAGE_URL} http://127.0.0.1:7272
1111
*** Test Cases ***
1212
Set default timeout
1313
Set Timeout 3s
14-
Run Keyword And Expect Error No new page has been open.* Wait for new window open
14+
${window count} = Get Window Count
15+
Run Keyword And Expect Error No new page has been open.* Wait for new window open ${window count}
1516

1617
Timeout wait for new window open
17-
Run Keyword And Expect Error No new page has been open.* Wait for new window open 1s
18-
Click Element id:readdocs
19-
Wait for new window open 5s
20-
18+
${window count} = Get Window Count
19+
Run Async Keywords
20+
... Wait for new window open ${window count} 5s AND
21+
... Click Element id:readdocs
2122

2223
*** Keywords ***
2324
Open test browser

PuppeteerLibrary/keywords/browsermanagement.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ def set_timeout(self, timeout):
158158
return orig_timeout
159159

160160
@keyword
161-
def wait_for_new_window_open(self, timeout=None):
161+
def get_window_count(self):
162+
""" Get windows count
163+
"""
164+
return self.loop.run_until_complete(self.async_func.get_window_count_async())
165+
166+
@keyword
167+
def wait_for_new_window_open(self, current_page_count=1, timeout=None):
162168
"""
163169
Waits until new page or tab opens.
164170
@@ -167,7 +173,7 @@ def wait_for_new_window_open(self, timeout=None):
167173
| Run Async Keywords | Click Element | id:view_conditions | AND |
168174
| ... | `Wait For New Window Open` | | |
169175
"""
170-
self.loop.run_until_complete(self.async_func.wait_for_new_window_open_async(timeout))
176+
self.loop.run_until_complete(self.async_func.wait_for_new_window_open_async(current_page_count, timeout))
171177

172178
@keyword
173179
def switch_window(self, locator='MAIN'):

PuppeteerLibrary/keywords/browsermanagement_async.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
class BrowserManagementKeywordsAsync(LibraryComponent):
88

99
@keyword
10-
async def wait_for_new_window_open_async(self, timeout=None):
11-
timeout = self.timestr_to_secs_for_default_timeout(timeout)
10+
async def get_window_count_async(self):
1211
pages = await self.ctx.get_browser().pages()
13-
await pages[-1].title() # workaround for force pages re-cache
14-
pre_page_len = len(pages)
12+
for page in pages:
13+
await page.title() # workaround for force pages re-cache
14+
return len(await self.ctx.get_browser().pages())
15+
16+
@keyword
17+
async def wait_for_new_window_open_async(self, current_page_count=1, timeout=None):
18+
page_len = 0
19+
pre_page_len = int(current_page_count)
20+
timeout = self.timestr_to_secs_for_default_timeout(timeout)
1521
timer = 0
1622
while timer < timeout:
17-
pages = await self.ctx.get_browser().pages()
18-
await pages[-1].title() # workaround for force pages re-cache
19-
page_len = len(pages)
23+
page_len = await self.get_window_count_async()
2024
if page_len > pre_page_len:
2125
return
2226
timer += 1

PuppeteerLibrary/keywords/element.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,9 @@ def element_text_should_not_be(self, locator, expected, ignore_case=False):
124124
125125
"""
126126
return self.loop.run_until_complete(self.async_func.element_text_should_not_be_async(locator, expected, ignore_case))
127+
128+
@keyword
129+
def upload_file(self, locator, file_path):
130+
""" Upload file
131+
"""
132+
return self.loop.run_until_complete(self.async_func.upload_file_async(locator, file_path))

PuppeteerLibrary/keywords/element_async.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ async def element_text_should_be_async(self, selenium_locator, expected, ignore_
9696
async def element_text_should_not_be_async(self, selenium_locator, expected, ignore_case=False):
9797
text = await self.get_text_async(selenium_locator)
9898
return BuiltIn().should_not_be_equal_as_strings(text, expected, ignore_case=ignore_case)
99+
100+
@keyword
101+
async def upload_file_async(self, selenium_locator, file_path):
102+
element = await self.ctx.get_current_page().querySelector_with_selenium_locator(selenium_locator)
103+
await element.uploadFile(file_path)

demoapp/html/file-upload.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<div class="container">
9+
<form method="post" enctype="multipart/form-data">
10+
Select image to upload:
11+
<input type="file" name="fileToUpload" id="fileToUpload">
12+
<input type="submit" value="Upload Image" name="submit">
13+
</form>
14+
</div>
15+
</body>
16+
</html>

demoapp/html/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
</div>
3939
</div>
4040
</div>
41-
<div id="container">
41+
42+
<div id="container" class="container">
4243
<h1>Login Page</h1>
4344
<p>Please input your user name and password and click the login button.</p>
4445
<form name="login_form" onsubmit="login(this.username_field.value, this.password_field.value); return false;">

0 commit comments

Comments
 (0)