From b45c4e903fcaab852ed5b08d64a1b5876785c67d Mon Sep 17 00:00:00 2001 From: Daron Andrew Edie Date: Tue, 17 Jul 2018 11:26:57 +1000 Subject: [PATCH] Changed demo to use SeleniumLibrary, minor changes to make files flake8 compliant --- PageObjectLibrary/__init__.py | 16 ++++++++-------- PageObjectLibrary/keywords.py | 10 +++++----- PageObjectLibrary/pageobject.py | 4 ++-- demo/resources/HomePage.py | 2 +- demo/resources/LoginPage.py | 4 +++- demo/resources/config.py | 12 +++++++----- demo/tests/demo.robot | 2 +- demo/webapp/demoserver.py | 8 ++++++-- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/PageObjectLibrary/__init__.py b/PageObjectLibrary/__init__.py index 748f774..0defb2a 100644 --- a/PageObjectLibrary/__init__.py +++ b/PageObjectLibrary/__init__.py @@ -13,9 +13,9 @@ class PageObjectLibrary(PageObjectLibraryKeywords): *PageObjectLibrary* is a lightweight library which supports using the page object pattern with - [http://robotframework.org/Selenium2Library/doc/Selenium2Library.html|Selenium2Library]. - This library does not replace Selenium2Library; rather, it - provides a framework around which to use Selenium2Library and the + [http://robotframework.org/SeleniumLibrary/doc/SeleniumLibrary.html|SeleniumLibrary]. + This library does not replace SeleniumLibrary; rather, it + provides a framework around which to use SeleniumLibrary and the lower-level [http://selenium-python.readthedocs.org/|Python bindings to Selenium] @@ -32,16 +32,16 @@ class your keywords have access to the following pre-defined attributes and methods: | =Attribute/method= | =Description= | - | ``self.se2lib`` | A reference to the Selenium2Library instance | + | ``self.se2lib`` | A reference to the SeleniumLibrary instance | | ``self.browser`` | A reference to the currently open browser | | ``self.locator`` | A wrapper around the ``_locators`` dictionary | | ``self.logger`` | A reference to the ``robot.api.logger`` instance | | ``self._wait_for_page_refresh()`` | a context manager for doing work that causes a page refresh | - = Using Selenium2Library Keywords = + = Using SeleniumLibrary Keywords = Within your keywords you have access to the full power of - Selenium2Library. You can use ``self.se2lib`` to access the + SeleniumLibrary. You can use ``self.se2lib`` to access the library keywords. The following example shows how to call the ``Capture Page Screenshot`` keyword: @@ -128,7 +128,7 @@ class your keywords have access to the following pre-defined Robot can import it, just like with any other keyword library. When you use the keyword `Go to page`, the keyword will automatically load the keyword library and put it at the front of - the Robot Framework library search order (see + the Robot Framework library search order (see [http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Library%20Search%20Order|Set Library Search Order]) In the following example it is assumed there is a second page @@ -137,7 +137,7 @@ class your keywords have access to the following pre-defined | ``*** Settings ***`` | Library PageObjectLibrary - | Library Selenium2Library + | Library SeleniumLibrary | Suite Setup Open browser http://www.example.com | Suite Teardown Close all browsers | diff --git a/PageObjectLibrary/keywords.py b/PageObjectLibrary/keywords.py index 97e9886..dc5733a 100644 --- a/PageObjectLibrary/keywords.py +++ b/PageObjectLibrary/keywords.py @@ -1,7 +1,7 @@ """PageObjectLibrary A library to support the creation of page objects using -selenium and Seleniuim2Library. +selenium and SeleniuimLibrary. """ @@ -34,11 +34,11 @@ def se2lib(self): # can be imported outside the context of a running # suite (ie: by libdoc, robotframework-hub, etc) try: - se2 = self.builtin.get_library_instance("Selenium2Library") + se2 = self.builtin.get_library_instance("SeleniumLibrary") except RuntimeError: - self.builtin.import_library("Selenium2Library") - se2 = self.builtin.get_library_instance("Selenium2Library") + self.builtin.import_library("SeleniumLibrary") + se2 = self.builtin.get_library_instance("SeleniumLibrary") return se2 @@ -94,7 +94,7 @@ def go_to_page(self, page_name, page_root=None): The effect is the same as if you had called the following three keywords: - | Selenium2Library.Go To http://www.example.com/login + | SeleniumLibrary.Go To http://www.example.com/login | Import Library ExampleLoginPage | Set Library Search Order ExampleLoginPage diff --git a/PageObjectLibrary/pageobject.py b/PageObjectLibrary/pageobject.py index cb35114..9e473cd 100644 --- a/PageObjectLibrary/pageobject.py +++ b/PageObjectLibrary/pageobject.py @@ -34,7 +34,7 @@ class variable PAGE_TITLE. A class can override this method if the Classes that inherit from this class have access to the following properties: - * se2lib a reference to an instance of Selenium2Library + * se2lib a reference to an instance of SeleniumLibrary * browser a reference to the current webdriver instance * logger a reference to robot.api.logger * locator a wrapper around the page object's ``_locators`` dictionary @@ -61,7 +61,7 @@ def __init__(self): # test (eg: by libdoc, robotframework-hub, etc) @property def se2lib(self): - return BuiltIn().get_library_instance("Selenium2Library") + return BuiltIn().get_library_instance("SeleniumLibrary") @property def browser(self): diff --git a/demo/resources/HomePage.py b/demo/resources/HomePage.py index c2740aa..7e03739 100644 --- a/demo/resources/HomePage.py +++ b/demo/resources/HomePage.py @@ -1,5 +1,6 @@ from PageObjectLibrary import PageObject + class HomePage(PageObject): """Keywords for the Home page of the demo app @@ -16,4 +17,3 @@ class HomePage(PageObject): # (eg: self.locator.username, etc) _locators = { } - diff --git a/demo/resources/LoginPage.py b/demo/resources/LoginPage.py index d53b57a..5323570 100644 --- a/demo/resources/LoginPage.py +++ b/demo/resources/LoginPage.py @@ -1,6 +1,8 @@ from PageObjectLibrary import PageObject + from robot.libraries.BuiltIn import BuiltIn + class LoginPage(PageObject): PAGE_TITLE = "Login - PageObjectLibrary Demo" PAGE_URL = "/login.html" @@ -24,7 +26,7 @@ def enter_username(self, username): """Enter the given string into the username field""" self.se2lib.input_text(self.locator.username, username) - def enter_password(self,password): + def enter_password(self, password): """Enter the given string into the password field""" self.se2lib.input_text(self.locator.password, password) diff --git a/demo/resources/config.py b/demo/resources/config.py index cd957e3..b4774b1 100644 --- a/demo/resources/config.py +++ b/demo/resources/config.py @@ -1,11 +1,12 @@ import os import sys + class Config(object): - """Configuration variables for this test suite + """Configuration variables for this test suite This creates a variable named CONFIG (${CONFIG} when included - in a test as a variable file. + in a test as a variable file. Example: @@ -28,11 +29,12 @@ def __init__(self): self.demo_root = os.path.abspath(os.path.join(_here, "..")) self.port = 8000 self.root_url = "http://localhost:%s" % self.port - self.username="test user" - self.password="password" + self.username = "test user" + self.password = "password" def __str__(self): return "" % str(self.__dict__) - + + # This creates a variable that robot can see CONFIG = Config() diff --git a/demo/tests/demo.robot b/demo/tests/demo.robot index 087d4de..4b5b8bb 100644 --- a/demo/tests/demo.robot +++ b/demo/tests/demo.robot @@ -5,7 +5,7 @@ | Variables | ../resources/config.py | | Library | PageObjectLibrary -| Library | Selenium2Library +| Library | SeleniumLibrary | Library | Process | | Suite Setup | Start webapp and open browser diff --git a/demo/webapp/demoserver.py b/demo/webapp/demoserver.py index adfc79a..cf471bf 100644 --- a/demo/webapp/demoserver.py +++ b/demo/webapp/demoserver.py @@ -15,6 +15,7 @@ import SocketServer as socketserver from urlparse import urlparse + def main(): parser = argparse.ArgumentParser(description="demo web server") parser.add_argument("-p", "--port", type=int, default=8000, help="port number for the server (default 8000)") @@ -27,11 +28,12 @@ def main(): try: httpd = DemoServer(("", args.port), DemoHandler) print("serving %s on port %s" % (docroot, 8000)) - print ("^C, or visit http://localhost:%s/admin/shutdown to stop" % args.port) + print("^C, or visit http://localhost:%s/admin/shutdown to stop" % args.port) httpd.serve_forever() except KeyboardInterrupt: pass + class DemoHandler(SimpleHTTPRequestHandler): def redirect(self, uri): @@ -50,7 +52,7 @@ def do_POST(self): def do_GET(self): url = urlparse(self.path) print("url: '%s' url.path: '%s'" % (url, url.path)) - if url.path == "" or url.path == "/" : + if url.path == "" or url.path == "/": self.redirect("/login.html") if url.path == "/authenticate": @@ -79,10 +81,12 @@ def get_form_data(self): }) return form + class DemoServer(socketserver.TCPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind(self.server_address) + if __name__ == "__main__": main()