|
1 |
| -import unittest |
2 |
| - |
| 1 | +import pytest |
3 | 2 | from selenium.common.exceptions import StaleElementReferenceException
|
4 | 3 | from mockito import mock, when, unstub
|
5 | 4 |
|
6 | 5 | from SeleniumLibrary.keywords import WaitingKeywords
|
7 | 6 |
|
| 7 | +TIMEOUT = 0.5 |
| 8 | + |
8 | 9 |
|
9 | 10 | def _raise(*a):
|
10 | 11 | raise StaleElementReferenceException('Darn')
|
11 | 12 |
|
12 | 13 |
|
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) |
| 14 | + |
| 15 | +@pytest.fixture(scope='module') |
| 16 | +def waiting(): |
| 17 | + return WaitingKeywords(mock()) |
| 18 | + |
| 19 | + |
| 20 | +def teardown_module(): |
| 21 | + unstub() |
| 22 | + |
| 23 | + |
| 24 | +def test_wait_until_element_is_visible(waiting): |
| 25 | + locator = '//div' |
| 26 | + element = mock() |
| 27 | + when(waiting).find_element(locator, required=False).thenReturn(element) |
| 28 | + when(element).is_displayed().thenRaise(StaleElementReferenceException()).thenReturn(True) |
| 29 | + waiting.wait_until_element_is_visible(locator, TIMEOUT) |
| 30 | + |
| 31 | + |
| 32 | +def test_wait_until_element_is_visible_fails(waiting): |
| 33 | + locator = '//div' |
| 34 | + element = mock() |
| 35 | + when(waiting).find_element(locator, required=False).thenReturn(element) |
| 36 | + when(element).is_displayed().thenRaise(StaleElementReferenceException('foo')) |
| 37 | + with pytest.raises(AssertionError) as error: |
| 38 | + waiting.wait_until_element_is_visible(locator, TIMEOUT) |
| 39 | + assert 'Message: foo' in str(error.value) |
| 40 | + |
| 41 | + |
| 42 | +def test_wait_until_element_is_not_visible(waiting): |
| 43 | + locator = '//div' |
| 44 | + element = mock() |
| 45 | + when(waiting).find_element(locator, required=False).thenReturn(element) |
| 46 | + when(element).is_displayed().thenRaise(StaleElementReferenceException()).thenReturn(False) |
| 47 | + waiting.wait_until_element_is_not_visible(locator, TIMEOUT) |
| 48 | + |
| 49 | + |
| 50 | +def test_wait_until_element_is_enabled(waiting): |
| 51 | + locator = '//div' |
| 52 | + element = mock() |
| 53 | + when(waiting).find_element(locator, None).thenReturn(element) |
| 54 | + when(element).is_enabled().thenRaise(StaleElementReferenceException()).thenReturn(True) |
| 55 | + waiting.wait_until_element_is_enabled(locator, TIMEOUT) |
| 56 | + |
| 57 | + |
| 58 | +def test_wait_until_element_is_enabled_get_attribute_readonly(waiting): |
| 59 | + locator = '//div' |
| 60 | + element = mock() |
| 61 | + when(waiting).find_element(locator, None).thenReturn(element) |
| 62 | + when(element).is_enabled().thenReturn(True) |
| 63 | + when(element).get_attribute('readonly').thenRaise(StaleElementReferenceException()).thenReturn(None) |
| 64 | + waiting.wait_until_element_is_enabled(locator, TIMEOUT) |
| 65 | + |
| 66 | + |
| 67 | +def test_wait_until_element_is_enabled_fails(waiting): |
| 68 | + locator = '//div' |
| 69 | + element = mock() |
| 70 | + when(waiting).find_element(locator, None).thenReturn(element) |
| 71 | + when(element).is_enabled().thenRaise(StaleElementReferenceException('foo')) |
| 72 | + with pytest.raises(AssertionError) as error: |
| 73 | + waiting.wait_until_element_is_enabled(locator, TIMEOUT) |
| 74 | + assert 'Message: foo' in str(error.value) |
| 75 | + |
| 76 | + |
| 77 | +def test_wait_until_element_contains(waiting): |
| 78 | + locator = '//div' |
| 79 | + text = 'foo' |
| 80 | + element1, element2 = mock(), mock({'text': 'foobar'}) |
| 81 | + element1.__class__.text = property(_raise) |
| 82 | + when(waiting).find_element(locator).thenReturn(element1).thenReturn(element2) |
| 83 | + waiting.wait_until_element_contains(locator, text, TIMEOUT) |
| 84 | + |
| 85 | + |
| 86 | +def test_wait_until_element_does_not_contain(waiting): |
| 87 | + locator = '//div' |
| 88 | + text = 'foo' |
| 89 | + element1, element2 = mock(), mock({'text': 'tidii'}) |
| 90 | + element1.__class__.text = property(_raise) |
| 91 | + when(waiting).find_element(locator).thenReturn(element1).thenReturn(element2) |
| 92 | + waiting.wait_until_element_does_not_contain(locator, text, TIMEOUT) |
0 commit comments