Skip to content

Commit 3bd28da

Browse files
authored
fix issue 138 get element count not throw exception and add keyword drag and drop (#143)
* Add drag and drop keywords * Fix support selenium locator * Return 0 if get elements count not found any items * Fix count not found items return 0 instead of throw exception * Add document for mouse drag limitation. Fixed set timeout keywords Co-authored-by: DESKTOP\atthaboon.s <atthaboon.s@qahive.com>
1 parent c1a93a8 commit 3bd28da

File tree

11 files changed

+49
-7
lines changed

11 files changed

+49
-7
lines changed

Examples/alert-handler/alert-handler.robot

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Dismiss alert
2323
... Click Element id=alert_confirm
2424
Click Element id:get_ajax
2525

26-
2726
*** Keywords ***
2827
Open browser to test page
2928
${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER}

Examples/form-handler/element-properties.robot

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ ${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html
1010

1111

1212
*** Test Cases ***
13-
Count element
13+
Count elements
1414
${No of h2} = Get Element Count css=h2
1515
Should Be Equal As Numbers 14 ${No of h2}
16+
17+
Count non existing element
18+
Set Timeout 1s
19+
${No of h2} = Get Element Count css=Hnotexisting
20+
Should Be Equal As Numbers 0 ${No of h2}
1621

1722
Get element attribute
1823
${type value} = Get Element Attribute id=alert_confirm type

Examples/input/mouse.robot

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Test Teardown Close All Browser
55
Suite Teardown Close Puppeteer
66

77
*** Variables ***
8-
${DEFAULT_BROWSER} firefox
8+
${DEFAULT_BROWSER} chrome
99
${HOME_PAGE_URL} http://127.0.0.1:7272/basic-html-elements.html
1010

1111

@@ -22,7 +22,11 @@ Mouse drag
2222
Mouse Down id=ball
2323
Mouse Move 40 50
2424
Mouse Up
25-
25+
26+
Mouse drag and drop
27+
[Tags] Ignore_firefox Ignore_ptchrome
28+
Drag And Drop id=ball id=dropdown-menu
29+
2630
*** Keywords ***
2731
Open browser to test page
2832
${BROWSER} = Get variable value ${BROWSER} ${DEFAULT_BROWSER}

PuppeteerLibrary/keywords/element.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ def element_text_should_not_be(self, locator, expected, ignore_case=False):
194194
def get_element_count(self, locator):
195195
""" Returns the number of elements matching ``locator``.
196196
"""
197-
return len(self.loop.run_until_complete(self.get_async_keyword_group().find_elements(locator)))
197+
try:
198+
return len(self.loop.run_until_complete(self.get_async_keyword_group().find_elements(locator)))
199+
except:
200+
return 0
198201

199202
##############################
200203
# Scrolling

PuppeteerLibrary/keywords/mouseevent.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def mouse_up(self):
3434
@keyword
3535
def mouse_move(self, x, y):
3636
"""Move mouse to position x, y.
37+
38+
*Limitation* Drag event only support for css / id locator. Not support for xpath locator or chain locator.
3739
"""
3840
self.info(f"Mouse move to x: '{x}', y: '{y}'.")
3941
return self.loop.run_until_complete(self.get_async_keyword_group().mouse_move(x, y))
42+
43+
@keyword
44+
def drag_and_drop(self, src_locator, desc_locator):
45+
"""Drag item form sort locator to destination locator
46+
47+
*Limitation* Drag event only support for css / id locator. Not support for xpath locator or chain locator.
48+
"""
49+
self.info(f"Draf from '{src_locator}' to '{desc_locator}'.")
50+
return self.loop.run_until_complete(self.get_async_keyword_group().drag_and_drop(src_locator, desc_locator))

PuppeteerLibrary/keywords/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def set_timeout(self, timeout):
2626
2727
"""
2828
orig_timeout = self.ctx.timeout
29-
self.ctx.get_current_library_context().timeout = timestr_to_secs(timeout)
29+
self.ctx.get_current_library_context().set_default_timeout(timestr_to_secs(timeout))
3030
self.info('Original timeout is ' + str(orig_timeout) + ' seconds')
3131
return orig_timeout
3232

PuppeteerLibrary/library_context/ilibrary_context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ async def stop_server(self):
2525
def is_server_started(self) -> bool:
2626
pass
2727

28+
@abstractmethod
29+
def set_default_timeout(self, timeout):
30+
pass
31+
2832
@abstractmethod
2933
async def create_new_page(self, options: dict={}) -> BasePage:
3034
pass

PuppeteerLibrary/playwright/async_keywords/playwright_mouseevent.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from PuppeteerLibrary.locators import SelectorAbstraction
12
from PuppeteerLibrary.utils.coverter import str2int, str2str
23
import time
34
from robot.libraries.BuiltIn import BuiltIn
@@ -30,3 +31,8 @@ async def mouse_move(self, x, y):
3031
x = str2int(x)
3132
y = str2int(y)
3233
await self.library_ctx.get_current_page().get_page().mouse.move(x, y)
34+
35+
async def drag_and_drop(self, src_locator, desc_locator):
36+
src_locator_value = SelectorAbstraction.get_selector(src_locator)
37+
desc_locator_value = SelectorAbstraction.get_selector(desc_locator)
38+
await self.library_ctx.get_current_page().get_page().drag_and_drop(src_locator_value, desc_locator_value)

PuppeteerLibrary/playwright/playwright_context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ async def start_server(self, options: dict={}):
7474
self.browser = await self.playwright.firefox.launch(
7575
headless=merged_options['headless'])
7676
self.browser.accept_downloads = True
77-
# self.current_context = await self.browser.new_context()
7877

7978
async def stop_server(self):
8079
await self.playwright.stop()
@@ -85,6 +84,10 @@ def is_server_started(self) -> bool:
8584
return True
8685
return False
8786

87+
def set_default_timeout(self, timeout):
88+
self.timeout = timeout
89+
self.get_current_page().get_page().set_default_timeout(timeout * 1000)
90+
8891
async def create_new_page(self, options: dict={}) -> BasePage:
8992
device_options = {
9093
'accept_downloads': True,
@@ -174,3 +177,4 @@ def _reset_context(self):
174177
def _reset_server_context(self):
175178
self._reset_context()
176179
self.playwright = None
180+

PuppeteerLibrary/puppeteer/async_keywords/puppeteer_mouseevent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ async def mouse_move(self, x, y):
2626
x = str2int(x)
2727
y = str2int(y)
2828
await self.library_ctx.get_current_page().get_page().mouse.move(x, y)
29+
30+
async def drag_and_drop(self, src_locator, desc_locator):
31+
raise Exception('Not supported for ptchrome. Please use pwchrome')

PuppeteerLibrary/puppeteer/puppeteer_context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ def is_server_started(self) -> bool:
8484
return True
8585
return False
8686

87+
def set_default_timeout(self, timeout):
88+
self.timeout = timeout
89+
8790
async def create_new_page(self, options: dict={}) -> BasePage:
8891
new_page = await self.browser.newPage()
8992
self.current_page = PuppeteerPage(new_page)

0 commit comments

Comments
 (0)