def _parse_listener(self, event_firing_webdriver): listener_module = self._string_to_modules(event_firing_webdriver) listener_count = len(listener_module) if listener_count > 1: message = 'Is is possible import only one listener but there was %s listeners.' % listener_count raise ValueError(message) listener_module = listener_module[0] importer = Importer('test library') listener = importer.import_class_or_module(listener_module.module) if not isclass(listener): message = "Importing test Selenium lister class '%s' failed." % listener_module.module raise DataError(message) return listener
def test_arguments_when_importing_by_path(self): path = create_temp_file('args.py', extra_content=''' class args: def __init__(self, arg='default'): self.arg = arg ''') importer = Importer().import_class_or_module_by_path for args, expected in [((), 'default'), (['positional'], 'positional'), (['arg=named'], 'named')]: lib = importer(path, args) assert_true(not inspect.isclass(lib)) assert_equal(lib.__class__.__name__, 'args') assert_equal(lib.arg, expected)
def _parse_plugins(self, plugins): libraries = [] importer = Importer('test library') for parsed_plugin in self._string_to_modules(plugins): plugin = importer.import_class_or_module(parsed_plugin.module) if not isclass(plugin): message = "Importing test library: '%s' failed." % parsed_plugin.module raise DataError(message) plugin = plugin(self, *parsed_plugin.args, **parsed_plugin.kw_args) if not isinstance(plugin, LibraryComponent): message = 'Plugin does not inherit SeleniumLibrary.base.LibraryComponent' raise PluginError(message) self._store_plugin_keywords(plugin) libraries.append(plugin) return libraries
def test_modules_do_not_take_arguments(self): path = create_temp_file('no_args_allowed.py') assert_raises_with_msg(DataError, "Importing '%s' failed: Modules do not take arguments." % path, Importer().import_class_or_module_by_path, path, ['invalid'])
def test_instantiate_failure(self): err = assert_raises(DataError, Importer().import_class_or_module, 'ExampleLibrary', ['accepts', 'no', 'args']) assert_true(unicode(err).startswith("Importing 'ExampleLibrary' failed: " "Creating instance failed: TypeError:"))
def test_when_importing_by_path(self): path = create_temp_file('args.py', extra_content='class args: a=1') lib = Importer().import_class_or_module_by_path(path, ()) assert_true(not inspect.isclass(lib)) assert_equals(lib.__class__.__name__, 'args') assert_equals(lib.a, 1)
def test_with_arguments(self): lib = Importer().import_class_or_module('libswithargs.Mixed', range(5)) assert_equals(lib.get_args(), (0, 1, '2 3 4'))
def test_when_importing_by_name(self): from ExampleLibrary import ExampleLibrary lib = Importer().import_class_or_module('ExampleLibrary', instantiate_with_args=()) assert_true(not inspect.isclass(lib)) assert_true(isinstance(lib, ExampleLibrary))
def _failing_import(self, name): importer = Importer().import_class_or_module return assert_raises(DataError, importer, name)
def _import(self, name, type=None, logger=None): return Importer(type, logger or LoggerStub()).import_class_or_module(name)
def test_instantiate_failure(self): assert_raises_with_msg( DataError, "Importing xxx 'ExampleLibrary' failed: Xxx 'ExampleLibrary' expected 0 arguments, got 3.", Importer('XXX').import_class_or_module, 'ExampleLibrary', ['accepts', 'no', 'args'] )
def test_escaping_not_needed_if_args_do_not_match_names(self): lib = Importer().import_class_or_module('libswithargs.Mixed', ['foo=b', 'bar=a']) assert_equal(lib.get_args(), ('foo=b', 'bar=a', ''))
def test_named_arguments(self): lib = Importer().import_class_or_module('libswithargs.Mixed', ['default=b', 'mandatory=a']) assert_equal(lib.get_args(), ('a', 'b', ''))
def test_logging(self): logger = LoggerStub(remove_extension=True) Importer(logger=logger).import_module('ExampleLibrary') logger.assert_message("Imported module 'ExampleLibrary' from '%s'." % join(LIBDIR, 'ExampleLibrary'))
def test_import_module(self): module = Importer().import_module('ExampleLibrary') assert_equal(module.ExampleLibrary().return_string_from_library('xxx'), 'xxx')
def _import_modules(self, plugins): importer = Importer('test library') return [ importer.import_class_or_module(plugin.plugin) for plugin in plugins ]