Skip to content

Commit

Permalink
[#515] REFACTOR: command.js.click for more ...
Browse files Browse the repository at this point in the history
to be event based
  • Loading branch information
yashaka committed Mar 4, 2024
1 parent 7da34bc commit 39481af
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
32 changes: 31 additions & 1 deletion selene/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,37 @@ def func(element: Element):
click: Command[Element] = Command(
'click',
# TODO: should we process collections too? i.e. click through all elements?
lambda element: element.execute_script('element.click()'),
lambda element: element.execute_script(
'''
const offsetX = arguments[0]
const offsetY = arguments[1]
const rect = element.getBoundingClientRect()
function mouseEvent() {
if (typeof (Event) === 'function') {
return new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left + rect.width / 2 + offsetX,
clientY: rect.top + rect.height / 2 + offsetY
})
}
else {
const event = document.createEvent('MouseEvent')
event.initEvent('click', true, true)
event.type = 'click'
event.view = window
event.clientX = rect.left + rect.width / 2 + offsetX
event.clientY = rect.top + rect.height / 2 + offsetY
return event
}
}
element.dispatchEvent(mouseEvent())
''',
0,
0,
),
)

clear_local_storage: Command[Browser] = Command(
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/element__perform__js__click_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import time

from selene import command
from tests.integration.helpers.givenpage import GivenPage


def test_command_js_click_does_not_wait_for_no_overlay(session_browser):
browser = session_browser.with_(timeout=0.5)
page = GivenPage(browser.driver)
page.opened_with_body(
'''
<div
id="overlay"
style='
display:block;
position: fixed;
display: block;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.1);
z-index: 2;
cursor: pointer;
'
>
</div>
<a href="#second">go to Heading 2</a>
<h2 id="second">Heading 2</h2>
'''
)
before_call = time.time()
page.execute_script_with_timeout(
'''
document.getElementById('overlay').style.display='none'
''',
0.75,
)

browser.element('a').perform(command.js.click)

time_diff = time.time() - before_call
assert time_diff < 0.25
assert "second" in browser.driver.current_url

0 comments on commit 39481af

Please sign in to comment.