Skip to content

Commit 707454d

Browse files
committed
Fix flaky test in examples python (#2042)
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 00151b4 commit 707454d

File tree

10 files changed

+80
-82
lines changed

10 files changed

+80
-82
lines changed

examples/python/tests/actions_api/test_mouse.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import pytest
12
from time import sleep
2-
33
from selenium.webdriver import ActionChains
44
from selenium.webdriver.common.actions.action_builder import ActionBuilder
55
from selenium.webdriver.common.actions.mouse_button import MouseButton
66
from selenium.webdriver.common.by import By
7+
from selenium.webdriver.support.ui import WebDriverWait
8+
from selenium.webdriver.support import expected_conditions as EC
79

810

911
def test_click_and_hold(driver):
1012
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
1113

1214
clickable = driver.find_element(By.ID, "clickable")
13-
ActionChains(driver)\
14-
.click_and_hold(clickable)\
15+
ActionChains(driver) \
16+
.click_and_hold(clickable) \
1517
.perform()
1618

1719
sleep(0.5)
@@ -22,8 +24,8 @@ def test_click_and_release(driver):
2224
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
2325

2426
clickable = driver.find_element(By.ID, "click")
25-
ActionChains(driver)\
26-
.click(clickable)\
27+
ActionChains(driver) \
28+
.click(clickable) \
2729
.perform()
2830

2931
assert "resultPage.html" in driver.current_url
@@ -33,8 +35,8 @@ def test_right_click(driver):
3335
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
3436

3537
clickable = driver.find_element(By.ID, "clickable")
36-
ActionChains(driver)\
37-
.context_click(clickable)\
38+
ActionChains(driver) \
39+
.context_click(clickable) \
3840
.perform()
3941

4042
sleep(0.5)
@@ -72,8 +74,8 @@ def test_double_click(driver):
7274
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
7375

7476
clickable = driver.find_element(By.ID, "clickable")
75-
ActionChains(driver)\
76-
.double_click(clickable)\
77+
ActionChains(driver) \
78+
.double_click(clickable) \
7779
.perform()
7880

7981
assert driver.find_element(By.ID, "click-status").text == "double-clicked"
@@ -83,8 +85,8 @@ def test_hover(driver):
8385
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
8486

8587
hoverable = driver.find_element(By.ID, "hover")
86-
ActionChains(driver)\
87-
.move_to_element(hoverable)\
88+
ActionChains(driver) \
89+
.move_to_element(hoverable) \
8890
.perform()
8991

9092
assert driver.find_element(By.ID, "move-status").text == "hovered"
@@ -94,8 +96,8 @@ def test_move_by_offset_from_element(driver):
9496
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
9597

9698
mouse_tracker = driver.find_element(By.ID, "mouse-tracker")
97-
ActionChains(driver)\
98-
.move_to_element_with_offset(mouse_tracker, 8, 0)\
99+
ActionChains(driver) \
100+
.move_to_element_with_offset(mouse_tracker, 8, 0) \
99101
.perform()
100102

101103
coordinates = driver.find_element(By.ID, "relative-location").text.split(", ")
@@ -104,7 +106,7 @@ def test_move_by_offset_from_element(driver):
104106

105107
def test_move_by_offset_from_viewport_origin_ab(driver):
106108
driver.get('https://selenium.dev/selenium/web/mouse_interaction.html')
107-
109+
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "absolute-location")))
108110
action = ActionBuilder(driver)
109111
action.pointer_action.move_to_location(8, 0)
110112
action.perform()
@@ -121,8 +123,8 @@ def test_move_by_offset_from_current_pointer_ab(driver):
121123
action.pointer_action.move_to_location(6, 3)
122124
action.perform()
123125

124-
ActionChains(driver)\
125-
.move_by_offset( 13, 15)\
126+
ActionChains(driver) \
127+
.move_by_offset(13, 15) \
126128
.perform()
127129

128130
coordinates = driver.find_element(By.ID, "absolute-location").text.split(", ")
@@ -136,8 +138,8 @@ def test_drag_and_drop_onto_element(driver):
136138

137139
draggable = driver.find_element(By.ID, "draggable")
138140
droppable = driver.find_element(By.ID, "droppable")
139-
ActionChains(driver)\
140-
.drag_and_drop(draggable, droppable)\
141+
ActionChains(driver) \
142+
.drag_and_drop(draggable, droppable) \
141143
.perform()
142144

143145
assert driver.find_element(By.ID, "drop-status").text == "dropped"
@@ -149,15 +151,8 @@ def test_drag_and_drop_by_offset(driver):
149151
draggable = driver.find_element(By.ID, "draggable")
150152
start = draggable.location
151153
finish = driver.find_element(By.ID, "droppable").location
152-
ActionChains(driver)\
153-
.drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y'])\
154+
ActionChains(driver) \
155+
.drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y']) \
154156
.perform()
155157

156158
assert driver.find_element(By.ID, "drop-status").text == "dropped"
157-
158-
159-
160-
161-
162-
163-
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import pytest
2+
import trio
23
from selenium.webdriver.common.by import By
34
from selenium.webdriver.common.log import Log
4-
5+
from selenium.webdriver.support.ui import WebDriverWait
6+
from selenium.webdriver.support import expected_conditions as EC
57

68
@pytest.mark.trio
79
async def test_mutation(driver):
810
async with driver.bidi_connection() as session:
911
async with Log(driver, session).mutation_events() as event:
10-
driver.get('https://www.selenium.dev/selenium/web/dynamic.html')
11-
driver.find_element(By.ID, "reveal").click()
12+
await trio.to_thread.run_sync(lambda: driver.get('https://www.selenium.dev/selenium/web/dynamic.html'))
13+
await trio.to_thread.run_sync(lambda: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "reveal"))))
14+
await trio.to_thread.run_sync(lambda: driver.find_element(By.ID, "reveal").click())
1215

1316
assert event["element"] == driver.find_element(By.ID, "revealed")

website_and_docs/content/documentation/webdriver/actions_api/mouse.en.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This is useful for focusing a specific element:
2525
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L22-L25" >}}
2626
{{< /tab >}}
2727
{{< tab header="Python" >}}
28-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L12-L15" >}}
28+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L14-L17" >}}
2929
{{< /tab >}}
3030
{{< tab header="CSharp" >}}
3131
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L17-L20" >}}
@@ -51,7 +51,7 @@ This is otherwise known as "clicking":
5151
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L34-L37" >}}
5252
{{< /tab >}}
5353
{{< tab header="Python" >}}
54-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L24-L27" >}}
54+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L26-L29" >}}
5555
{{< /tab >}}
5656
{{< tab header="CSharp" >}}
5757
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L30-L33" >}}
@@ -86,7 +86,7 @@ This is otherwise known as "right-clicking":
8686
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L46-L49" >}}
8787
{{< /tab >}}
8888
{{< tab header="Python" >}}
89-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L35-L38" >}}
89+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L37-L40" >}}
9090
{{< /tab >}}
9191
{{< tab header="CSharp" >}}
9292
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L43-L46" >}}
@@ -112,7 +112,7 @@ There is no convenience method for this, it is just pressing and releasing mouse
112112
{{< /tab >}}
113113
{{< tab header="Python" >}}
114114
{{< badge-version version="4.2" >}}
115-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L49-L52" >}}
115+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L51-L54" >}}
116116
{{< /tab >}}
117117
{{< tab header="CSharp" >}}
118118
{{< badge-version version="4.2" >}}
@@ -141,7 +141,7 @@ There is no convenience method for this, it is just pressing and releasing mouse
141141
{{< /tab >}}
142142
{{< tab header="Python" >}}
143143
{{< badge-version version="4.2" >}}
144-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L63-L66" >}}
144+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L65-L68" >}}
145145
{{< /tab >}}
146146
{{< tab header="CSharp" >}}
147147
{{< badge-version version="4.2" >}}
@@ -169,7 +169,7 @@ This method combines moving to the center of an element with pressing and releas
169169
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L93-L96" >}}
170170
{{< /tab >}}
171171
{{< tab header="Python" >}}
172-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L74-L77" >}}
172+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L76-L79" >}}
173173
{{< /tab >}}
174174
{{< tab header="CSharp" >}}
175175
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L91-L94" >}}
@@ -196,7 +196,7 @@ Note that the element must be in the viewport or else the command will error.
196196
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L105-L108" >}}
197197
{{< /tab >}}
198198
{{< tab header="Python" >}}
199-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L85-L88" >}}
199+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L87-L90" >}}
200200
{{< /tab >}}
201201
{{< tab header="CSharp" >}}
202202
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L104-L107" >}}
@@ -228,7 +228,7 @@ then moves by the provided offset.
228228
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L118-L121" >}}
229229
{{< /tab >}}
230230
{{< tab header="Python" >}}
231-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L96-L99" >}}
231+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L98-L101" >}}
232232
{{< /tab >}}
233233
{{< tab header="CSharp" >}}
234234
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L132-L135" >}}
@@ -254,7 +254,7 @@ offset.
254254
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L131-L136" >}}
255255
{{< /tab >}}
256256
{{< tab header="Python" >}}
257-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L108-L110" >}}
257+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L110-L112" >}}
258258
{{< /tab >}}
259259
{{< tab header="CSharp" >}}
260260
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L146-L150" >}}
@@ -286,7 +286,7 @@ the current mouse position.
286286
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L153-L155" >}}
287287
{{< /tab >}}
288288
{{< tab header="Python" >}}
289-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L124-L126" >}}
289+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L126-L128" >}}
290290
{{< /tab >}}
291291
{{< tab header="CSharp" >}}
292292
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L167-L169" >}}
@@ -312,7 +312,7 @@ moves to the location of the target element and then releases the mouse.
312312
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L166-L170" >}}
313313
{{< /tab >}}
314314
{{< tab header="Python" >}}
315-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L137-L141" >}}
315+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L139-L143" >}}
316316
{{< /tab >}}
317317
{{< tab header="CSharp" >}}
318318
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L181-L185" >}}
@@ -337,7 +337,7 @@ This method firstly performs a click-and-hold on the source element, moves to th
337337
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L179-L184" >}}
338338
{{< /tab >}}
339339
{{< tab header="Python" >}}
340-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L149-L154" >}}
340+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L151-L156" >}}
341341
{{< /tab >}}
342342
{{< tab header="CSharp" >}}
343343
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L195-L200" >}}

website_and_docs/content/documentation/webdriver/actions_api/mouse.ja.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This is useful for focusing a specific element:
2525
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L22-L25" >}}
2626
{{< /tab >}}
2727
{{< tab header="Python" >}}
28-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L12-L15" >}}
28+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L14-L17" >}}
2929
{{< /tab >}}
3030
{{< tab header="CSharp" >}}
3131
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L17-L20" >}}
@@ -51,7 +51,7 @@ This is otherwise known as "clicking":
5151
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L34-L37" >}}
5252
{{< /tab >}}
5353
{{< tab header="Python" >}}
54-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L24-L27" >}}
54+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L26-L29" >}}
5555
{{< /tab >}}
5656
{{< tab header="CSharp" >}}
5757
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L30-L33" >}}
@@ -86,7 +86,7 @@ This is otherwise known as "right-clicking":
8686
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L46-L49" >}}
8787
{{< /tab >}}
8888
{{< tab header="Python" >}}
89-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L35-L38" >}}
89+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L37-L40" >}}
9090
{{< /tab >}}
9191
{{< tab header="CSharp" >}}
9292
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L43-L46" >}}
@@ -112,7 +112,7 @@ There is no convenience method for this, it is just pressing and releasing mouse
112112
{{< /tab >}}
113113
{{< tab header="Python" >}}
114114
{{< badge-version version="4.2" >}}
115-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L49-L52" >}}
115+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L51-L54" >}}
116116
{{< /tab >}}
117117
{{< tab header="CSharp" >}}
118118
{{< badge-version version="4.2" >}}
@@ -141,7 +141,7 @@ There is no convenience method for this, it is just pressing and releasing mouse
141141
{{< /tab >}}
142142
{{< tab header="Python" >}}
143143
{{< badge-version version="4.2" >}}
144-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L63-L66" >}}
144+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L65-L68" >}}
145145
{{< /tab >}}
146146
{{< tab header="CSharp" >}}
147147
{{< badge-version version="4.2" >}}
@@ -169,7 +169,7 @@ This method combines moving to the center of an element with pressing and releas
169169
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L93-L96" >}}
170170
{{< /tab >}}
171171
{{< tab header="Python" >}}
172-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L74-L77" >}}
172+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L76-L79" >}}
173173
{{< /tab >}}
174174
{{< tab header="CSharp" >}}
175175
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L91-L94" >}}
@@ -196,7 +196,7 @@ Note that the element must be in the viewport or else the command will error.
196196
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L105-L108" >}}
197197
{{< /tab >}}
198198
{{< tab header="Python" >}}
199-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L85-L88" >}}
199+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L87-L90" >}}
200200
{{< /tab >}}
201201
{{< tab header="CSharp" >}}
202202
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L104-L107" >}}
@@ -228,7 +228,7 @@ then moves by the provided offset.
228228
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L118-L121" >}}
229229
{{< /tab >}}
230230
{{< tab header="Python" >}}
231-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L96-L99" >}}
231+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L98-L101" >}}
232232
{{< /tab >}}
233233
{{< tab header="CSharp" >}}
234234
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L132-L135" >}}
@@ -254,7 +254,7 @@ offset.
254254
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L131-L136" >}}
255255
{{< /tab >}}
256256
{{< tab header="Python" >}}
257-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L108-L110" >}}
257+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L110-L112" >}}
258258
{{< /tab >}}
259259
{{< tab header="CSharp" >}}
260260
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L146-L150" >}}
@@ -286,7 +286,7 @@ the current mouse position.
286286
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L153-L155" >}}
287287
{{< /tab >}}
288288
{{< tab header="Python" >}}
289-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L124-L126" >}}
289+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L126-L128" >}}
290290
{{< /tab >}}
291291
{{< tab header="CSharp" >}}
292292
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L167-L169" >}}
@@ -312,7 +312,7 @@ moves to the location of the target element and then releases the mouse.
312312
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L166-L170" >}}
313313
{{< /tab >}}
314314
{{< tab header="Python" >}}
315-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L137-L141" >}}
315+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L139-L143" >}}
316316
{{< /tab >}}
317317
{{< tab header="CSharp" >}}
318318
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L181-L185" >}}
@@ -337,7 +337,7 @@ This method firstly performs a click-and-hold on the source element, moves to th
337337
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java#L179-L184" >}}
338338
{{< /tab >}}
339339
{{< tab header="Python" >}}
340-
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L149-L154" >}}
340+
{{< gh-codeblock path="/examples/python/tests/actions_api/test_mouse.py#L151-L156" >}}
341341
{{< /tab >}}
342342
{{< tab header="CSharp" >}}
343343
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs#L195-L200" >}}

0 commit comments

Comments
 (0)