def test_page_object_keyword_is_callable(self, get_context_mock, get_library_instance_mock): """Assert that the page object keyword is callable""" po = PageObjects(FOO_PATH) po.load_page_object("Test", "Foo") result = po.foo_keyword_1("hello") self.assertEqual(result, "foo keyword 1: hello")
def test_log_page_object_keywords(self, log_mock, get_context_mock, get_library_instance_mock): """Verify that the log_page_objects keyword logs keywords from all imported page objects""" po = PageObjects(FOO_PATH, BAR_PATH) po.log_page_object_keywords() expected_calls = [ mock.call("('Test', 'Foo'): foo_keyword_1"), mock.call("('Test', 'Bar'): bar_keyword_1, bar_keyword_2"), ] log_mock.assert_has_calls(expected_calls, any_order=True)
def test_non_namespaced_object_name(self, get_context_mock, get_library_instance_mock): """Verify that the object name is not prefixed by a namespace when there is no namespace""" with mock.patch.object(CumulusCI, "get_namespace_prefix", return_value=""): po = PageObjects(FOO_PATH) FooTestPage = importer.import_class_or_module_by_path(FOO_PATH) MockGetLibraryInstance.libs["FooTestPage"] = _PageObjectLibrary( FooTestPage()) pobj = po.get_page_object("Test", "Foo__c") self.assertEqual(pobj.object_name, "Foo__c")
def _run_task(self): kwfiles = [] processed_files = [] for library_name in self.options["path"]: kwfile = KeywordFile(library_name) try: if self.is_pageobject_library(library_name): PageObjects._reset() module = Importer().import_class_or_module_by_path( os.path.abspath(library_name)) kwfile.doc = module.__doc__ if hasattr(module, "TITLE"): kwfile.title = module.TITLE for pobj_name, pobj in sorted( PageObjects.registry.items()): pobj = PageObjects.registry[pobj_name] libname = "{}.{}".format(pobj.__module__, pobj.__name__) libdoc = LibraryDocBuilder().build(libname) libdoc.src = os.path.basename(library_name) libdoc.pobj = libname kwfile.add_keywords(libdoc, pobj_name) else: libdoc = DocumentationBuilder(library_name).build( library_name) kwfile.add_keywords(libdoc) # if we get here, we were able to process the file correctly kwfiles.append(kwfile) processed_files.append(library_name) except RobotNotRunningError as e: # oddly, robot's exception has a traceback embedded in the message, so we'll # only print out the first line to hide most of the noise self.logger.warn("unexpected error: {}".format( str(e).split("\n")[0])) try: with open(self.options["output"], "w") as f: html = self._render_html(kwfiles) f.write(html) self.logger.info("created {}".format(f.name)) except Exception as e: raise TaskOptionsError( "Unable to create output file '{}' ({})".format( self.options["output"], e.strerror)) return {"files": processed_files, "html": html}
def test_non_namespaced_object_name( self, get_context_mock, get_library_instance_mock ): """Verify that the object name is not prefixed by a namespace when there is no namespace""" with mock.patch.object(CumulusCI, "get_namespace_prefix", return_value=""): pobj = PageObjects().load_page_object("Listing", "CustomObject__c") self.assertEqual(pobj.object_name, "CustomObject__c")
def test_file_in_pythonpath(self, get_context_mock, get_library_instance_mock): """Verify we can find a page object via PYTHONPATH""" # PageObjects will throw an error if it can't find the file. # As long as this doesn't throw an error, we're golden. if HERE not in sys.path: sys.path.append(HERE) PageObjects("FooTestPage.py")
def test_exception_not_found(self, get_context_mock, get_library_instance_mock): """Verify we get an assertion of we can't find a page object file""" # make sure the folder isn't on system path by accident if HERE in sys.path: sys.path.remove(HERE) with pytest.raises( ImportError, match="Unable to find page object file 'FooTestPage.py'" ): PageObjects("FooTestPage.py")
def test_import_failed(self, get_context_mock, get_library_instance_mock): with temporary_dir() as d: with open("busted.py", "w") as f: f.write("class Busted # incomplete class\n") f.close() sys.path.append(d) with pytest.raises( ImportError, match="Unable to import page object 'busted.py': .*" ): PageObjects("busted.py")
def test_PageObject(self, get_context_mock, get_library_instance_mock): po = PageObjects() self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS) self.assertEqual(po.registry, {}) po = PageObjects(FOO_PATH, BAR_PATH) if hasattr(self, "assertCountEqual"): self.assertCountEqual(po.registry.keys(), (("Test", "Foo__c"), ("Test", "Bar__c"))) else: # gah! python3 renamed this assert self.assertItemsEqual(po.registry.keys(), (("Test", "Foo__c"), ("Test", "Bar__c"))) # The page object class will have been imported by robot.utils.Importer, # so we need to use that here to validate which class got imported. FooTestPage = importer.import_class_or_module_by_path(FOO_PATH) BarTestPage = importer.import_class_or_module_by_path(BAR_PATH) self.assertEqual(po.registry[("Test", "Foo__c")], FooTestPage) self.assertEqual(po.registry[("Test", "Bar__c")], BarTestPage)
def test_PageObject_registry_with_custom_pageobjects( self, get_context_mock, get_library_instance_mock): """Verify that custom page objects get added to the registry""" po = PageObjects(FOO_PATH, BAR_PATH) # The page object class will have been imported by robot.utils.Importer, # so we need to use that here to validate which class got imported. FooTestPage = importer.import_class_or_module_by_path(FOO_PATH) BarTestPage = importer.import_class_or_module_by_path(BAR_PATH) expected_registry = BASE_REGISTRY expected_registry.update({ ("Test", "Foo__c"): FooTestPage, ("Test", "Bar__c"): BarTestPage }) self.assertEqual(po.registry, expected_registry)
def test_load_single_page_object(self, get_context_mock, get_library_instance_mock): """Verify that we don't see page object keywords until they are explicitly requested""" po = PageObjects(FOO_PATH) # Until we request the page object, we shouldn't be able to # see the page-specific keywords self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS) # Now load the page object and verify the Foo keyword shows up po.load_page_object("Test", "Foo") self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS + ["foo_keyword_1"])
def test_PageObject(self, get_context_mock, get_library_instance_mock): """Smoke test to make sure the default registry is set up and keywords exist""" po = PageObjects() self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS) self.assertEqual(po.registry, BASE_REGISTRY)
def test_load_multiple_page_objects( self, get_context_mock, get_library_instance_mock ): """Verify that we can import multiple page objects, but only one is visible at a time This test is based on the current design of only allowing a single page object to be active at a time. That might change in the future - we might end up having page objects be pushed on a stack. """ po = PageObjects(FOO_PATH, BAR_PATH) # Until we request the page object, we shouldn't be able to # see any keywords except the core page object keywords self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS) # now load the "Foo" page object and verify only the Foo keyword # shows up and is callable. po.load_page_object("Test", "Foo") self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS + ["foo_keyword_1"]) self.assertEqual(po.foo_keyword_1("hello"), "foo keyword 1: hello") # now load the "Bar" page object and verify only the Bar keyword # shows up and is callable. po.load_page_object("Test", "Bar") self.assertEqual( po.get_keyword_names(), CORE_KEYWORDS + ["bar_keyword_1", "bar_keyword_2"] ) self.assertEqual(po.bar_keyword_1("hello"), "bar keyword 1: hello")
def test_PageObject(self, get_context_mock, get_library_instance_mock): po = PageObjects() # no page objects loaded, so get_keyword_names should only return # the core keywords self.assertEqual(po.get_keyword_names(), CORE_KEYWORDS) self.assertEqual(po.registry, {})