def test_load(self): for drv in ["dummy", "phantom", "chrome", "firefox"]: cls = cli.loadmodules(drv, ["browser", "ctrl", "screencast", "webhook", "imageproc", "content"]) self.assertEqual(cls.__name__.lower(), drv) mods = cls.listmodule() self.assertTrue("echo" in mods) self.assertTrue("invalid" not in mods) for drv in ["illegal", "hello", "world"]: cls = cli.loadmodules(drv, []) # default is "Phantom" self.assertEqual(cls.__name__, "Phantom")
def test_getlocator(self): cls = cli.loadmodules("dummy", []) drv = cls() param = { "id": "id1" } res = drv.getlocator(param) self.assertEqual(res[0], "id") self.assertEqual(res[1], "id1") param = { "name": "name1" } res = drv.getlocator(param) self.assertEqual(res[0], "name") self.assertEqual(res[1], "name1") param = { "link_text": "ltxt1" } res = drv.getlocator(param) self.assertEqual(res[0], "link text") self.assertEqual(res[1], "ltxt1") param = { "tag": "tag1" } res = drv.getlocator(param) self.assertEqual(res[0], "tag name") self.assertEqual(res[1], "tag1")
def test_defun(self): cls = cli.loadmodules("dummy", []) drv = cls() defun = { "name": "dummy", "defun": { "name": "func1", "args": ["a1", "a2"], "return": "r", "progn": [ { "name": "hello", "echo": "{{a1}} is {{a2}}", }, { "name": "set-retval", "var": { "r": "hello", }, }, ] } } drv.run([defun]) self.assertNotEqual(drv.do2_func1, None) drv.run([{"name": "call func1", "func1": {"a1": "xyz", "a2": "abc"}, "register": "rval1"}]) self.assertEqual(drv.variables.get("rval1", None), "hello")
def test_findone(self): cls = cli.loadmodules("dummy", []) drv = cls() drv.driver.find_element = MagicMock() param = {"link_text": "ltxt1"} res = drv.findone(param) self.assertIsNotNone(res) drv.driver.find_element.assert_called_once()
def test_base(self): def dummymodule(self, param): return "hello" cls = cli.loadmodules("dummy", []) cls.do_dummy = dummymodule drv = cls() res = drv.run([{"name": "dummy", "dummy": None, "with_items": ["a", "b", "c"]}]) self.assertEqual(res, "hello")
def test_removelocator(self): cls = cli.loadmodules("dummy", []) drv = cls() res = drv.removelocator({ "id": "id1", "link text": "ltxt1", "blabla": "xyz" }) self.assertEqual(res, {"blabla": "xyz"})
def test_renderdict(self): cls = cli.loadmodules("dummy", []) drv = cls() drv.variables["hello"] = "world" data = drv.render_dict({"test": "replace string {{hello}}", "test2": ["{{hello}}", "world"]}) self.assertIn("test", data) self.assertEqual(data["test"], "replace string world") self.assertIn("test2", data) self.assertEqual(data["test2"], ["world", "world"])
def test_unknown(self): cls = cli.loadmodules("dummy", []) drv = cls() with self.assertRaisesRegex(Exception, "module not found"): drv.run1({"hello": "world"}) with self.assertRaises(AttributeError): drv.run1("hello world") with self.assertRaises(TypeError): drv.run1(["hello", "world"]) with self.assertRaises(TypeError): drv.run1([1, 2, 3])
def dotest(self, param, expected=None, **kwargs): cls = cli.loadmodules("dummy", []) drv = cls() import logging drv.log.setLevel(logging.ERROR + 10) # suppress logging if isinstance(param, str): param = yaml.safe_load(param) if isinstance(param, dict): res = drv.run([param]) if isinstance(param, str): res = drv.run([{param: None}]) if isinstance(param, (list, tuple)): res = drv.run(param) self.assertEqual(res, expected) for k, v in kwargs.items(): self.assertEqual(drv.variables.get(k), v)
def dummy(self): cls = cli.loadmodules("dummy", ["browser"]) return cls, cls()