1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
#!/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
from pyvirtualdisplay import Display
from urllib import parse
import argparse
import time
import logging
import sys
import os
import re
import json
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
taler_baseurl = os.environ.get('TALER_BASEURL', 'https://test.taler.net/')
display = Display(visible=0, size=(1024, 768))
def abort(client):
client.quit()
if display.old_display_var:
display.stop()
sys.exit(1)
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'}
if not args.withhead:
display.start()
if args.remote:
client = webdriver.Remote(desired_capabilities=cap, command_executor=args.remote)
else:
logger.info("About to get chromed")
client = webdriver.Chrome(desired_capabilities=cap)
logger.info("Got chromed")
client.get('https://taler.net')
listener = """\
document.addEventListener('taler-query-id-result', 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")
ext_id = html.get_attribute('data-taler-wallet-id')
logger.info("Extension ID: %s" % str(ext_id))
return {'client': client, 'ext_id': ext_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 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_menuentry=None):
"""Make donation at donations.test.taler.net. Assume the wallet has coins.
Just donate to the default receiver"""
client.get(parse.urljoin(taler_baseurl, "donations"))
try:
form = client.find_element(By.TAG_NAME, "form")
except NoSuchElementException:
logger.error('No donation form found')
abort(client)
if amount_menuentry:
xpath_menu = '//select[@id="taler-donation"]'
try:
dropdown = client.find_element(By.XPATH, xpath_menu)
for option in dropdown.find_elements_by_tag_name("option"):
if option.get_attribute("innerHTML") == amount_menuentry:
option = WebDriverWait(client, 10).until(EC.visibility_of(option))
logger.info("Picked donation %s" % option.text)
option.click()
break
except NoSuchElementException:
logger.error("value '" + str(amount_value) + "' is not offered by this shop to donate, please adapt it")
abort(client)
form.submit() # amount and receiver chosen
wait = WebDriverWait(client, 10)
try:
confirm_taler = wait.until(EC.element_to_be_clickable((By.ID, "select-payment-method")))
logger.info("confirm_taler: %s" % confirm_taler.get_attribute("outerHTML"))
except NoSuchElementException:
logger.error('Could not trigger contract on donation shop')
abort(client)
confirm_taler.click() # Taler as payment option chosen
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')
abort(client)
confirm_pay.click()
def check_article(client, title):
try:
client.find_element(By.XPATH, "//h1[contains(., '%s')]" % title.replace("_", " "))
except NoSuchElementException:
logger.error("Article not correctly bought")
client.quit()
display.stop()
return False
return True
def buy_article(client, title, fulfillment_url=None):
"""Buy article at shop.test.taler.net. Assume the wallet has coins.
Return False if some error occurs, the fulfillment URL otherwise"""
if fulfillment_url:
client.get(fulfillment_url)
if check_article(client, title):
return fulfillment_url
return False
client.get(parse.urljoin(taler_baseurl, "shop"))
wait = WebDriverWait(client, 10)
try:
teaser = wait.until(EC.element_to_be_clickable((By.XPATH, "//h3/a[@href=\"/essay/%s\"]" % title)))
logger.info("Scrolling to article position: %s", teaser.location['y'])
client.execute_script("window.scrollBy(30, %s)" % teaser.location['y'])
teaser.click()
except (NoSuchElementException, TimeoutException) as e:
logger.error("Could not choose chapter '%s'" % title)
abort(client)
time.sleep(1)
try:
confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']")))
logger.info("Pay button turned clickable")
except TimeoutException:
logger.error('Could not confirm payment on blog')
abort(client)
confirm_pay.click()
time.sleep(3)
if not check_article(client, title):
return False
return client.current_url
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"))
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")
abort(client)
register_link.click()
try:
client.find_element(By.TAG_NAME, "form")
except NoSuchElementException:
logger.error("Register form not found")
abort(client)
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
try:
button = client.find_element(By.ID, "select-exchange")
except NoSuchElementException:
logger.error("Selecting exchange impossible")
abort(client)
# 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, amount_menuentry=None):
"""Register and withdraw (1) KUDOS for a fresh user"""
register(client)
logger.info("Withdrawing..")
wait = WebDriverWait(client, 10)
# trigger withdrawal button
try:
button = client.find_element(By.ID, "select-exchange")
except NoSuchElementException:
logger.error("Selecting exchange impossible")
abort(client)
if amount_menuentry:
xpath_menu = '//select[@id="reserve-amount"]'
try:
dropdown = client.find_element(By.XPATH, xpath_menu)
for option in dropdown.find_elements_by_tag_name("option"):
if option.get_attribute("innerHTML") == amount_menuentry:
option = WebDriverWait(client, 10).until(EC.visibility_of(option))
option.click()
break
except NoSuchElementException:
logger.error("value '" + str(amount_value) + "' is not offered by this bank to withdraw, please adapt it")
abort(client)
# confirm amount
logger.info("About to confirm amount")
button.click()
logger.info("Exchange confirmation refreshed")
# Confirm exchange (in-wallet page)
try:
logger.info("Polling for the button")
exchange_input = client.find_element(By.XPATH, "//input[@class='url']")
# Construct Exchange URL
l = list(parse.urlsplit(taler_baseurl))
l[1] = "exchange" + "." + l[1]
exchange_input.send_keys(parse.urlunsplit(l))
accept_exchange = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]")))
except TimeoutException:
logger.error("Could not confirm exchange")
abort(client)
# This click returns the captcha page (put wait?)
logger.info("About to confirm exchange")
accept_exchange.click()
try:
accept_fees = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]")))
except TimeoutException:
logger.error("Could not accept fees")
abort(client)
accept_fees.click()
# Properly wait for the next page to be loaded?
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")
abort(client)
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:
form = client.find_element(By.TAG_NAME, "form")
except NoSuchElementException:
logger.error("Could not submit captcha answer (therefore trigger withdrawal)")
abort(client)
form.submit()
# check outcome
try:
client.find_element(By.CLASS_NAME, "informational-ok")
except NoSuchElementException:
logger.error("Withdrawal not completed")
abort(client)
logger.info("Withdrawal completed")
parser = argparse.ArgumentParser()
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")
args = parser.parse_args()
logger.info("testing against " + taler_baseurl)
logger.info("Getting extension's ID..")
ret = client_setup(args)
logger.info("Creating the browser driver..")
client = ret['client']
client.implicitly_wait(10)
withdraw(client, "10.00 TESTKUDOS")
# FIXME: wait for coins appropriately
time.sleep(2)
logger.info("Buying article..")
fulfillment_url_25 = buy_article(client, "25._The_Danger_of_Software_Patents")
fulfillment_url_41 = buy_article(client, "41._Avoiding_Ruinous_Compromises")
client.delete_all_cookies()
ret = buy_article(client, "25._The_Danger_of_Software_Patents", fulfillment_url_25)
logger.info("Donating..")
make_donation(client, "1.0 TESTKUDOS")
logger.info("Bookmarked purchase: '%s'" % ret)
logger.info("Test passed")
client.quit()
display.stop()
sys.exit(0)
|