|
| 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