def __init__(self, temp_dir=DEFAULT_TMP_DIR): """Construct a temporary directory containing a file named user-extenions.js in the system's temporary directory.""" self.name = os.path.join(tempfile.mkdtemp(dir=temp_dir), TempJSExtensionFile.EXT_FILE_NAME) log("creating javascript tempfile: %s" % self.name) self.file = open(self.name, 'w')
def close(self): """Delete the temporary file and directory __init__ made""" dir = os.path.dirname(self.name) self.file.close() os.remove(self.name) os.rmdir(dir) log("(cleaning up javascript mess) deleted file: %s and dir: %s" % (self.file.name, dir))
def __init__(self, timeout=60, host=DEFAULT_HOST, port=DEFAULT_PORT, plugin_dir=""): """Load the wbSelenium library with arguments for Robot's SeleniumLibrary and selenium-server. Classes defined in python files are searched in the base directory and plugin directory. This class inherits from SeleniumLibrary and also all other classes specified in the plugin and base directories. Dynamic inheritance is implemented using the Class.__bases__ += method which means all external classes must be new style (inherit from object). """ SeleniumLibrary.__init__(self, timeout, host, port) # use plugin dir if specified, otherwise just load WikiBhasha base extension_paths = [wbSelenium.BASE_DIR] + (plugin_dir and [os.path.abspath(plugin_dir)] or []) # get list of all of the javascript files and flatten list js_files = sum([self._get_ext_js(path) for path in extension_paths], []) self._user_ext_file = js_files and TempJSExtensionFile() or None def process_curry(file): self._process_js_ext(file, self._user_ext_file) map(process_curry, js_files) # inherit from other extensions (must take a flat tuple) ext_classes = tuple(sum([self._get_ext_classes(path) for path in extension_paths], [])) # plugins override SeleniumLibrary in MRO wbSelenium.__bases__ = ext_classes + wbSelenium.__bases__ for klass in ext_classes: log("wbSelenium imported class: " + klass.__name__) #super(klass, self).__init__() klass.__init__(self)
def _get_ext_js(path): """Return a list of files (their absolute path) in "path" which end in extension ".js". This method is static as it is independent of the class instances and it is easier to test as static.""" log("searching path %s for javascript files" % path) full_path = os.path.join(path, wbSelenium.JS_DIR) files = [] if os.path.exists(full_path): files = [os.path.abspath(os.path.join(full_path, file)) for file in os.listdir(full_path) if os.path.splitext(file)[1].startswith(".js")] log("found files %s in path %s" % (files, path)) return files
def _get_ext_classes(path): """Return a list of class objects defined in all ".py" files within "path". All .py modules are imported using "__import__", while classes are identified using the "inspect" module. The sys.path list is altered to allow imports from the external path. This method is static as it is independent of the class instances and it is easier to test as static.""" #path = os.abspath(path) log("searching path %s for python plugin classes to extend from" % path) if not os.path.exists(path): raise ImportError, "extension directory path %s does not exist" % ( path, ) # add the specified path to sys.path so we can import from them sys.path.append(path) try: # only do top level search for .py files # this may need to change if we want pyc support extensions = [ __import__(os.path.splitext(file)[0]) for file in os.listdir(path) if os.path.splitext(file)[1] == ".py" ] # only get classes that are defined inside the module, ignore imported classes def get_classes_from_module(module): classes = inspect.getmembers(module, inspect.isclass) # filter out hidden classes and imported classes return filter( lambda x: x[1].__module__ == module.__name__ and not x[1].__name__.startswith('_'), classes) # grab all the classes defined within the module and flatten list classes = sum([ dict(get_classes_from_module(module)).values() for module in extensions ], []) finally: # remove the module path that we previously added to sys sys.path.remove(path) log("found classes %s in path %s" % (classes, path)) return classes
def _get_ext_classes(path): """Return a list of class objects defined in all ".py" files within "path". All .py modules are imported using "__import__", while classes are identified using the "inspect" module. The sys.path list is altered to allow imports from the external path. This method is static as it is independent of the class instances and it is easier to test as static.""" #path = os.abspath(path) log("searching path %s for python plugin classes to extend from" % path) if not os.path.exists(path): raise ImportError, "extension directory path %s does not exist" % (path,) # add the specified path to sys.path so we can import from them sys.path.append(path) try: # only do top level search for .py files # this may need to change if we want pyc support extensions = [__import__(os.path.splitext(file)[0]) for file in os.listdir(path) if os.path.splitext(file)[1] == ".py"] # only get classes that are defined inside the module, ignore imported classes def get_classes_from_module(module): classes = inspect.getmembers(module, inspect.isclass) # filter out hidden classes and imported classes return filter(lambda x: x[1].__module__ == module.__name__ and not x[1].__name__.startswith('_'), classes) # grab all the classes defined within the module and flatten list classes = sum([dict(get_classes_from_module(module)) .values() for module in extensions], []) finally: # remove the module path that we previously added to sys sys.path.remove(path) log("found classes %s in path %s" % (classes, path)) return classes
def wait_until_server_has_started(host=DEFAULT_HOST, port=DEFAULT_PORT): """Stall callee until selenium-server on `host` and `port` becomes active""" log("waiting until selenium-server has started") if not selenium_server_is_running(host, port): raise RuntimeError