2016-11-12 23:34:39 +01:00
|
|
|
#!/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, Select
|
|
|
|
from selenium.common.exceptions import NoSuchElementException, TimeoutException
|
|
|
|
from selenium.webdriver.common.action_chains import ActionChains
|
2017-02-16 09:54:08 +01:00
|
|
|
from pyvirtualdisplay import Display
|
2016-11-12 23:34:39 +01:00
|
|
|
from urllib import parse
|
|
|
|
import argparse
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
|
2017-11-02 15:59:06 +01:00
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s',
|
|
|
|
level=logging.INFO)
|
2016-11-12 23:34:39 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-11-02 15:59:06 +01:00
|
|
|
taler_baseurl = os.environ.get('TALER_BASEURL',
|
|
|
|
'https://test.taler.net/')
|
2017-02-20 15:22:19 +01:00
|
|
|
display = Display(visible=0, size=(1024, 768))
|
2017-07-19 14:36:29 +02:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
class TestContext():
|
|
|
|
def __init__(self, client):
|
|
|
|
self.client = client
|
|
|
|
self.wait = WebDriverWait(client, 20)
|
|
|
|
|
2017-11-02 13:47:32 +01:00
|
|
|
def kill_display():
|
2017-07-19 22:05:22 +02:00
|
|
|
if hasattr(display, "old_display_var"):
|
2017-07-19 14:36:29 +02:00
|
|
|
display.stop()
|
2017-11-02 13:47:32 +01:00
|
|
|
|
|
|
|
# All the operations we need upon errors.
|
|
|
|
def abort(client):
|
|
|
|
client.quit()
|
|
|
|
kill_display()
|
2017-07-19 14:36:29 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2016-11-12 23:34:39 +01:00
|
|
|
def client_setup(args):
|
|
|
|
"""Return a dict containing the driver and the extension's id"""
|
|
|
|
co = webdriver.ChromeOptions()
|
|
|
|
if args.ext:
|
|
|
|
co.add_extension(args.ext)
|
|
|
|
elif args.extdir:
|
|
|
|
co.add_argument("load-extension=%s" % args.extdir)
|
|
|
|
else:
|
|
|
|
logger.error("Provide one between '--ext' and '--ext-unpacked'")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
cap = co.to_capabilities()
|
|
|
|
cap['loggingPrefs'] = {'driver': 'INFO', 'browser': 'INFO'}
|
|
|
|
|
2017-07-19 14:36:29 +02:00
|
|
|
if not args.withhead:
|
|
|
|
display.start()
|
2016-11-12 23:34:39 +01:00
|
|
|
if args.remote:
|
2017-11-02 15:59:06 +01:00
|
|
|
client = webdriver.Remote(desired_capabilities=cap,
|
|
|
|
command_executor=args.remote)
|
2016-11-12 23:34:39 +01:00
|
|
|
else:
|
|
|
|
client = webdriver.Chrome(desired_capabilities=cap)
|
|
|
|
client.get('https://taler.net')
|
2017-02-15 17:07:45 +01:00
|
|
|
|
2016-11-12 23:34:39 +01:00
|
|
|
listener = """\
|
2017-02-15 17:07:45 +01:00
|
|
|
document.addEventListener('taler-query-id-result', function(evt){
|
2016-11-12 23:34:39 +01:00
|
|
|
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)
|
|
|
|
html = client.find_element(By.TAG_NAME, "html")
|
2017-02-15 17:07:45 +01:00
|
|
|
ext_id = html.get_attribute('data-taler-wallet-id')
|
|
|
|
logger.info("Extension ID: %s" % str(ext_id))
|
|
|
|
return {'client': client, 'ext_id': ext_id}
|
2016-11-12 23:34:39 +01:00
|
|
|
|
2017-10-29 23:37:18 +01:00
|
|
|
def print_log(client):
|
|
|
|
print("--- Dumping browser log: ---")
|
|
|
|
for log in client.get_log("browser"):
|
|
|
|
print(log['level'] + ': ' + log['message'])
|
|
|
|
print("--- End of browser log ---")
|
|
|
|
|
2016-11-12 23:34:39 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
def make_donation(ctx, amount_menuentry):
|
2017-11-02 13:47:32 +01:00
|
|
|
"""Make donation at donations.test.taler.net. Assume
|
|
|
|
the wallet has coins. Just donate to the default receiver"""
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.get(parse.urljoin(taler_baseurl, "donations"))
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
form = ctx.wait.until(EC.visibility_of_element_located((By.TAG_NAME,
|
2017-11-02 15:59:06 +01:00
|
|
|
"form")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error('No donation form found')
|
2017-11-02 13:47:32 +01:00
|
|
|
return False
|
|
|
|
xpath_menu = '//select[@id="taler-donation"]'
|
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
dropdown = ctx.client.find_element(By.XPATH, xpath_menu)
|
2017-11-02 13:47:32 +01:00
|
|
|
for option in dropdown.find_elements_by_tag_name("option"):
|
|
|
|
if option.get_attribute("innerHTML") == amount_menuentry:
|
2017-11-02 16:20:48 +01:00
|
|
|
option = ctx.wait.until(EC.visibility_of(option))
|
2017-11-02 13:47:32 +01:00
|
|
|
option.click()
|
|
|
|
break
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-11-02 13:47:32 +01:00
|
|
|
logger.error("amount '" + str(amount_value) + "\
|
|
|
|
' is not offered by this shop to donate")
|
|
|
|
return False
|
|
|
|
form.submit()
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
confirm_taler = ctx.wait.until(EC.element_to_be_clickable((By.ID,
|
2017-11-02 15:59:06 +01:00
|
|
|
"select-payment-method")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error('Could not trigger contract on donation shop')
|
2017-11-02 13:47:32 +01:00
|
|
|
return False
|
|
|
|
confirm_taler.click()
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
confirm_pay = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-10-17 13:30:55 +02:00
|
|
|
"//button[@class='pure-button button-success']")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error('Could not confirm payment on donation shop')
|
2017-11-02 13:47:32 +01:00
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
confirm_pay.click()
|
2017-11-02 13:47:32 +01:00
|
|
|
return True
|
2016-11-12 23:34:39 +01:00
|
|
|
|
2017-11-02 11:59:36 +01:00
|
|
|
# Check if the current page is displaying the article
|
|
|
|
# whose title is 'title'.
|
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
def check_article(ctx, title):
|
2017-02-20 14:52:08 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.wait.until(EC.visibility_of_element_located((By.XPATH,
|
2017-11-02 15:59:06 +01:00
|
|
|
"//h1[contains(., '%s')]" % title.replace("_", " "))))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-11-02 15:59:06 +01:00
|
|
|
logger.error("Article '%s' not shown on this (%s) page\
|
2017-11-02 16:20:48 +01:00
|
|
|
" % (title, ctx.client.current_url))
|
2017-02-20 14:52:08 +01:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
def buy_article(ctx, title, fulfillment_url=None):
|
2017-11-02 13:47:32 +01:00
|
|
|
"""Buy article at shop.test.taler.net. Assume the wallet
|
|
|
|
has coins. Return False if some error occurs, the fulfillment
|
|
|
|
URL otherwise"""
|
2017-11-02 12:51:32 +01:00
|
|
|
|
2017-02-20 14:52:08 +01:00
|
|
|
if fulfillment_url:
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.get(fulfillment_url)
|
|
|
|
if check_article(ctx, title):
|
2017-02-20 14:52:08 +01:00
|
|
|
return fulfillment_url
|
|
|
|
return False
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.get(parse.urljoin(taler_baseurl, "shop"))
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
teaser = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-11-02 13:47:32 +01:00
|
|
|
"//h3/a[@href=\"/essay/%s\"]" % title)))
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.execute_script("window.scrollBy(30, %s)" % teaser.location['y'])
|
2017-02-20 14:52:08 +01:00
|
|
|
teaser.click()
|
2017-06-27 22:53:37 +02:00
|
|
|
except (NoSuchElementException, TimeoutException) as e:
|
|
|
|
logger.error("Could not choose chapter '%s'" % title)
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
time.sleep(1)
|
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
confirm_pay = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-10-17 13:30:55 +02:00
|
|
|
"//button[@class='pure-button button-success']")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-11-02 13:47:32 +01:00
|
|
|
logger.error('Could not confirm contract on blog (timed out)')
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
confirm_pay.click()
|
2017-02-20 14:52:08 +01:00
|
|
|
time.sleep(3)
|
2017-11-02 16:20:48 +01:00
|
|
|
if not check_article(ctx, title):
|
2017-02-20 14:52:08 +01:00
|
|
|
return False
|
2017-11-02 16:20:48 +01:00
|
|
|
return ctx.client.current_url
|
2016-11-12 23:34:39 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
def register(ctx):
|
2017-11-02 13:47:32 +01:00
|
|
|
"""Register a new user to the bank delaying its execution
|
|
|
|
until the profile page is shown"""
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.get(parse.urljoin(taler_baseurl, "bank"))
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
register_link = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-11-02 15:59:06 +01:00
|
|
|
"//a[@href='/accounts/register/']")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error("Could not find register link on bank's homepage")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
register_link.click()
|
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.wait.until(EC.visibility_of_element_located((By.TAG_NAME, "form")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error("Register form not found")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
register = """\
|
|
|
|
var form = document.getElementsByTagName('form')[0];
|
|
|
|
form.username.value = '%s';
|
|
|
|
form.password.value = 'test';
|
|
|
|
form.submit();
|
2017-11-02 13:47:32 +01:00
|
|
|
""" % str(int(time.time()))
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.execute_script(register)
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.wait.until(EC.element_to_be_clickable((By.ID,
|
2017-11-02 15:59:06 +01:00
|
|
|
"select-exchange")))
|
2017-11-22 11:18:34 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error("Selecting exchange impossible")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
|
|
|
return True
|
2016-11-12 23:34:39 +01:00
|
|
|
|
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
def withdraw(ctx, amount_menuentry):
|
2016-11-12 23:34:39 +01:00
|
|
|
"""Register and withdraw (1) KUDOS for a fresh user"""
|
|
|
|
# trigger withdrawal button
|
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
button = ctx.wait.until(EC.element_to_be_clickable((By.ID,
|
2017-11-02 15:59:06 +01:00
|
|
|
"select-exchange")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error("Selecting exchange impossible")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
|
|
|
xpath_menu = '//select[@id="reserve-amount"]'
|
|
|
|
try:
|
2017-11-02 15:59:06 +01:00
|
|
|
# No need to wait: if 'button' above exists, then
|
|
|
|
# menu elements do.
|
2017-11-02 16:20:48 +01:00
|
|
|
dropdown = ctx.client.find_element(By.XPATH, xpath_menu)
|
2017-11-02 12:51:32 +01:00
|
|
|
for option in dropdown.find_elements_by_tag_name("option"):
|
|
|
|
if option.get_attribute("innerHTML") == amount_menuentry:
|
2017-11-02 16:20:48 +01:00
|
|
|
option = ctx.wait.until(EC.visibility_of(option))
|
2017-11-02 12:51:32 +01:00
|
|
|
option.click()
|
|
|
|
break
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-11-02 13:47:32 +01:00
|
|
|
logger.error("amount '" + str(amount_value) + "' \
|
|
|
|
is not offered by this bank to withdraw")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
button.click()
|
2017-02-15 17:07:45 +01:00
|
|
|
# Confirm exchange (in-wallet page)
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
accept_exchange = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-11-02 12:51:32 +01:00
|
|
|
"//button[@class='pure-button button-success']")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-02-15 17:07:45 +01:00
|
|
|
logger.error("Could not confirm exchange")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
2017-06-27 22:53:37 +02:00
|
|
|
accept_exchange.click()
|
2016-11-12 23:34:39 +01:00
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
answer = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-11-02 15:59:06 +01:00
|
|
|
"//input[@name='pin_0']")))
|
2017-11-02 16:20:48 +01:00
|
|
|
question = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
|
2017-11-02 15:59:06 +01:00
|
|
|
"//span[@class='captcha-question']/div")))
|
|
|
|
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-11-02 12:51:32 +01:00
|
|
|
logger.error("Captcha page unavailable or malformed")
|
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
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]])
|
|
|
|
try:
|
2017-11-02 15:59:06 +01:00
|
|
|
# No need to wait, if CAPTCHA elements exists
|
|
|
|
# then submitting button has to.
|
2017-11-02 16:20:48 +01:00
|
|
|
form = ctx.client.find_element(By.TAG_NAME, "form")
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2017-11-02 12:51:32 +01:00
|
|
|
logger.error("Could not submit captcha answer")
|
|
|
|
return False
|
2016-11-12 23:34:39 +01:00
|
|
|
form.submit()
|
|
|
|
# check outcome
|
|
|
|
try:
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.wait.until(EC.presence_of_element_located((By.CLASS_NAME,
|
2017-11-02 15:59:06 +01:00
|
|
|
"informational-ok")))
|
2017-11-23 11:27:01 +01:00
|
|
|
except (NoSuchElementException, TimeoutException):
|
2016-11-12 23:34:39 +01:00
|
|
|
logger.error("Withdrawal not completed")
|
2017-11-02 12:51:32 +01:00
|
|
|
return False
|
|
|
|
return True
|
2016-11-12 23:34:39 +01:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2017-11-02 11:59:36 +01:00
|
|
|
parser.add_argument('--ext', help="packed extension (.crx file)",
|
|
|
|
metavar="CRX", type=str, dest="ext")
|
|
|
|
parser.add_argument('--ext-unpacked',
|
|
|
|
help="loads unpacked extension from directory",
|
|
|
|
metavar="EXTDIR", type=str, dest="extdir")
|
|
|
|
parser.add_argument('--remote',
|
|
|
|
help="Whether the test is to be run against URI, or locally",
|
|
|
|
metavar="URI", type=str, dest="remote")
|
|
|
|
parser.add_argument('--with-head',
|
|
|
|
help="Graphically shows the browser (useful to debug)",
|
|
|
|
action="store_true", dest="withhead")
|
2016-11-12 23:34:39 +01:00
|
|
|
args = parser.parse_args()
|
2017-11-02 12:51:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx = TestContext(client_setup(args)['client'])
|
2017-11-02 12:51:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
if not register(ctx):
|
|
|
|
print_log(ctx.client)
|
|
|
|
abort(ctx.client)
|
2017-11-02 12:51:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
if not withdraw(ctx, "10.00 TESTKUDOS"):
|
|
|
|
print_log(ctx.client)
|
|
|
|
abort(ctx.client)
|
2017-11-02 12:51:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
fulfillment_url_25 = buy_article(ctx,
|
|
|
|
"25._The_Danger_of_Software_Patents")
|
2017-11-02 13:47:32 +01:00
|
|
|
|
|
|
|
if not fulfillment_url_25:
|
2017-11-02 16:20:48 +01:00
|
|
|
print_log(ctx.client)
|
|
|
|
abort(ctx.client)
|
2017-11-02 13:47:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.delete_all_cookies()
|
2017-11-02 13:47:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
if not buy_article(ctx, "25._The_Danger_of_Software_Patents",
|
|
|
|
fulfillment_url_25):
|
|
|
|
print_log(ctx.client)
|
2017-11-02 13:47:32 +01:00
|
|
|
logger.error("Could not replay payment")
|
2017-11-02 16:20:48 +01:00
|
|
|
abort(ctx.client)
|
2017-11-02 13:47:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
if not make_donation(ctx, "1.0 TESTKUDOS"):
|
|
|
|
print_log(ctx.client)
|
|
|
|
abort(ctx.client)
|
2017-11-02 13:47:32 +01:00
|
|
|
|
2017-11-02 16:20:48 +01:00
|
|
|
ctx.client.quit()
|
2017-11-02 13:47:32 +01:00
|
|
|
kill_display()
|
2016-11-12 23:34:39 +01:00
|
|
|
sys.exit(0)
|