2016-04-27 16:58:06 +02: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/
|
|
|
|
"""
|
|
|
|
|
2016-04-27 13:54:35 +02:00
|
|
|
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
|
2016-04-28 15:08:34 +02:00
|
|
|
from selenium.common.exceptions import NoSuchElementException, TimeoutException
|
2016-04-27 16:58:06 +02:00
|
|
|
from urllib import parse
|
2016-04-28 15:08:34 +02:00
|
|
|
import argparse
|
2016-04-27 13:54:35 +02:00
|
|
|
import time
|
|
|
|
import logging
|
2016-04-28 15:08:34 +02:00
|
|
|
import sys
|
2016-04-27 16:58:06 +02:00
|
|
|
import os
|
2016-05-03 15:05:58 +02:00
|
|
|
import re
|
2016-05-12 13:54:48 +02:00
|
|
|
import json
|
2016-04-27 13:54:35 +02:00
|
|
|
|
2016-05-12 15:06:53 +02:00
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
|
2016-04-27 13:54:35 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-05-03 15:05:58 +02:00
|
|
|
taler_baseurl = os.environ.get('TALER_BASEURL', 'https://test.taler.net/')
|
2016-04-27 13:54:35 +02:00
|
|
|
|
2016-04-28 15:08:34 +02:00
|
|
|
def client_setup(args):
|
2016-04-27 13:54:35 +02:00
|
|
|
"""Return a dict containing the driver and the extension's id"""
|
|
|
|
co = webdriver.ChromeOptions()
|
2016-05-12 15:06:53 +02:00
|
|
|
co.add_extension(args.ext)
|
2016-05-12 13:54:48 +02:00
|
|
|
cap = co.to_capabilities()
|
2016-04-27 13:54:35 +02:00
|
|
|
cap['loggingPrefs'] = {'driver': 'INFO', 'browser': 'INFO'}
|
2016-05-12 13:54:48 +02:00
|
|
|
if args.remote:
|
|
|
|
client = webdriver.Remote(desired_capabilities=cap, command_executor=args.remote)
|
|
|
|
else:
|
|
|
|
client = webdriver.Chrome(desired_capabilities=cap)
|
2016-04-27 13:54:35 +02:00
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2016-05-03 15:05:58 +02:00
|
|
|
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"
|
|
|
|
|
2016-05-03 14:16:04 +02:00
|
|
|
def make_donation(client, amount_value=None):
|
2016-04-27 14:39:39 +02:00
|
|
|
"""Make donation at shop.test.taler.net. Assume the wallet has coins"""
|
2016-04-27 16:58:06 +02:00
|
|
|
client.get(parse.urljoin(taler_baseurl, "shop"))
|
2016-04-28 15:08:34 +02:00
|
|
|
try:
|
|
|
|
form = client.find_element(By.TAG_NAME, "form")
|
|
|
|
except NoSuchElementException:
|
|
|
|
logger.error('No donation form found')
|
|
|
|
sys.exit(1)
|
2016-05-03 14:16:04 +02:00
|
|
|
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)
|
2016-04-27 14:39:39 +02:00
|
|
|
form.submit() # amount and receiver chosen
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 14:39:39 +02:00
|
|
|
confirm_taler.click() # Taler as payment option chosen
|
2016-05-03 14:16:04 +02:00
|
|
|
# explicit get() is needed, it hangs (sometimes) otherwise
|
|
|
|
time.sleep(1)
|
|
|
|
client.get(client.current_url)
|
2016-04-27 14:39:39 +02:00
|
|
|
wait = WebDriverWait(client, 10)
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 14:39:39 +02:00
|
|
|
confirm_pay.click()
|
|
|
|
|
2016-04-27 16:58:06 +02:00
|
|
|
|
|
|
|
def buy_article(client):
|
|
|
|
"""Buy article at blog.test.taler.net. Assume the wallet has coins"""
|
|
|
|
client.get(parse.urljoin(taler_baseurl, "blog"))
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 16:58:06 +02:00
|
|
|
teaser.click()
|
2016-05-03 14:16:04 +02:00
|
|
|
# explicit get() is needed, it hangs (sometimes) otherwise
|
|
|
|
time.sleep(1)
|
|
|
|
client.get(client.current_url)
|
2016-04-27 16:58:06 +02:00
|
|
|
wait = WebDriverWait(client, 10)
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 16:58:06 +02:00
|
|
|
confirm_pay.click()
|
|
|
|
|
|
|
|
|
2016-04-27 13:54:35 +02:00
|
|
|
def register(client):
|
|
|
|
"""Register a new user to the bank delaying its execution until the
|
|
|
|
profile page is shown"""
|
2016-04-27 16:58:06 +02:00
|
|
|
client.get(parse.urljoin(taler_baseurl, "bank"))
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 16:58:06 +02:00
|
|
|
register_link.click()
|
2016-04-28 15:08:34 +02:00
|
|
|
try:
|
|
|
|
client.find_element(By.TAG_NAME, "form")
|
|
|
|
except NoSuchElementException:
|
|
|
|
logger.error("Register form not found")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2016-04-27 13:54:35 +02:00
|
|
|
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
|
2016-04-28 15:08:34 +02:00
|
|
|
try:
|
|
|
|
button = client.find_element(By.ID, "select-exchange")
|
|
|
|
except NoSuchElementException:
|
|
|
|
logger.error("Selecting exchange impossible")
|
|
|
|
sys.exit(1)
|
2016-04-27 13:54:35 +02:00
|
|
|
# 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')
|
|
|
|
|
|
|
|
|
2016-05-03 11:12:12 +02:00
|
|
|
def withdraw(client, amount_value=None):
|
2016-04-27 13:54:35 +02:00
|
|
|
"""Register and withdraw (1) KUDOS for a fresh user"""
|
|
|
|
register(client)
|
|
|
|
# trigger withdrawal button
|
2016-04-28 15:08:34 +02:00
|
|
|
try:
|
|
|
|
button = client.find_element(By.ID, "select-exchange")
|
|
|
|
except NoSuchElementException:
|
|
|
|
logger.error("Selecting exchange impossible")
|
|
|
|
sys.exit(1)
|
2016-05-03 11:12:12 +02:00
|
|
|
if amount_value:
|
|
|
|
xpath = "//select/option[@value='" + str(amount_value) + "']"
|
|
|
|
try:
|
2016-05-03 14:16:04 +02:00
|
|
|
desired_amount = client.find_element(By.XPATH, xpath)
|
|
|
|
desired_amount.click()
|
2016-05-03 11:12:12 +02:00
|
|
|
except NoSuchElementException:
|
|
|
|
logger.error("value '" + str(amount_value) + "' is not offered by this bank to withdraw, please adapt it")
|
|
|
|
sys.exit(1)
|
2016-04-27 13:54:35 +02:00
|
|
|
button.click()
|
|
|
|
location = client.execute_script("return document.location.href")
|
|
|
|
client.get(location)
|
|
|
|
# Confirm xchg
|
|
|
|
wait = WebDriverWait(client, 10)
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 13:54:35 +02:00
|
|
|
# This click returns the captcha page (put wait?)
|
|
|
|
button.click()
|
2016-04-28 15:08:34 +02:00
|
|
|
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)
|
2016-04-27 13:54:35 +02: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]])
|
2016-04-28 15:08:34 +02:00
|
|
|
try:
|
|
|
|
form = client.find_element(By.TAG_NAME, "form")
|
|
|
|
except NoSuchElementException:
|
|
|
|
logger.error("Could not submit captcha answer (therefore trigger withdrawal)")
|
|
|
|
sys.exit(1)
|
2016-04-27 13:54:35 +02:00
|
|
|
form.submit()
|
|
|
|
# check outcome
|
2016-04-28 15:08:34 +02:00
|
|
|
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()
|
2016-05-12 15:06:53 +02:00
|
|
|
parser.add_argument('--ext', help="packed extension (.crx file)", metavar="CRX", type=str, dest="ext", required=True)
|
2016-05-12 13:54:48 +02:00
|
|
|
parser.add_argument('--remote', help="Whether the test is to be run against URI, or locally", metavar="URI", type=str, dest="remote")
|
2016-04-28 15:08:34 +02:00
|
|
|
args = parser.parse_args()
|
2016-05-22 21:50:41 +02:00
|
|
|
logger.info("testing against " + taler_baseurl)
|
2016-05-12 11:44:56 +02:00
|
|
|
logger.info("Getting extension's ID..")
|
2016-04-28 15:08:34 +02:00
|
|
|
ret = client_setup(args)
|
2016-05-12 11:44:56 +02:00
|
|
|
logger.info("Creating the browser driver..")
|
2016-04-27 13:54:35 +02:00
|
|
|
client = ret['client']
|
2016-04-27 16:58:06 +02:00
|
|
|
client.implicitly_wait(10)
|
2016-05-12 11:44:56 +02:00
|
|
|
logger.info("Withdrawing..")
|
2016-05-03 14:16:04 +02:00
|
|
|
withdraw(client, 10)
|
2016-05-12 11:44:56 +02:00
|
|
|
# switch_base() # inducing error
|
|
|
|
logger.info("Making donations..")
|
2016-05-03 14:16:04 +02:00
|
|
|
make_donation(client, 6.0)
|
2016-05-12 11:44:56 +02:00
|
|
|
logger.info("Buying article..")
|
2016-04-27 16:58:06 +02:00
|
|
|
buy_article(client)
|
2016-04-28 15:08:34 +02:00
|
|
|
logger.info("Test passed")
|
2016-04-27 16:58:06 +02:00
|
|
|
client.close()
|
2016-04-28 15:08:34 +02:00
|
|
|
sys.exit(0)
|