From 320eac1675fd3c4b2de4d5dd404ecc413b9ea505 Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Wed, 27 Apr 2016 13:54:35 +0200 Subject: selenium test not in unittest --- selenium/test.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 selenium/test.py (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py new file mode 100644 index 000000000..8720c79d7 --- /dev/null +++ b/selenium/test.py @@ -0,0 +1,100 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.ui import WebDriverWait +import time +import logging + +logger = logging.getLogger(__name__) +bank_url = 'http://127.0.0.1:9898' + +def client_setup(): + """Return a dict containing the driver and the extension's id""" + co = webdriver.ChromeOptions() + co.add_argument("load-extension=/home/marcello/wallet-webex") + cap = webdriver.DesiredCapabilities.CHROME.copy() + cap['loggingPrefs'] = {'driver': 'INFO', 'browser': 'INFO'} + client = webdriver.Chrome(chrome_options=co, desired_capabilities=cap) + client.get('https://taler.net') + listener = """\ + document.addEventListener('taler-id', function(evt){ + var html = document.getElementsByTagName('html')[0]; + html.setAttribute('data-taler-wallet-id', evt.detail.id); + }); + + var evt = new CustomEvent('taler-query-id'); + document.dispatchEvent(evt); + """ + client.execute_script(listener) + client.implicitly_wait(5) + html = client.find_element(By.TAG_NAME, "html") + return {'client': client, 'ext_id': html.get_attribute('data-taler-wallet-id')} + + +def is_error(client): + """Return True in case of errors in the browser, False otherwise""" + for log_type in ['browser']: + for log in client.get_log(log_type): + if log['level'] is 'error': + print(log['level'] + ': ' + log['message']) + return True + return False + + +def register(client): + """Register a new user to the bank delaying its execution until the + profile page is shown""" + client.get(bank_url + '/accounts/register') + client.find_element(By.TAG_NAME, "form") + register = """\ + var form = document.getElementsByTagName('form')[0]; + form.username.value = '%s'; + form.password.value = 'test'; + form.submit(); + """ % str(int(time.time())) # need fresh username + + client.execute_script(register) + # need implicit wait to be set up + button = client.find_element(By.ID, "select-exchange") + # when button is gotten, the browser is in the profile page + # so the function can return + if not is_error(client): + logger.info('correctly registered at bank') + else: + logger.error('User not registered at bank') + + +def withdraw(client): + """Register and withdraw (1) KUDOS for a fresh user""" + register(client) + # trigger withdrawal button + button = client.find_element(By.ID, "select-exchange") + button.click() + location = client.execute_script("return document.location.href") + client.get(location) + # Confirm xchg + wait = WebDriverWait(client, 10) + button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]"))) + # This click returns the captcha page (put wait?) + button.click() + answer = client.find_element(By.XPATH, "//input[@name='pin_0']") + question = client.find_element(By.XPATH, "//span[@class='captcha-question']/div") + questionTok = question.text.split() + op1 = int(questionTok[2]) + op2 = int(questionTok[4]) + res = {'+': op1 + op2, '-': op1 - op2, u'\u00d7': op1 * op2} + answer.send_keys(res[questionTok[3]]) + form = client.find_element(By.TAG_NAME, "form") + form.submit() + # check outcome + msg_succ = client.find_element(By.CLASS_NAME, "informational-ok") + if not msg_succ: + logger.error('Error in withdrawing') + else: logger.info('Withdrawal successful') + + + +ret = client_setup() +client = ret['client'] +withdraw(client) +client.close() -- cgit v1.2.3 From 07715206e54cab23788e3167e5a77509ffd3e369 Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Wed, 27 Apr 2016 14:39:39 +0200 Subject: make donations via selenium --- selenium/test.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py index 8720c79d7..c0a5de742 100644 --- a/selenium/test.py +++ b/selenium/test.py @@ -6,7 +6,8 @@ import time import logging logger = logging.getLogger(__name__) -bank_url = 'http://127.0.0.1:9898' +bank = 'http://bank.test.taler.net' +donations = 'http://shop.test.taler.net' def client_setup(): """Return a dict containing the driver and the extension's id""" @@ -41,10 +42,21 @@ def is_error(client): return False +def make_donation(client): + """Make donation at shop.test.taler.net. Assume the wallet has coins""" + client.get(donations) + form = client.find_element(By.TAG_NAME, "form") + form.submit() # amount and receiver chosen + confirm_taler = client.find_element(By.XPATH, "//form//input[@type='button']") + confirm_taler.click() # Taler as payment option chosen + wait = WebDriverWait(client, 10) + confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) + confirm_pay.click() + def register(client): """Register a new user to the bank delaying its execution until the profile page is shown""" - client.get(bank_url + '/accounts/register') + client.get(bank + '/accounts/register') client.find_element(By.TAG_NAME, "form") register = """\ var form = document.getElementsByTagName('form')[0]; @@ -93,8 +105,8 @@ def withdraw(client): else: logger.info('Withdrawal successful') - ret = client_setup() client = ret['client'] withdraw(client) -client.close() +make_donation(client) +# client.close() -- cgit v1.2.3 From 5ea6f8dd00266e1b5cf76826f54364119a295948 Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Wed, 27 Apr 2016 16:58:06 +0200 Subject: selenium tests in "/component" form + fetching baseurl from env --- selenium/test.py | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py index c0a5de742..a6155321e 100644 --- a/selenium/test.py +++ b/selenium/test.py @@ -1,13 +1,22 @@ +#!/usr/bin/env python3 + +""" +Tests for the wallet. It looks for an env variable called TALER_BASEURL +where it appends "/banks" etc. in order to find bank and shops. If not +found, it defaults to https://test.taler.net/ +""" + from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait +from urllib import parse import time import logging +import os logger = logging.getLogger(__name__) -bank = 'http://bank.test.taler.net' -donations = 'http://shop.test.taler.net' +taler_baseurl = os.environ['TALER_BASEURL'] if 'TALER_BASEURL' in os.environ else 'https://test.taler.net/' def client_setup(): """Return a dict containing the driver and the extension's id""" @@ -27,10 +36,19 @@ def client_setup(): document.dispatchEvent(evt); """ client.execute_script(listener) - client.implicitly_wait(5) html = client.find_element(By.TAG_NAME, "html") return {'client': client, 'ext_id': html.get_attribute('data-taler-wallet-id')} +# Note that the db appears to be reset automatically by the driver +def destroy_db(client, ext_id): + url = 'chrome-extension://' + ext_id + '/popup/popup.html#/debug' + client.get(url) + button = client.find_element(By.XPATH, "//div[@id='content']/button[3]") + button.click() + time.sleep(4) + alert = client.switch_to.alert + alert.accept() + def is_error(client): """Return True in case of errors in the browser, False otherwise""" @@ -44,7 +62,7 @@ def is_error(client): def make_donation(client): """Make donation at shop.test.taler.net. Assume the wallet has coins""" - client.get(donations) + client.get(parse.urljoin(taler_baseurl, "shop")) form = client.find_element(By.TAG_NAME, "form") form.submit() # amount and receiver chosen confirm_taler = client.find_element(By.XPATH, "//form//input[@type='button']") @@ -53,10 +71,23 @@ def make_donation(client): confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) confirm_pay.click() + +def buy_article(client): + """Buy article at blog.test.taler.net. Assume the wallet has coins""" + client.get(parse.urljoin(taler_baseurl, "blog")) + teaser = client.find_element(By.XPATH, "//ul/h3/a[1]") # Pick 'Foreword' chapter + teaser.click() + wait = WebDriverWait(client, 10) + confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) + confirm_pay.click() + + def register(client): """Register a new user to the bank delaying its execution until the profile page is shown""" - client.get(bank + '/accounts/register') + client.get(parse.urljoin(taler_baseurl, "bank")) + register_link = client.find_element(By.XPATH, "//a[@href='/accounts/register/']") + register_link.click() client.find_element(By.TAG_NAME, "form") register = """\ var form = document.getElementsByTagName('form')[0]; @@ -107,6 +138,8 @@ def withdraw(client): ret = client_setup() client = ret['client'] +client.implicitly_wait(10) withdraw(client) make_donation(client) -# client.close() +buy_article(client) +client.close() -- cgit v1.2.3 From ce87fc0b90262cb44dda780b128c1ab0afd3b609 Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Thu, 28 Apr 2016 15:08:34 +0200 Subject: deploying try-catch(es) in selenium code --- selenium/test.py | 105 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 83 insertions(+), 22 deletions(-) (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py index a6155321e..d697f73c8 100644 --- a/selenium/test.py +++ b/selenium/test.py @@ -10,18 +10,21 @@ from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait +from selenium.common.exceptions import NoSuchElementException, TimeoutException from urllib import parse +import argparse import time import logging +import sys import os logger = logging.getLogger(__name__) taler_baseurl = os.environ['TALER_BASEURL'] if 'TALER_BASEURL' in os.environ else 'https://test.taler.net/' -def client_setup(): +def client_setup(args): """Return a dict containing the driver and the extension's id""" co = webdriver.ChromeOptions() - co.add_argument("load-extension=/home/marcello/wallet-webex") + co.add_argument("load-extension=" + args.extdir) cap = webdriver.DesiredCapabilities.CHROME.copy() cap['loggingPrefs'] = {'driver': 'INFO', 'browser': 'INFO'} client = webdriver.Chrome(chrome_options=co, desired_capabilities=cap) @@ -63,22 +66,44 @@ def is_error(client): def make_donation(client): """Make donation at shop.test.taler.net. Assume the wallet has coins""" client.get(parse.urljoin(taler_baseurl, "shop")) - form = client.find_element(By.TAG_NAME, "form") + try: + form = client.find_element(By.TAG_NAME, "form") + except NoSuchElementException: + logger.error('No donation form found') + sys.exit(1) form.submit() # amount and receiver chosen - confirm_taler = client.find_element(By.XPATH, "//form//input[@type='button']") + try: + confirm_taler = client.find_element(By.XPATH, "//form//input[@type='button']") + except NoSuchElementException: + logger.error('Could not trigger contract on donation shop') + sys.exit(1) confirm_taler.click() # Taler as payment option chosen + # HUNG once, an explicit get() may be needed wait = WebDriverWait(client, 10) - confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) + try: + confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) + except TimeoutException: + logger.error('Could not confirm payment on donation shop') + sys.exit(1) confirm_pay.click() def buy_article(client): """Buy article at blog.test.taler.net. Assume the wallet has coins""" client.get(parse.urljoin(taler_baseurl, "blog")) - teaser = client.find_element(By.XPATH, "//ul/h3/a[1]") # Pick 'Foreword' chapter + try: + teaser = client.find_element(By.XPATH, "//ul/h3/a[1]") # Pick 'Foreword' chapter + except NoSuchElementException: + logger.error('Could not choose "Foreword" chapter on blog') + sys.exit(1) teaser.click() + # HUNG once, an explicit get() may be needed wait = WebDriverWait(client, 10) - confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) + try: + confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) + except TimeoutException: + logger.error('Could not confirm payment on blog') + sys.exit(1) confirm_pay.click() @@ -86,9 +111,18 @@ def register(client): """Register a new user to the bank delaying its execution until the profile page is shown""" client.get(parse.urljoin(taler_baseurl, "bank")) - register_link = client.find_element(By.XPATH, "//a[@href='/accounts/register/']") + try: + register_link = client.find_element(By.XPATH, "//a[@href='/accounts/register/']") + except NoSuchElementException: + logger.error("Could not find register link on bank's homepage") + sys.exit(1) register_link.click() - client.find_element(By.TAG_NAME, "form") + try: + client.find_element(By.TAG_NAME, "form") + except NoSuchElementException: + logger.error("Register form not found") + sys.exit(1) + register = """\ var form = document.getElementsByTagName('form')[0]; form.username.value = '%s'; @@ -98,7 +132,11 @@ def register(client): client.execute_script(register) # need implicit wait to be set up - button = client.find_element(By.ID, "select-exchange") + try: + button = client.find_element(By.ID, "select-exchange") + except NoSuchElementException: + logger.error("Selecting exchange impossible") + sys.exit(1) # when button is gotten, the browser is in the profile page # so the function can return if not is_error(client): @@ -111,35 +149,58 @@ def withdraw(client): """Register and withdraw (1) KUDOS for a fresh user""" register(client) # trigger withdrawal button - button = client.find_element(By.ID, "select-exchange") + try: + button = client.find_element(By.ID, "select-exchange") + except NoSuchElementException: + logger.error("Selecting exchange impossible") + sys.exit(1) button.click() location = client.execute_script("return document.location.href") client.get(location) # Confirm xchg wait = WebDriverWait(client, 10) - button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]"))) + try: + button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]"))) + except TimeoutException: + logger.error("Could not confirm exchange (therefore provide withdrawal needed data)") + sys.exit(1) # This click returns the captcha page (put wait?) button.click() - answer = client.find_element(By.XPATH, "//input[@name='pin_0']") - question = client.find_element(By.XPATH, "//span[@class='captcha-question']/div") + try: + answer = client.find_element(By.XPATH, "//input[@name='pin_0']") + question = client.find_element(By.XPATH, "//span[@class='captcha-question']/div") + except NoSuchElementException: + logger.error("Captcha page not gotten or malformed") + sys.exit(1) questionTok = question.text.split() op1 = int(questionTok[2]) op2 = int(questionTok[4]) res = {'+': op1 + op2, '-': op1 - op2, u'\u00d7': op1 * op2} answer.send_keys(res[questionTok[3]]) - form = client.find_element(By.TAG_NAME, "form") + try: + form = client.find_element(By.TAG_NAME, "form") + except NoSuchElementException: + logger.error("Could not submit captcha answer (therefore trigger withdrawal)") + sys.exit(1) form.submit() # check outcome - msg_succ = client.find_element(By.CLASS_NAME, "informational-ok") - if not msg_succ: - logger.error('Error in withdrawing') - else: logger.info('Withdrawal successful') - - -ret = client_setup() + try: + client.find_element(By.CLASS_NAME, "informational-ok") + except NoSuchElementException: + logger.error("Withdrawal not completed") + sys.exit(1) + logger.info("Withdrawal completed") + + +parser = argparse.ArgumentParser() +parser.add_argument('--extdir', help="Folder containing the unpacked extension", metavar="EXTDIR", type=str, dest="extdir", required=True) +args = parser.parse_args() +ret = client_setup(args) client = ret['client'] client.implicitly_wait(10) withdraw(client) make_donation(client) buy_article(client) +logger.info("Test passed") client.close() +sys.exit(0) -- cgit v1.2.3 From 08e60506d5df6db78fe29541b751fb7bf13861d0 Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Tue, 3 May 2016 11:12:12 +0200 Subject: selenium: the wallet can select amounts from the bank's dropdown --- selenium/test.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py index d697f73c8..b2bece4e0 100644 --- a/selenium/test.py +++ b/selenium/test.py @@ -145,7 +145,7 @@ def register(client): logger.error('User not registered at bank') -def withdraw(client): +def withdraw(client, amount_value=None): """Register and withdraw (1) KUDOS for a fresh user""" register(client) # trigger withdrawal button @@ -154,6 +154,13 @@ def withdraw(client): except NoSuchElementException: logger.error("Selecting exchange impossible") sys.exit(1) + if amount_value: + xpath = "//select/option[@value='" + str(amount_value) + "']" + try: + client.find_element(By.XPATH, xpath) + except NoSuchElementException: + logger.error("value '" + str(amount_value) + "' is not offered by this bank to withdraw, please adapt it") + sys.exit(1) button.click() location = client.execute_script("return document.location.href") client.get(location) @@ -198,7 +205,7 @@ args = parser.parse_args() ret = client_setup(args) client = ret['client'] client.implicitly_wait(10) -withdraw(client) +withdraw(client, 11) make_donation(client) buy_article(client) logger.info("Test passed") -- cgit v1.2.3 From 048e4f8dc9cfca81907ff6562125a92f9661882c Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Tue, 3 May 2016 14:16:04 +0200 Subject: nondefault amounts to donate via selenium --- selenium/test.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py index b2bece4e0..3d46585bf 100644 --- a/selenium/test.py +++ b/selenium/test.py @@ -42,17 +42,6 @@ def client_setup(args): html = client.find_element(By.TAG_NAME, "html") return {'client': client, 'ext_id': html.get_attribute('data-taler-wallet-id')} -# Note that the db appears to be reset automatically by the driver -def destroy_db(client, ext_id): - url = 'chrome-extension://' + ext_id + '/popup/popup.html#/debug' - client.get(url) - button = client.find_element(By.XPATH, "//div[@id='content']/button[3]") - button.click() - time.sleep(4) - alert = client.switch_to.alert - alert.accept() - - def is_error(client): """Return True in case of errors in the browser, False otherwise""" for log_type in ['browser']: @@ -63,7 +52,7 @@ def is_error(client): return False -def make_donation(client): +def make_donation(client, amount_value=None): """Make donation at shop.test.taler.net. Assume the wallet has coins""" client.get(parse.urljoin(taler_baseurl, "shop")) try: @@ -71,6 +60,14 @@ def make_donation(client): except NoSuchElementException: logger.error('No donation form found') sys.exit(1) + if amount_value: + xpath = "//select[@id='taler-donation']/option[@value='" + str(amount_value) + "']" + try: + desired_amount = client.find_element(By.XPATH, xpath) + desired_amount.click() + except NoSuchElementException: + logger.error("value '" + str(amount_value) + "' is not offered by this shop to donate, please adapt it") + sys.exit(1) form.submit() # amount and receiver chosen try: confirm_taler = client.find_element(By.XPATH, "//form//input[@type='button']") @@ -78,7 +75,9 @@ def make_donation(client): logger.error('Could not trigger contract on donation shop') sys.exit(1) confirm_taler.click() # Taler as payment option chosen - # HUNG once, an explicit get() may be needed + # explicit get() is needed, it hangs (sometimes) otherwise + time.sleep(1) + client.get(client.current_url) wait = WebDriverWait(client, 10) try: confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) @@ -97,7 +96,9 @@ def buy_article(client): logger.error('Could not choose "Foreword" chapter on blog') sys.exit(1) teaser.click() - # HUNG once, an explicit get() may be needed + # explicit get() is needed, it hangs (sometimes) otherwise + time.sleep(1) + client.get(client.current_url) wait = WebDriverWait(client, 10) try: confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) @@ -157,7 +158,8 @@ def withdraw(client, amount_value=None): if amount_value: xpath = "//select/option[@value='" + str(amount_value) + "']" try: - client.find_element(By.XPATH, xpath) + desired_amount = client.find_element(By.XPATH, xpath) + desired_amount.click() except NoSuchElementException: logger.error("value '" + str(amount_value) + "' is not offered by this bank to withdraw, please adapt it") sys.exit(1) @@ -205,8 +207,8 @@ args = parser.parse_args() ret = client_setup(args) client = ret['client'] client.implicitly_wait(10) -withdraw(client, 11) -make_donation(client) +withdraw(client, 10) +make_donation(client, 6.0) buy_article(client) logger.info("Test passed") client.close() -- cgit v1.2.3 From dc83b85e85110eb7ed2e96ecdbcbb90b5a5620aa Mon Sep 17 00:00:00 2001 From: Marcello Stanisci Date: Tue, 3 May 2016 15:05:58 +0200 Subject: selenium: adding function to switch between 'test' and 'demo' base URLs, used to introduce currency mismatches when testing errors management --- selenium/test.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'selenium/test.py') diff --git a/selenium/test.py b/selenium/test.py index 3d46585bf..14140e953 100644 --- a/selenium/test.py +++ b/selenium/test.py @@ -17,9 +17,10 @@ import time import logging import sys import os +import re logger = logging.getLogger(__name__) -taler_baseurl = os.environ['TALER_BASEURL'] if 'TALER_BASEURL' in os.environ else 'https://test.taler.net/' +taler_baseurl = os.environ.get('TALER_BASEURL', 'https://test.taler.net/') def client_setup(args): """Return a dict containing the driver and the extension's id""" @@ -52,6 +53,17 @@ def is_error(client): return False +def switch_base(): + """If 'test' is in TALER_BASEURL, then make it be 'demo', and viceversa. + Used to trig currency mismatch errors. It assumes that the https://{test,demo}.taler.net + layout is being used""" + global taler_baseurl + url = parse.urlparse(taler_baseurl) + if url[1] == 'test.taler.net': + taler_baseurl = "https://demo.taler.net" + if url[1] == 'demo.taler.net': + taler_baseurl = "https://test.taler.net" + def make_donation(client, amount_value=None): """Make donation at shop.test.taler.net. Assume the wallet has coins""" client.get(parse.urljoin(taler_baseurl, "shop")) @@ -208,6 +220,7 @@ ret = client_setup(args) client = ret['client'] client.implicitly_wait(10) withdraw(client, 10) +switch_base() # inducing error make_donation(client, 6.0) buy_article(client) logger.info("Test passed") -- cgit v1.2.3