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_raises_error_if_image_file_doesnt_exist(self, mocker): mocker.patch("clickshot.locater.ScreenGrabber") Image = mocker.patch("clickshot.locater.Image") Image.load.side_effect = FileNotFoundError with pytest.raises(FileNotFoundError): Locater().locate("image", Rect(left=1, top=2, width=3, height=4))
def test_a_region_is_automatically_created_when_accessed( self, default_config): region = Region("screen", default_config, boundary=Rect(left=0, top=10, width=32, height=8)) assert_that(region.test.name, is_("test"))
def test_returns_rgb_image_from_mss(self, mocker, pixels, rgb_array): mss = mocker.patch("clickshot.screen_grabber.mss") mss().grab.return_value = pixels grabber = ScreenGrabber() image = grabber.grab(Rect(left=0, top=1, width=2, height=3)) numpy.testing.assert_array_equal(image.data, rgb_array)
def test_raises_error_if_element_is_not_found(self, mocker): Image = mocker.patch("clickshot.locater.Image") screenshot = Image("Screenshot") screenshot.match_template.side_effect = ElementNotFoundError ScreenGrabber = mocker.patch("clickshot.locater.ScreenGrabber") ScreenGrabber().grab.return_value = screenshot with pytest.raises(ElementNotFoundError): Locater().locate("image", Rect(left=1, top=2, width=3, height=4))
def test_last_screenshot_is_updated_by_locate(self, mocker): Image = mocker.patch("clickshot.locater.Image") screenshot = Image("Screenshot") ScreenGrabber = mocker.patch("clickshot.locater.ScreenGrabber") ScreenGrabber().grab.return_value = screenshot locater = Locater() locater.locate("image", Rect(left=1, top=2, width=3, height=4)) assert_that(locater.last_screenshot, is_(screenshot))
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_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_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_returns_location_if_element_is_found(self, mocker): Image = mocker.patch("clickshot.locater.Image") screenshot = Image("Screenshot") screenshot.match_template.return_value = Rect(left=4, top=5, width=6, height=7) ScreenGrabber = mocker.patch("clickshot.locater.ScreenGrabber") ScreenGrabber().grab.return_value = screenshot template = Image("Template") Image.load.return_value = template result = Locater().locate("image", Rect(left=1, top=2, width=3, height=4)) assert_that(result, is_(Rect(left=4, top=5, width=6, height=7))) ScreenGrabber().grab.assert_called_with( Rect(left=1, top=2, width=3, height=4)) Image.load.assert_called_with("image") screenshot.match_template.assert_called_with(template)
def test_passes_rect_parameter_to_mss(self, mocker, pixels): mss = mocker.patch("clickshot.screen_grabber.mss") mss().grab.return_value = pixels grabber = ScreenGrabber() grabber.grab(Rect(left=0, top=1, width=2, height=3)) mss().grab.assert_called_with({ "left": 0, "top": 1, "width": 2, "height": 3 })
def test_last_screenshot_is_updated_by_locate_if_image_file_doesnt_exist( self, mocker): Image = mocker.patch("clickshot.locater.Image") screenshot = Image("Screenshot") ScreenGrabber = mocker.patch("clickshot.locater.ScreenGrabber") ScreenGrabber().grab.return_value = screenshot Image.load.side_effect = FileNotFoundError locater = Locater() with pytest.raises(FileNotFoundError): locater.locate("image", Rect(left=1, top=2, width=3, height=4)) assert_that(locater.last_screenshot, is_(screenshot))
def test_resizes_from_hidpi_monitor(self, mocker, pixels, rgb_array): mss = mocker.patch("clickshot.screen_grabber.mss") mss().grab.return_value = pixels resized_image_data = numpy.array([[24]]) resize = mocker.patch("clickshot.screen_grabber.cv2.resize") resize.return_value = resized_image_data # Ask for a smaller region than is returned by mss().grab. # This indicates a HiDPI monitor, which should then be resized back to the # requested region size. grabber = ScreenGrabber() image = grabber.grab(Rect(left=0, top=0, width=2, height=1)) numpy.testing.assert_array_equal(image.data, resized_image_data) numpy.testing.assert_array_equal(resize.call_args[0][0], rgb_array) assert_that(resize.call_args[0][1], is_((2, 1)))
def region(config): region = mock.create_autospec(Region, instance=True) region._name = "my_region" region._boundary = Rect(left=5, top=10, width=20, height=30) region._config = config return region