def test_a_region_can_be_created_with_two_elements(self, default_config): element = ElementConfig("test") element2 = ElementConfig("test2") region = Region("area", default_config).configure([element, element2]) assert_that(region._name, is_("area")) assert_that(region._config, is_(default_config)) assert_that(region.test.name, is_("test")) assert_that(region.test2.name, is_("test2"))
def test_default_timeout_is_30(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") element = Element(ElementConfig(name="my_element"), region) element.wait_until_visible() retry_with_timeout.assert_called_with(mocker.ANY, 30, log=mocker.ANY)
def test_custom_timeout_can_be_set(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") element = Element(ElementConfig(name="my_element"), region) element.wait_until_visible(timeout_seconds=15) retry_with_timeout.assert_called_with(mocker.ANY, 15, log=mocker.ANY)
def test_returns_false_if_element_not_found(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") retry_with_timeout.side_effect = ElementNotFoundError() element = Element(ElementConfig(name="my_element"), region) result = element.is_visible() assert_that(result, is_(False))
def test_exception_raised_if_element_not_found(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") retry_with_timeout.side_effect = ElementNotFoundError() element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot = mocker.Mock() with pytest.raises(ElementNotFoundError): element.wait_until_visible()
def test_default_timeout_is_30(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot = mocker.Mock() element.click() retry_with_timeout.assert_called_with(mocker.ANY, 30, log=mocker.ANY)
def test_custom_timeout_can_be_set(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot = mocker.Mock() element.click(timeout_seconds=50) retry_with_timeout.assert_called_with(mocker.ANY, 50, log=mocker.ANY)
def test_screenshot_saved_if_image_file_not_found(self, mocker, region): mocker.patch("clickshot.element.Mouse") retry_with_timeout = mocker.patch("clickshot.element.retry_with_timeout") retry_with_timeout.side_effect = FileNotFoundError() element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot = mocker.Mock() with pytest.raises(FileNotFoundError): element.wait_until_visible() element.save_last_screenshot.assert_called()
def test_returns_if_element_found(self, mocker, region): mocker.patch("clickshot.element.Mouse") Locater = mocker.patch("clickshot.element.Locater") Locater().locate.return_value = Rect(left=0, top=15, width=10, height=20) element = Element(ElementConfig(name="my_element"), region) element.wait_until_visible() Locater().locate.assert_called_with( Path("images/my_region-my_element.png"), region._boundary )
def test_click_parameters_are_applied(self, mocker, region): Mouse = mocker.patch("clickshot.element.Mouse") Locater = mocker.patch("clickshot.element.Locater") Locater().locate.return_value = Rect(left=0, top=15, width=10, height=20) element = Element( ElementConfig(name="my_element", click_offset=(2, 3)), region, ) element.click(button=Button.right, count=2) assert_that(Mouse().position, is_((7, 28))) Mouse().click.assert_called_with(button=Button.right, count=2)
def test_element_is_clicked(self, mocker, region): Mouse = mocker.patch("clickshot.element.Mouse") Locater = mocker.patch("clickshot.element.Locater") Locater().locate.return_value = Rect(left=0, top=15, width=10, height=20) element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot = mocker.Mock() element.click() assert_that(Mouse().position, is_((5, 25))) Mouse().click.assert_called_with(button=Button.left, count=1)
def test_mouse_is_moved_away_from_failsafe(self, mocker, region): Mouse = mocker.patch("clickshot.element.Mouse") position_mock = mocker.PropertyMock(return_value=(0, 0)) type(Mouse()).position = position_mock Locater = mocker.patch("clickshot.element.Locater") Locater().locate.return_value = Rect(left=0, top=15, width=10, height=20) element = Element(ElementConfig(name="my_element"), region) element.click() assert_that(position_mock.mock_calls, has_item(mock.call((10, 10))))
def test_a_region_can_be_created_with_one_element(self, default_config): element = ElementConfig("test") region = Region("screen", default_config, boundary=Rect(left=0, top=10, width=32, height=8)) region.configure([element]) assert_that(region._name, is_("screen")) assert_that(region._config, is_(default_config)) assert_that(region.test.name, is_("test")) assert_that(region._boundary, is_(Rect(left=0, top=10, width=32, height=8)))
def test_failsafe_aborts_click_attempt(self, mocker, region): Mouse = mocker.patch("clickshot.element.Mouse") position_mock = mocker.PropertyMock(return_value=(0, 0)) type(Mouse()).position = position_mock Locater = mocker.patch("clickshot.element.Locater") Locater().locate.side_effect = ElementNotFoundError() element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot = mock.Mock() with pytest.raises(ElementNotFoundError): with pytest.warns(UserWarning): element.click()
def test_failsafe_aborts_is_visible_attempt(self, mocker, region): Mouse = mocker.patch("clickshot.element.Mouse") position_mock = mocker.PropertyMock(return_value=(0, 0)) type(Mouse()).position = position_mock Locater = mocker.patch("clickshot.element.Locater") Locater().locate.side_effect = ElementNotFoundError() element = Element(ElementConfig(name="my_element"), region) with pytest.warns(UserWarning): result = element.is_visible(30) assert_that(result, is_(False))
def test_screenshot_saved(self, mocker, region, capsys): screenshot = mock.Mock() Locater = mocker.patch("clickshot.element.Locater") Locater().last_screenshot = screenshot screenshot.save.return_value = "screenshots/my_region-my-element.png" element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot() screenshot.save.assert_called_with(Path("screenshots/my_region-my_element.png")) stdout = capsys.readouterr().out assert_that( stdout, contains_string("Expected Image: images/my_region-my_element.png\n") ) assert_that( stdout, contains_string("Screenshot: screenshots/my_region-my-element.png\n"), )
def test_string_representation_is_informative(self, region): element = Element(ElementConfig(name="my_element"), region) assert_that(str(element), is_("<Element name='my_element'>"))
def test_screenshot_not_saved_if_it_is_none(self, mocker, region): Locater = mocker.patch("clickshot.element.Locater") Locater().last_screenshot = None element = Element(ElementConfig(name="my_element"), region) element.save_last_screenshot()
from clickshot import Region, ElementConfig from .config import config sidebar = Region("sidebar", config).configure([ ElementConfig("close_button", click_offset=(86, 0)), ])
from clickshot import Region, ElementConfig from .config import config browser = Region("browser", config).configure([ ElementConfig("updatescanner_button", click_offset=(-20, 0)), ])