From df597f07a1a4b0d72f795bc4dc46a7f387532f29 Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Wed, 13 May 2015 13:53:24 +0100 Subject: [PATCH] Switch to using pytest-variables for credentials --- README.md | 6 ++--- credentials.yaml | 43 ------------------------------ pages/desktop/base.py | 4 +-- pages/desktop/login_page.py | 10 +++---- pages/desktop/page_provider.py | 34 +++++++++++------------ pages/page.py | 4 --- requirements.txt | 1 + tests/desktop/test_kb_article.py | 30 ++++++++++++++------- tests/desktop/test_login_logout.py | 29 +++++++++++++------- tests/desktop/test_questions.py | 30 +++++++++++---------- tests/desktop/test_search.py | 5 ++-- variables.json | 12 +++++++++ 12 files changed, 97 insertions(+), 111 deletions(-) delete mode 100644 credentials.yaml create mode 100644 variables.json diff --git a/README.md b/README.md index 42b6d14..293974e 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Some of the tests in sumo-tests require logging in to https://support.allizom.or 1. Create two username and password combinations on https://support.allizom.org 2. Join [#sumo][sumo] and ask for one of these users to be upgraded to admin (or ask someone on [#mozwebqa][mozwebqa] to do this for you) -3. Copy sumo-tests/credentials.yaml to a location outside of sumo-tests. update the 'default' and 'admin' users in credentials.yaml with those credentials +3. Copy sumo-tests/variables.json to a location outside of sumo-tests. update the 'default' and 'admin' users in variables.json with those credentials [mozwebqa]:http://chat.mibbit.com/?server=irc.mozilla.org&channel=#mozwebqa [sumo]:http://chat.mibbit.com/?server=irc.mozilla.org&channel=#sumo @@ -104,9 +104,9 @@ Some of the tests in sumo-tests require logging in to https://support.allizom.or Before each test run, clean up the repo: find . \( -name 'results*' -or -name '*.pyc' \) -print0 | xargs -0 rm -Rf -To run tests locally its a simple case of calling the command below from this directory +To run tests locally it's a simple case of calling the command below from this directory: - py.test --driver=firefox --destructive --credentials=/full/path/to/credentials.yaml . + py.test --driver=firefox --destructive --variables=/full/path/to/variables.json . __Output__ Output of a test run should look like this: diff --git a/credentials.yaml b/credentials.yaml deleted file mode 100644 index 8d117f6..0000000 --- a/credentials.yaml +++ /dev/null @@ -1,43 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# -# File contains users' data. -# -# Each user is a section named with its role -# and any number of values. At least email, -# password and name should be present. -# -# Example: -# admin: -# email: email@site.com -# password: password -# name: Test User -# -# Still, you are free to add any more data you wish. It will be kept -# in the same dictionary. -# -# Example: -# admin: -# email: email@site.com -# password: password -# name: Test User -# username: testuser -# some_user_data: data -# -# The contents of this file are accessible via the pytest-mozwebqa plugin: -# -# Example: -# credentials = mozwebqa.credentials['default'] -# credentials['email'] - -default: - email: - password: - name: - -admin: - email: - password: - name: \ No newline at end of file diff --git a/pages/desktop/base.py b/pages/desktop/base.py index 5503324..814abf6 100755 --- a/pages/desktop/base.py +++ b/pages/desktop/base.py @@ -25,9 +25,9 @@ def click_card_grid(self, locator): def header(self): return self.HeaderRegion(self.testsetup) - def sign_in(self, user="default"): + def sign_in(self, username, password): login = self.header.click_login() - login.log_in(user) + login.log_in(username, password) def sign_out(self): self.header.click_logout() diff --git a/pages/desktop/login_page.py b/pages/desktop/login_page.py index 2fd7226..5a228bf 100644 --- a/pages/desktop/login_page.py +++ b/pages/desktop/login_page.py @@ -23,14 +23,12 @@ class LoginPage(Base): _logged_in_as_div_locator = (By.CSS_SELECTOR, 'div#mod-login_box > div') _logged_in_text = 'Logged in as' - def log_in(self, user='default'): - credentials = self.testsetup.credentials[user] - - self.selenium.find_element(*self._username_box_locator).send_keys(credentials['name']) - self.selenium.find_element(*self._password_box_locator).send_keys(credentials['password']) + def log_in(self, username, password): + self.selenium.find_element(*self._username_box_locator).send_keys(username) + self.selenium.find_element(*self._password_box_locator).send_keys(password) self.selenium.find_element(*self._log_in_button_locator).click() if not self.header.is_user_logged_in: error = self.selenium.find_element(*self._login_error_locator).text - error = "login failed for %s\n" % credentials['name'] + error + error = "login failed for %s\n" % username + error Assert.fail(error) diff --git a/pages/desktop/page_provider.py b/pages/desktop/page_provider.py index 48487b0..f8f389a 100644 --- a/pages/desktop/page_provider.py +++ b/pages/desktop/page_provider.py @@ -18,21 +18,21 @@ def _set_window_size(self): if self.selenium.get_window_size()['width'] < 1920: self.selenium.set_window_size(1920, 1080) - def _go_to_page(self, page_object, do_login=False, user='default'): + def _go_to_page(self, page_object, username=None, password=None): self._set_window_size() self.selenium.get(self.base_url + page_object._page_url) page_object.is_the_current_page - if (do_login): - page_object.sign_in(user) + if all((username, password)): + page_object.sign_in(username, password) page_object.header.dismiss_staging_site_warning_if_present() return page_object - def _go_to_page_with_login_redirect(self, page_object, user='default'): + def _go_to_page_with_login_redirect(self, page_object, username, password): self._set_window_size() from pages.desktop.login_page import LoginPage self.selenium.get(self.base_url + page_object._page_url) login_page = LoginPage(self.testsetup) - login_page.log_in(user) + login_page.log_in(username, password) page_object.is_the_current_page page_object.header.dismiss_staging_site_warning_if_present() return page_object @@ -45,28 +45,28 @@ def new_user_registration_page(self): ''' pages for which login is optional ''' - def home_page(self, do_login=False, user='default'): + def home_page(self, username=None, password=None): from pages.desktop.support_home_page import SupportHomePage - return self._go_to_page(SupportHomePage(self.testsetup), do_login, user) + return self._go_to_page(SupportHomePage(self.testsetup), username, password) - def new_question_page(self, do_login=True, user='default'): + def new_question_page(self, username=None, password=None): from pages.desktop.questions_page import AskNewQuestionsPage - return self._go_to_page(AskNewQuestionsPage(self.testsetup), do_login, user) + return self._go_to_page(AskNewQuestionsPage(self.testsetup), username, password) - def questions_page(self, do_login=False, user='default'): + def questions_page(self, username=None, password=None): from pages.desktop.questions_page import QuestionsPage - return self._go_to_page(QuestionsPage(self.testsetup), do_login, user) + return self._go_to_page(QuestionsPage(self.testsetup), username, password) - def search_page(self, do_login=False, user='default'): + def search_page(self, username=None, password=None): from pages.desktop.search_page import SearchPage - return self._go_to_page(SearchPage(self.testsetup), do_login, user) + return self._go_to_page(SearchPage(self.testsetup), username, password) - def refine_search_page(self, do_login=True, user='default'): + def refine_search_page(self, username=None, password=None): from pages.desktop.refine_search_page import RefineSearchPage - return self._go_to_page(RefineSearchPage(self.testsetup), do_login, user) + return self._go_to_page(RefineSearchPage(self.testsetup), username, password) ''' pages for which login is required ''' - def new_kb_article_page(self, user='admin'): + def new_kb_article_page(self, username, password): from pages.desktop.knowledge_base_new_article import KnowledgeBaseNewArticle - return self._go_to_page_with_login_redirect(KnowledgeBaseNewArticle(self.testsetup), user) + return self._go_to_page_with_login_redirect(KnowledgeBaseNewArticle(self.testsetup), username, password) diff --git a/pages/page.py b/pages/page.py index a0723b4..227b9b8 100644 --- a/pages/page.py +++ b/pages/page.py @@ -94,7 +94,3 @@ def wait_for_ajax(self): if self.selenium.execute_script("return jQuery.active == 0"): return raise Exception("Wait for AJAX timed out after %s seconds" % count) - - def get_user_name(self, user='default'): - credentials = self.testsetup.credentials[user] - return credentials['name'] diff --git a/requirements.txt b/requirements.txt index 7ddff37..8ae6b42 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,5 +8,6 @@ chardet==2.1.1 py==1.4.26 pytest==2.7.0 pytest-mozwebqa +pytest-variables requests==2.4.3 selenium diff --git a/tests/desktop/test_kb_article.py b/tests/desktop/test_kb_article.py index 35851ab..7c65a25 100644 --- a/tests/desktop/test_kb_article.py +++ b/tests/desktop/test_kb_article.py @@ -10,13 +10,15 @@ class TestKnowledgeBaseArticle: - def test_that_article_can_be_created(self, mozwebqa): + def test_that_article_can_be_created(self, mozwebqa, variables): """ Creates a new knowledge base article. Verifies creation. Deletes the article """ - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + user = variables['users']['admin'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() @@ -37,14 +39,16 @@ def test_that_article_can_be_created(self, mozwebqa): kb_article = kb_edit_article.navigation.click_show_history() kb_article.delete_entire_article_document() - def test_that_article_can_be_edited(self, mozwebqa): + def test_that_article_can_be_edited(self, mozwebqa, variables): """ Creates a new knowledge base article. Verifies creation. Edits the article, verifies the edition. Deletes the article """ - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + user = variables['users']['admin'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() @@ -74,13 +78,15 @@ def test_that_article_can_be_edited(self, mozwebqa): kb_article_history = kb_edit_article.navigation.click_show_history() kb_article_history.delete_entire_article_document() - def test_that_article_can_be_deleted(self, mozwebqa): + def test_that_article_can_be_deleted(self, mozwebqa, variables): """ Creates a new knowledge base article. Deletes the article. Verifies the deletion. """ - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + user = variables['users']['admin'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() @@ -100,13 +106,15 @@ def test_that_article_can_be_deleted(self, mozwebqa): actual_page_title = kb_article_history.page_title Assert.contains("Page Not Found", actual_page_title) - def test_that_article_can_be_previewed_before_submitting(self, mozwebqa): + def test_that_article_can_be_previewed_before_submitting(self, mozwebqa, variables): """ Start a new knowledge base article. Preview. Verify the contents in the preview """ - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + user = variables['users']['default'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() @@ -119,12 +127,14 @@ def test_that_article_can_be_previewed_before_submitting(self, mozwebqa): # Does not need to be deleted as it does not commit the article - def test_that_article_can_be_translated(self, mozwebqa): + def test_that_article_can_be_translated(self, mozwebqa, variables): """ Creates a new knowledge base article. Translate article """ - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + user = variables['users']['admin'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() diff --git a/tests/desktop/test_login_logout.py b/tests/desktop/test_login_logout.py index 02f6617..d4be1c8 100644 --- a/tests/desktop/test_login_logout.py +++ b/tests/desktop/test_login_logout.py @@ -13,9 +13,10 @@ class TestLoginLogout: @pytest.mark.nondestructive - def test_login(self, mozwebqa): + def test_login(self, mozwebqa, variables): + user = variables['users']['default'] home_page = PageProvider(mozwebqa).home_page() - home_page.sign_in(user='default') + home_page.sign_in(user['username'], user['password']) Assert.true(home_page.header.is_user_logged_in, 'User not shown to be logged in') @@ -29,8 +30,10 @@ def test_login(self, mozwebqa): 'search_page', 'refine_search_page', ]) - def test_logout_from_pages(self, mozwebqa, page_method): - page_under_test = getattr(PageProvider(mozwebqa), page_method)(do_login=True, user='default') + def test_logout_from_pages(self, mozwebqa, variables, page_method): + user = variables['users']['default'] + page_under_test = getattr(PageProvider(mozwebqa), page_method)( + user['username'], user['password']) Assert.true(page_under_test.header.is_user_logged_in, 'User not shown to be logged in') # sign out @@ -39,8 +42,10 @@ def test_logout_from_pages(self, mozwebqa, page_method): Assert.true(page_under_test.header.is_user_logged_out) @pytest.mark.native - def test_logout_from_new_kb_article_page(self, mozwebqa): - new_kb_page = PageProvider(mozwebqa).new_kb_article_page() + def test_logout_from_new_kb_article_page(self, mozwebqa, variables): + user = variables['users']['default'] + new_kb_page = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) Assert.true(new_kb_page.header.is_user_logged_in, 'User not shown to be logged in') # sign out @@ -49,8 +54,10 @@ def test_logout_from_new_kb_article_page(self, mozwebqa): Assert.true(register_page.header.is_user_logged_out) @pytest.mark.native - def test_logout_from_edit_kb_article_page(self, mozwebqa): - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + def test_logout_from_edit_kb_article_page(self, mozwebqa, variables): + user = variables['users']['default'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() @@ -66,8 +73,10 @@ def test_logout_from_edit_kb_article_page(self, mozwebqa): Assert.true(register_page.header.is_user_logged_out) @pytest.mark.native - def test_logout_from_translate_kb_article_page(self, mozwebqa): - kb_new_article = PageProvider(mozwebqa).new_kb_article_page() + def test_logout_from_translate_kb_article_page(self, mozwebqa, variables): + user = variables['users']['default'] + kb_new_article = PageProvider(mozwebqa).new_kb_article_page( + user['username'], user['password']) # create a new article mock_article = MockArticle() diff --git a/tests/desktop/test_questions.py b/tests/desktop/test_questions.py index c985e0b..abb2b25 100644 --- a/tests/desktop/test_questions.py +++ b/tests/desktop/test_questions.py @@ -13,14 +13,16 @@ class TestQuestions: @pytest.mark.native - def test_that_posting_question_works(self, mozwebqa): + def test_that_posting_question_works(self, mozwebqa, variables): """Posts a question to /questions""" + user = variables['users']['default'] timestamp = datetime.datetime.today() q_to_ask = "automation test question %s" % (timestamp) q_details = "This is a test. %s" % (timestamp) # go to the /questions/new page and log in - ask_new_questions_page = PageProvider(mozwebqa).new_question_page() + ask_new_questions_page = PageProvider(mozwebqa).new_question_page( + user['username'], user['password']) # post a question ask_new_questions_page.click_firefox_product_link() @@ -90,19 +92,20 @@ def test_that_questions_problem_count_increments(self, mozwebqa): Assert.equal(initial_count + 1, post_click_count) - def test_contributor_flow_to_support_forum_post(self, mozwebqa): + def test_contributor_flow_to_support_forum_post(self, mozwebqa, variables): """ Shows a contributor can start on the home page and move all the way to answering a question in the forum. """ - reply_text = "reply" - # 1. Start on the home page # 2. Log in # 3. Use the contributor bar to go to the forums. # The questions page should list 20 posts. # 3.1 go to the question page - questions_page = PageProvider(mozwebqa).questions_page(do_login=True) + user = variables['users']['default'] + questions_page = PageProvider(mozwebqa).questions_page( + user['username'], user['password']) + questions_page.click_all_products() # 3.2 ensure the size of the list is 20 Assert.greater(questions_page.questions_count, 0, @@ -117,11 +120,10 @@ def test_contributor_flow_to_support_forum_post(self, mozwebqa): # 5. Go to the thread # 6. Scroll to the bottom and click into the text field # 7. Type reply - # 7.1 get the login-user name to check the author of the reply - username = forum_page.header.login_user_name - # 7.2 reply the post - forum_page.post_reply(reply_text) - # 7.3 check if posting a reply finishes without an error - is_reply_present = forum_page.is_reply_text_present(username, reply_text) - Assert.true(is_reply_present, - u'reply with "%s" text posted by %s is not present' % (reply_text, username)) + # 7.1 reply the post + reply = "reply" + forum_page.post_reply(reply) + # 7.2 check if posting a reply finishes without an error + Assert.true(forum_page.is_reply_text_present(user['username'], reply), + u'reply with "%s" text posted by %s is not present' % ( + reply, user['username'])) diff --git a/tests/desktop/test_search.py b/tests/desktop/test_search.py index a1749d2..247ffa4 100644 --- a/tests/desktop/test_search.py +++ b/tests/desktop/test_search.py @@ -13,7 +13,7 @@ class TestSearch: forum_search_term = "Firefox crash" @pytest.mark.nondestructive - def test_no_query_adv_forum_search(self, mozwebqa): + def test_no_query_adv_forum_search(self, mozwebqa, variables): if mozwebqa.base_url in ['https://support-dev.allizom.org', 'https://support.mozilla.org']: pytest.skip('Search results are not guaranteed to exist on %s' % mozwebqa.base_url) @@ -22,7 +22,8 @@ def test_no_query_adv_forum_search(self, mozwebqa): # do test refine_search_pg.click_support_questions_tab() - refine_search_pg.type_in_asked_by_box(refine_search_pg.get_user_name('default')) + username = variables['users']['default']['username'] + refine_search_pg.type_in_asked_by_box(username) refine_search_pg.click_search_button_support() Assert.true(refine_search_pg.search_result_count > 0, "No search results not found") diff --git a/variables.json b/variables.json new file mode 100644 index 0000000..30e9298 --- /dev/null +++ b/variables.json @@ -0,0 +1,12 @@ +{ + "users": { + "default": { + "username": "", + "password": "", + "email": ""}, + "admin": { + "username": "", + "password": "", + "email": ""} + } +}