Skip to content

Commit cdf764b

Browse files
authored
Merge pull request #1242 from seleniumbase/improve-recorder-and-click-with-offset
Improve Recorder Mode and click_with_offset()
2 parents 164878c + 0768c3d commit cdf764b

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

help_docs/method_summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ self.click_if_visible(selector, by=By.CSS_SELECTOR)
130130

131131
self.click_active_element()
132132

133-
self.click_with_offset(selector, x, y, by=By.CSS_SELECTOR, mark=False, timeout=None)
133+
self.click_with_offset(selector, x, y, by=By.CSS_SELECTOR, mark=None, timeout=None)
134134

135135
self.is_selected(selector, by=By.CSS_SELECTOR, timeout=None)
136136
# Duplicates: self.is_checked(selector, by=By.CSS_SELECTOR, timeout=None)

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "2.4.16"
2+
__version__ = "2.4.17"

seleniumbase/extensions/recorder.zip

90 Bytes
Binary file not shown.

seleniumbase/fixtures/base_case.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,12 +1734,13 @@ def click_active_element(self):
17341734
self.__slow_mode_pause_if_active()
17351735

17361736
def click_with_offset(
1737-
self, selector, x, y, by=By.CSS_SELECTOR, mark=False, timeout=None
1737+
self, selector, x, y, by=By.CSS_SELECTOR, mark=None, timeout=None
17381738
):
17391739
"""
17401740
Click an element at an {X,Y}-offset location.
17411741
{0,0} is the top-left corner of the element.
17421742
If mark==True, will draw a dot at location. (Useful for debugging)
1743+
In Demo Mode, mark becomes True unless set to False. (Default: None)
17431744
"""
17441745
from selenium.webdriver.common.action_chains import ActionChains
17451746

@@ -1752,23 +1753,26 @@ def click_with_offset(
17521753
element = page_actions.wait_for_element_visible(
17531754
self.driver, selector, by, timeout
17541755
)
1755-
self.__demo_mode_highlight_if_active(selector, by)
1756+
if self.demo_mode:
1757+
self.highlight(selector, by=by, loops=1)
1758+
elif self.slow_mode:
1759+
self.__slow_scroll_to_element(element)
1760+
if self.demo_mode and mark is None:
1761+
mark = True
17561762
if mark:
17571763
selector = self.convert_to_css_selector(selector, by=by)
17581764
selector = re.escape(selector)
17591765
selector = self.__escape_quotes_if_needed(selector)
1760-
px = x - 2
1761-
if px < 0:
1762-
px = 0
1763-
py = y - 2
1764-
if py < 0:
1765-
py = 0
1766+
px = x - 3
1767+
py = y - 3
17661768
script = (
17671769
"var canvas = document.querySelector('%s');"
17681770
"var ctx = canvas.getContext('2d');"
1771+
"ctx.fillStyle = '#F8F808';"
1772+
"ctx.fillRect(%s, %s, 7, 7);"
17691773
"ctx.fillStyle = '#F80808';"
1770-
"ctx.fillRect(%s, %s, 5, 5);"
1771-
% (selector, px, py)
1774+
"ctx.fillRect(%s+1, %s+1, 5, 5);"
1775+
% (selector, px, py, px, py)
17721776
)
17731777
self.execute_script(script)
17741778
try:
@@ -3739,6 +3743,19 @@ def __process_recorded_actions(self):
37393743
sb_actions.append('self.%s("%s")' % (method, action[1]))
37403744
else:
37413745
sb_actions.append("self.%s('%s')" % (method, action[1]))
3746+
elif action[0] == "canva":
3747+
method = "click_with_offset"
3748+
selector = action[1][0]
3749+
p_x = action[1][1]
3750+
p_y = action[1][2]
3751+
if '"' not in selector:
3752+
sb_actions.append(
3753+
'self.%s("%s", %s, %s)' % (method, selector, p_x, p_y)
3754+
)
3755+
else:
3756+
sb_actions.append(
3757+
"self.%s('%s', %s, %s)" % (method, selector, p_x, p_y)
3758+
)
37423759
elif action[0] == "input" or action[0] == "js_ty":
37433760
method = "type"
37443761
if action[0] == "js_ty":

seleniumbase/js_code/recorder_js.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
basic_tags.push('h1');
199199
basic_tags.push('h2');
200200
basic_tags.push('h3');
201+
basic_tags.push('canvas');
201202
basic_tags.push('center');
202203
basic_tags.push('input');
203204
basic_tags.push('textarea');
@@ -625,7 +626,7 @@
625626
}
626627
else
627628
document.recorded_actions.push(['click', selector, href, d_now]);
628-
// hover_click() if dropdown.
629+
// hover_click()
629630
if (el.parentElement.classList.contains('dropdown-content') &&
630631
el.parentElement.parentElement.classList.contains('dropdown'))
631632
{
@@ -641,6 +642,15 @@
641642
document.recorded_actions.pop();
642643
document.recorded_actions.push(['h_clk', pa_s, ch_s, d_now]);
643644
}
645+
else if (tag_name === 'canvas')
646+
{
647+
rect = el.getBoundingClientRect();
648+
p_x = event.clientX - rect.left;
649+
p_y = event.clientY - rect.top;
650+
c_offset = [selector, p_x, p_y];
651+
document.recorded_actions.pop();
652+
document.recorded_actions.push(['canva', c_offset, href, d_now]);
653+
}
644654
}
645655
else if (ra_len > 0 &&
646656
document.recorded_actions[ra_len-1][0] === 'mo_dn' &&

0 commit comments

Comments
 (0)