def test_nested_stepsfiles(self): """Test that stepsfiles within stepsfiles are correctly handled.""" # phisically create the files -- mocking os.open() would be too cumbersome stepsfile1 = self._create_temp_file( extension=".steps", contents="item_for_text.txt some_text_matchfile.match") # second step file contains a reference to the first stepsfile2 = self._create_temp_file(extension=".steps", contents=stepsfile1) stepsfile3_contents = [ "item_for_contour.png some_contour_matchfile.match", "item_for_cascade.xml some_cascade_matchfile.match", # third step file contains a reference to the second stepsfile2 ] stepsfile3 = self._create_temp_file( prefix=self.stepsfile_name, extension=".steps", contents=os.linesep.join(stepsfile3_contents)) chain = Chain(stepsfile3) expected_types = [Image, Pattern, Text] self.assertEqual([type(s) for s in chain._steps], expected_types)
def test_hybrid_nomatch(self): finder = HybridFinder() finder.configure_backend("autopy") finder.synchronize_backend("autopy") finder.params["find"]["similarity"].value = 1.0 matches = finder.find(Chain('circle_fake'), Image('all_shapes')) # verify match accuracy self.assertEqual(len(matches), 0) # verify dumped files count and names (4+4+7) dumps = self._verify_and_get_dumps(15, multistep=True)
def _build_chain(self, stepsfile_contents, stepsfile=None): """ Create an instance of :py:class:`guibot.target.Chain` to be used by the tests. :param str stepsfile_contents: contents for the stepsfile to be passed when creating the finder :param str stepsfile: name of the stepsfile to load or None to use the default :returns: an instance of the finder :rtype: :py:class:`finder.Finder` """ filename = self._create_temp_file(prefix=self.stepsfile_name, extension=".steps", contents=stepsfile_contents) return Chain(os.path.splitext(filename)[0])
def test_hybrid_multiconfig(self): finder = HybridFinder() finder.configure_backend("autopy") finder.synchronize_backend("autopy") finder.params["find"]["similarity"].value = 1.0 matches = finder.find(Chain('circle'), Image('all_shapes')) # verify match accuracy self.assertEqual(len(matches), 1) self.assertEqual(matches[0].x, 104) self.assertEqual(matches[0].y, 10) # verify dumped files count and names (+1 as we match with template) dumps = self._verify_and_get_dumps(9, multistep=True)
def test_stepsfile_lookup(self): """Test that the stepsfile will be searched using :py:class:`guibot.fileresolver.FileResolver`.""" tmp_dir = mkdtemp() tmp_steps_file = "{}/{}.steps".format(tmp_dir, self.stepsfile_missing) with open(tmp_steps_file, "w") as fp: fp.write("image_for_autopy.png some_autopy_matchfile.match") filename = os.path.basename(os.path.splitext(tmp_steps_file)[0]) try: with patch("guibot.fileresolver.FileResolver.search", wraps=lambda _: tmp_steps_file) as mock_search: Chain(self.stepsfile_missing) # but make sure we did search for the "missing" stepsfile mock_search.assert_any_call("{}.steps".format(filename)) finally: os.unlink(tmp_steps_file) os.rmdir(tmp_dir)
def test_hybrid_same(self): finder = HybridFinder() finder.configure_backend("autopy") finder.synchronize_backend("autopy") finder.params["find"]["similarity"].value = 1.0 matches = finder.find(Chain('circle_simple'), Image('all_shapes')) # verify match accuracy self.assertEqual(len(matches), 1) self.assertEqual(matches[0].x, 104) self.assertEqual(matches[0].y, 10) # verify dumped files count and names dumps = self._verify_and_get_dumps(4) self._verify_dumped_images('shape_blue_circle', 'all_shapes', dumps, "autopy") self._verify_single_hotmap(dumps, "autopy")
def test_step_save(self): """Test that dumping a chain to a file works and that the content is preserved.""" # The Text target accepts either a file or a text string and we test # with both modes. For the first mode we need a real file. text_file = self._create_temp_file(prefix="some_text_file", extension=".txt") with open(text_file, "w") as fp: fp.write("ocr_string") # create real temp files for these -- they are saved using open() and we are not # mocking those calls. Also, the temp files will automatically be removed on tear down deep_csv = self._create_temp_file(prefix="item_for_deep", extension=".csv") cascade_xml = self._create_temp_file(prefix="item_for_cascade", extension=".xml") # no need to mock png files -- the Image target uses PIL.Image.save(), which we mocked # destination stepfile target_filename = self._create_temp_file(extension=".steps") stepsfile_contents = [ "item_for_contour.png some_contour_matchfile.match", "item_for_tempfeat.png some_tempfeat_matchfile.match", "item_for_feature.png some_feature_matchfile.match", "{} some_deep_matchfile.match".format(deep_csv), "17 some_deep_matchfile.match", "{} some_cascade_matchfile.match".format(cascade_xml), "item_for_template.png some_template_matchfile.match", "item_for_autopy.png some_autopy_matchfile.match", "{} some_text_matchfile.match".format( os.path.splitext(text_file)[0]), "some_text_content some_text_matchfile.match" ] expected_content = [ "item_for_contour.png item_for_contour.match", "item_for_tempfeat.png item_for_tempfeat.match", "item_for_feature.png item_for_feature.match", "{0}.csv {0}.match".format(os.path.splitext(deep_csv)[0]), "17 17.match", "{0}.xml {0}.match".format(os.path.splitext(cascade_xml)[0]), "item_for_template.png item_for_template.match", "item_for_autopy.png item_for_autopy.match", "{0}.txt {0}.match".format(os.path.splitext(text_file)[0]), "some_text_content some_text_content.match" ] source_stepsfile = self._create_temp_file( prefix=self.stepsfile_name, extension=".steps", contents=os.linesep.join(stepsfile_contents)) FileResolver().add_path(os.path.dirname(text_file)) try: chain = Chain(os.path.splitext(source_stepsfile)[0]) chain.save(target_filename) with open(target_filename, "r") as f: generated_content = f.read().splitlines() # assert that the generated steps file has the expected content self.assertEqual(generated_content, expected_content) # build a list of the match filenames generated from # the calls to `Finder.to_match_file()` generated_match_names = [] for c in self.mock_match_write.call_args_list: generated_match_names.append(c[0][1]) # get a list expected_match_names = [x.split("\t")[1] for x in expected_content] expected_match_names.insert( 0, os.path.splitext(source_stepsfile)[0] + ".match") # and assert that a match file was generated for each line # and for the steps file itself self.assertEqual(generated_match_names, expected_match_names) finally: FileResolver().remove_path(os.path.dirname(text_file))