Skip to content

Commit 8120ef4

Browse files
authored
Supress StaleElementReferenceException in waiting keywords (#1269)
Fixes #1270
1 parent 282fddd commit 8120ef4

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/SeleniumLibrary/keywords/waiting.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import time
1818

19+
from selenium.common.exceptions import StaleElementReferenceException
20+
1921
from SeleniumLibrary.base import LibraryComponent, keyword
2022
from SeleniumLibrary.errors import ElementNotFound
2123
from SeleniumLibrary.utils import is_noney, secs_to_timestr
@@ -228,6 +230,9 @@ def _wait_until_worker(self, condition, timeout, error):
228230
return
229231
except ElementNotFound as err:
230232
not_found = str(err)
233+
except StaleElementReferenceException as err:
234+
self.info('Suppressing StaleElementReferenceException from Selenium.')
235+
not_found = err
231236
else:
232237
not_found = None
233238
time.sleep(0.2)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import unittest
2+
3+
from selenium.common.exceptions import StaleElementReferenceException
4+
from mockito import mock, when, unstub
5+
6+
from SeleniumLibrary.keywords import WaitingKeywords
7+
8+
9+
def _raise(*a):
10+
raise StaleElementReferenceException('Darn')
11+
12+
13+
class TableKeywordsTest(unittest.TestCase):
14+
15+
@classmethod
16+
def setUpClass(cls):
17+
cls.ctx = mock()
18+
cls.waiting = WaitingKeywords(cls.ctx)
19+
cls.timeout = 0.5
20+
cls.count = 0
21+
22+
def tearDown(self):
23+
unstub()
24+
25+
def test_wait_until_element_is_visible(self):
26+
locator = '//div'
27+
element = mock()
28+
when(self.waiting).find_element(locator, required=False).thenReturn(element)
29+
when(element).is_displayed().thenRaise(StaleElementReferenceException()).thenReturn(True)
30+
self.waiting.wait_until_element_is_visible(locator, self.timeout)
31+
32+
def test_wait_until_element_is_visible_fails(self):
33+
locator = '//div'
34+
element = mock()
35+
when(self.waiting).find_element(locator, required=False).thenReturn(element)
36+
when(element).is_displayed().thenRaise(StaleElementReferenceException('foo'))
37+
with self.assertRaisesRegexp(AssertionError, 'Message: foo'):
38+
self.waiting.wait_until_element_is_visible(locator, self.timeout)
39+
40+
def test_wait_until_element_is_not_visible(self):
41+
locator = '//div'
42+
element = mock()
43+
when(self.waiting).find_element(locator, required=False).thenReturn(element)
44+
when(element).is_displayed().thenRaise(StaleElementReferenceException()).thenReturn(False)
45+
self.waiting.wait_until_element_is_not_visible(locator, self.timeout)
46+
47+
def test_wait_until_element_is_enabled(self):
48+
locator = '//div'
49+
element = mock()
50+
when(self.waiting).find_element(locator, None).thenReturn(element)
51+
when(element).is_enabled().thenRaise(StaleElementReferenceException()).thenReturn(True)
52+
self.waiting.wait_until_element_is_enabled(locator, self.timeout)
53+
54+
def test_wait_until_element_is_enabled_get_attribute_readonly(self):
55+
locator = '//div'
56+
element = mock()
57+
when(self.waiting).find_element(locator, None).thenReturn(element)
58+
when(element).is_enabled().thenReturn(True)
59+
when(element).get_attribute('readonly').thenRaise(StaleElementReferenceException()).thenReturn(None)
60+
self.waiting.wait_until_element_is_enabled(locator, self.timeout)
61+
62+
def test_wait_until_element_is_enabled_fails(self):
63+
locator = '//div'
64+
element = mock()
65+
when(self.waiting).find_element(locator, None).thenReturn(element)
66+
when(element).is_enabled().thenRaise(StaleElementReferenceException('foo'))
67+
with self.assertRaisesRegexp(AssertionError, 'Message: foo'):
68+
self.waiting.wait_until_element_is_enabled(locator, self.timeout)
69+
70+
def test_wait_until_element_contains(self):
71+
locator = '//div'
72+
text = 'foo'
73+
element1, element2 = mock(), mock({'text': 'foobar'})
74+
element1.__class__.text = property(_raise)
75+
when(self.waiting).find_element(locator).thenReturn(element1).thenReturn(element2)
76+
self.waiting.wait_until_element_contains(locator, text, self.timeout)
77+
78+
def test_wait_until_element_does_not_contain(self):
79+
locator = '//div'
80+
text = 'foo'
81+
element1, element2 = mock(), mock({'text': 'tidii'})
82+
element1.__class__.text = property(_raise)
83+
when(self.waiting).find_element(locator).thenReturn(element1).thenReturn(element2)
84+
self.waiting.wait_until_element_does_not_contain(locator, text, self.timeout)

0 commit comments

Comments
 (0)