Skip to content

Commit

Permalink
Enhance return http data for wait for request & response keywords (#45)
Browse files Browse the repository at this point in the history
- Enhance return result for wait for request and response keywords
- Enhance Run Async Keywords support return result value
- Hot fixed wait for new page crash during call .title() function
  • Loading branch information
atthaboon authored Sep 3, 2020
1 parent 34091ff commit cf24b7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
18 changes: 12 additions & 6 deletions Examples/wait-for-demo.robot
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ Demo wait for http request
${HEADLESS} Get variable value ${HEADLESS} ${False}
&{options} = create dictionary headless=${HEADLESS}
Open browser ${HOME_PAGE_URL} options=${options}
Run Async Keywords
... Click Element id:get_ajax AND
... Wait for request url /ajax_info.json
${results} = Run Async Keywords
... Wait for request url /ajax_info.json AND
... Click Element id:get_ajax
Should Contain ${results[0].url} ajax
Should Be Equal As Strings ${results[0].method} GET
Log ${results[0].body}

Demo wait for http response
${HEADLESS} Get variable value ${HEADLESS} ${False}
&{options} = create dictionary headless=${HEADLESS}
Open browser ${HOME_PAGE_URL} options=${options}
Run Async Keywords
... Click Element id:get_ajax AND
... Wait for response url /ajax_info.json\\?count=3 200 name.*?p1.*?name.*?p2.*?name.*?p3
${results} = Run Async Keywords
... Wait for response url /ajax_info.json\\?count=3 200 name.*?p1.*?name.*?p2.*?name.*?p3 AND
... Click Element id:get_ajax
Should Contain ${results[0].url} ajax
Should Be Equal As Strings ${results[0].status} 200
Log ${results[0].body}

Demo wait for navigation
${HEADLESS} Get variable value ${HEADLESS} ${False}
Expand Down
13 changes: 8 additions & 5 deletions PuppeteerLibrary/keywords/browsermanagement_async.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import time
from PuppeteerLibrary.base.robotlibcore import keyword
from PuppeteerLibrary.base.librarycomponent import LibraryComponent
Expand All @@ -11,7 +12,10 @@ async def get_window_count_async(self):
pages = await self.ctx.get_browser().pages()
for page in pages:
# Workaround: for force pages re-cache
await page.title()
try:
await page.title()
except:
return -1
return len(await self.ctx.get_browser().pages())

@keyword
Expand All @@ -22,13 +26,12 @@ async def wait_for_new_window_open_async(self, timeout=None):
# Page length still not update / same as before open new window
pre_page_len = len(await self.ctx.get_browser().pages())
timeout = self.timestr_to_secs_for_default_timeout(timeout)
timer = 0
while timer < timeout:
max_time = time.time() + timeout
while time.time() < max_time:
page_len = await self.get_window_count_async()
if page_len > pre_page_len:
return
timer += 1
time.sleep(1)
await asyncio.sleep(0.5)
raise Exception('No new page has been open. pre: ' + str(pre_page_len) + ' current: ' + str(page_len))

@keyword
Expand Down
5 changes: 3 additions & 2 deletions PuppeteerLibrary/keywords/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def run_async_keywords(self, *keywords):
# Ensure that script load async keywords before run async keywords function
"""Executes all the given keywords in a asynchronous and wait until all keyword is completed
``Return`` Array of return for reach keywords based on index
Example:
| Open browser | ${HOME_PAGE_URL} | options=${options} | |
Expand All @@ -20,14 +22,13 @@ def run_async_keywords(self, *keywords):
"""
self.ctx.load_async_keywords()
run_keyword = _RunKeyword()
self.loop.run_until_complete( self._run_async_keywords(run_keyword._split_run_keywords(list(keywords))) )
return self.loop.run_until_complete( self._run_async_keywords(run_keyword._split_run_keywords(list(keywords))) )

async def _run_async_keywords(self, iterable):
statements = []
for kw, args in iterable:
kw_name = kw.lower().replace(' ', '_') + '_async'
statements.append(self.ctx.keywords[kw_name](*args))

try:
return await asyncio.gather(*statements)
except Exception as err:
Expand Down
4 changes: 4 additions & 0 deletions PuppeteerLibrary/keywords/waiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def wait_for_request_url(self, url, method='GET', body=None, timeout=None):
The ``body`` is request body message. body can match using regexp
``Return`` request msg with properties {url, method, body}
Example:
| Open browser | ${HOME_PAGE_URL} | options=${options} | | |
Expand Down Expand Up @@ -60,6 +62,8 @@ def wait_for_response_url(self, url, status=200, body=None, timeout=None):
- 500
Reference:[https://restfulapi.net/http-status-codes/|https://restfulapi.net/http-status-codes/]
``Return`` request msg with properties {url, status, body}
Example:
| Open browser | ${HOME_PAGE_URL} | options=${options} | | |
Expand Down
14 changes: 14 additions & 0 deletions PuppeteerLibrary/keywords/waiting_async.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import time
import re
from robot.utils import DotDict
from PuppeteerLibrary.base.librarycomponent import LibraryComponent
from PuppeteerLibrary.base.robotlibcore import keyword

Expand All @@ -15,10 +16,12 @@ async def wait_for_request_url_async(self, url, method='GET', body=None, timeout
, options={
'timeout': self.timestr_to_secs_for_default_timeout(timeout) * 1000
})

try:
pos_data = (await req.postData())
except:
pos_data = ''

if body is None or re.search(body, pos_data.replace('\n', '')):
log_str = 'Wait for request url: '+req.method+' - '+req.url
if pos_data != '':
Expand All @@ -27,6 +30,12 @@ async def wait_for_request_url_async(self, url, method='GET', body=None, timeout
else:
raise Exception('Can\'t match request body with ' + body + ' \n ' + pos_data)

return DotDict({
'url': req.url,
'method': req.method,
'body': pos_data
})

@keyword
async def wait_for_response_url_async(self, url, status=200, body=None, timeout=None):
res = await self.ctx.get_current_page().waitForResponse(
Expand All @@ -46,6 +55,11 @@ async def wait_for_response_url_async(self, url, status=200, body=None, timeout=
self.info(log_str)
else:
raise Exception('Can\'t match response body with '+body+' \n '+res_text)
return DotDict({
'url': res.url,
'status': res.status,
'body': res_text
})

@keyword
async def wait_for_navigation_async(self, timeout=None):
Expand Down

0 comments on commit cf24b7d

Please sign in to comment.