Skip to content

Commit 4d31343

Browse files
authored
Add Wait for location related keywords (#18)
* Add draft keyword * Add wait for location contain text * Add wait until location does not contains
1 parent 47862a3 commit 4d31343

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

Examples/wait-for-demo.robot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ Demo wait for element contains text
5555
Open browser ${HOME_PAGE_URL} options=${options}
5656
Wait Until Element Contains css:#container p Please input your user name
5757
Wait Until Element Does Not Contains css:#container p Invalid user name and/or password
58+
59+
Demo wait for location contains
60+
${HEADLESS} Get variable value ${HEADLESS} ${False}
61+
&{options} = create dictionary headless=${HEADLESS}
62+
Open browser ${HOME_PAGE_URL} options=${options}
63+
Go To ${HOME_PAGE_URL}/docs.html
64+
Switch Window NEW
65+
Wait Until Location Contains docs.html

PuppeteerLibrary/keywords/browsermanagement.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ async def open_browser_async():
5050
'width': merged_options['width'],
5151
'height': merged_options['height']
5252
},
53-
args=['--no-sandbox', '--disable-setuid-sandbox'])
53+
# Only for ubuntu
54+
# args=['--no-sandbox', '--disable-setuid-sandbox'])
55+
args=['--disable-setuid-sandbox'])
5456
self.ctx.current_page = await self.ctx.browser.newPage()
5557
await self.ctx.current_page.goto(url)
5658
await self.ctx.current_page.screenshot({'path': 'example.png'})

PuppeteerLibrary/keywords/waiting.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,20 @@ def wait_until_element_does_not_contains(self, locator, text, timeout=None):
175175
"""
176176
return self.loop.run_until_complete(self.async_func.wait_until_element_does_not_contains_async(locator, text, timeout))
177177

178+
@keyword
179+
def wait_until_location_contains(self, expected, timeout=None):
180+
"""
181+
Waits until the current URL contains `expected`.
182+
183+
The `expected` argument contains the expected value in url.
184+
"""
185+
return self.loop.run_until_complete(self.async_func.wait_until_location_contains_async(expected, timeout))
186+
187+
@keyword
188+
def wait_until_location_does_not_contains(self, expected, timeout=None):
189+
"""
190+
Waits until the current URL does not contains `expected`.
191+
192+
The `expected` argument contains the expected value must not in url.
193+
"""
194+
return self.loop.run_until_complete(self.async_func.wait_until_location_does_not_contains_async(expected, timeout))

PuppeteerLibrary/keywords/waiting_async.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,33 @@ async def validate_element_contains_text():
9393
validate_element_contains_text,
9494
self.timestr_to_secs_for_default_timeout(timeout))
9595

96+
@keyword
97+
async def wait_until_location_contains_async(self, expected, timeout=None):
98+
async def validate_url_contains_text():
99+
return expected in self.ctx.get_current_page().url
100+
return await self._wait_until_worker(
101+
validate_url_contains_text,
102+
self.timestr_to_secs_for_default_timeout(timeout))
103+
104+
@keyword
105+
async def wait_until_location_does_not_contains_async(self, expected, timeout=None):
106+
async def validate_url_not_contains_text():
107+
return expected not in self.ctx.get_current_page().url
108+
return await self._wait_until_worker(
109+
validate_url_not_contains_text,
110+
self.timestr_to_secs_for_default_timeout(timeout))
111+
96112
async def _wait_until_worker(self, condition, timeout, error=None):
97113
max_time = time.time() + timeout
98114
not_found = None
99115
while time.time() < max_time:
100116
try:
101117
if await condition():
102118
return
119+
else:
120+
not_found = None
103121
except Exception as err:
104122
not_found = err
105-
else:
106-
not_found = None
107123
await asyncio.sleep(0.2)
108124
raise AssertionError(not_found or error)
109125

0 commit comments

Comments
 (0)