bundling args in a class

This commit is contained in:
Marcello Stanisci 2017-11-02 16:20:48 +01:00
parent d4e8b98934
commit 5d816ca065
No known key found for this signature in database
GPG Key ID: 8D526861953F4C0F

View File

@ -29,6 +29,11 @@ taler_baseurl = os.environ.get('TALER_BASEURL',
'https://test.taler.net/') 'https://test.taler.net/')
display = Display(visible=0, size=(1024, 768)) display = Display(visible=0, size=(1024, 768))
class TestContext():
def __init__(self, client):
self.client = client
self.wait = WebDriverWait(client, 20)
def kill_display(): def kill_display():
if hasattr(display, "old_display_var"): if hasattr(display, "old_display_var"):
display.stop() display.stop()
@ -84,34 +89,22 @@ def print_log(client):
print("--- End of browser log ---") print("--- End of browser log ---")
def switch_base(): def make_donation(ctx, amount_menuentry):
"""If 'test' is in TALER_BASEURL, then make it be 'demo',
and viceversa. Used to trigger currency mismatch errors.
It assumes that the https://{test,demo}.taler.net layout
for URLs is 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):
"""Make donation at donations.test.taler.net. Assume """Make donation at donations.test.taler.net. Assume
the wallet has coins. Just donate to the default receiver""" the wallet has coins. Just donate to the default receiver"""
client.get(parse.urljoin(taler_baseurl, "donations")) ctx.client.get(parse.urljoin(taler_baseurl, "donations"))
try: try:
form = wait.until(EC.visibility_of_element_located((By.TAG_NAME, form = ctx.wait.until(EC.visibility_of_element_located((By.TAG_NAME,
"form"))) "form")))
except NoSuchElementException: except NoSuchElementException:
logger.error('No donation form found') logger.error('No donation form found')
return False return False
xpath_menu = '//select[@id="taler-donation"]' xpath_menu = '//select[@id="taler-donation"]'
try: try:
dropdown = client.find_element(By.XPATH, xpath_menu) dropdown = ctx.client.find_element(By.XPATH, xpath_menu)
for option in dropdown.find_elements_by_tag_name("option"): for option in dropdown.find_elements_by_tag_name("option"):
if option.get_attribute("innerHTML") == amount_menuentry: if option.get_attribute("innerHTML") == amount_menuentry:
option = wait.until(EC.visibility_of(option)) option = ctx.wait.until(EC.visibility_of(option))
option.click() option.click()
break break
except NoSuchElementException: except NoSuchElementException:
@ -120,14 +113,14 @@ def make_donation(client, amount_menuentry):
return False return False
form.submit() form.submit()
try: try:
confirm_taler = wait.until(EC.element_to_be_clickable((By.ID, confirm_taler = ctx.wait.until(EC.element_to_be_clickable((By.ID,
"select-payment-method"))) "select-payment-method")))
except NoSuchElementException: except NoSuchElementException:
logger.error('Could not trigger contract on donation shop') logger.error('Could not trigger contract on donation shop')
return False return False
confirm_taler.click() confirm_taler.click()
try: try:
confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, confirm_pay = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//button[@class='pure-button button-success']"))) "//button[@class='pure-button button-success']")))
except TimeoutException: except TimeoutException:
logger.error('Could not confirm payment on donation shop') logger.error('Could not confirm payment on donation shop')
@ -138,62 +131,62 @@ def make_donation(client, amount_menuentry):
# Check if the current page is displaying the article # Check if the current page is displaying the article
# whose title is 'title'. # whose title is 'title'.
def check_article(client, title): def check_article(ctx, title):
try: try:
wait.until(EC.visibility_of_element_located((By.XPATH, ctx.wait.until(EC.visibility_of_element_located((By.XPATH,
"//h1[contains(., '%s')]" % title.replace("_", " ")))) "//h1[contains(., '%s')]" % title.replace("_", " "))))
except NoSuchElementException: except NoSuchElementException:
logger.error("Article '%s' not shown on this (%s) page\ logger.error("Article '%s' not shown on this (%s) page\
" % (title, client.current_url)) " % (title, ctx.client.current_url))
return False return False
return True return True
def buy_article(client, title, fulfillment_url=None): def buy_article(ctx, title, fulfillment_url=None):
"""Buy article at shop.test.taler.net. Assume the wallet """Buy article at shop.test.taler.net. Assume the wallet
has coins. Return False if some error occurs, the fulfillment has coins. Return False if some error occurs, the fulfillment
URL otherwise""" URL otherwise"""
if fulfillment_url: if fulfillment_url:
client.get(fulfillment_url) ctx.client.get(fulfillment_url)
if check_article(client, title): if check_article(ctx, title):
return fulfillment_url return fulfillment_url
return False return False
client.get(parse.urljoin(taler_baseurl, "shop")) ctx.client.get(parse.urljoin(taler_baseurl, "shop"))
try: try:
teaser = wait.until(EC.element_to_be_clickable((By.XPATH, teaser = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//h3/a[@href=\"/essay/%s\"]" % title))) "//h3/a[@href=\"/essay/%s\"]" % title)))
client.execute_script("window.scrollBy(30, %s)" % teaser.location['y']) ctx.client.execute_script("window.scrollBy(30, %s)" % teaser.location['y'])
teaser.click() teaser.click()
except (NoSuchElementException, TimeoutException) as e: except (NoSuchElementException, TimeoutException) as e:
logger.error("Could not choose chapter '%s'" % title) logger.error("Could not choose chapter '%s'" % title)
return False return False
time.sleep(1) time.sleep(1)
try: try:
confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, confirm_pay = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//button[@class='pure-button button-success']"))) "//button[@class='pure-button button-success']")))
except TimeoutException: except TimeoutException:
logger.error('Could not confirm contract on blog (timed out)') logger.error('Could not confirm contract on blog (timed out)')
return False return False
confirm_pay.click() confirm_pay.click()
time.sleep(3) time.sleep(3)
if not check_article(client, title): if not check_article(ctx, title):
return False return False
return client.current_url return ctx.client.current_url
def register(client): def register(ctx):
"""Register a new user to the bank delaying its execution """Register a new user to the bank delaying its execution
until the profile page is shown""" until the profile page is shown"""
client.get(parse.urljoin(taler_baseurl, "bank")) ctx.client.get(parse.urljoin(taler_baseurl, "bank"))
try: try:
register_link = wait.until(EC.element_to_be_clickable((By.XPATH, register_link = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//a[@href='/accounts/register/']"))) "//a[@href='/accounts/register/']")))
except NoSuchElementException: except NoSuchElementException:
logger.error("Could not find register link on bank's homepage") logger.error("Could not find register link on bank's homepage")
return False return False
register_link.click() register_link.click()
try: try:
wait.until(EC.visibility_of_element_located((By.TAG_NAME, "form"))) ctx.wait.until(EC.visibility_of_element_located((By.TAG_NAME, "form")))
except NoSuchElementException: except NoSuchElementException:
logger.error("Register form not found") logger.error("Register form not found")
return False return False
@ -203,9 +196,9 @@ def register(client):
form.password.value = 'test'; form.password.value = 'test';
form.submit(); form.submit();
""" % str(int(time.time())) """ % str(int(time.time()))
client.execute_script(register) ctx.client.execute_script(register)
try: try:
wait.until(EC.element_to_be_clickable((By.ID, ctx.wait.until(EC.element_to_be_clickable((By.ID,
"select-exchange"))) "select-exchange")))
except NoSuchElementException: except NoSuchElementException:
logger.error("Selecting exchange impossible") logger.error("Selecting exchange impossible")
@ -213,11 +206,11 @@ def register(client):
return True return True
def withdraw(client, amount_menuentry): def withdraw(ctx, amount_menuentry):
"""Register and withdraw (1) KUDOS for a fresh user""" """Register and withdraw (1) KUDOS for a fresh user"""
# trigger withdrawal button # trigger withdrawal button
try: try:
button = wait.until(EC.element_to_be_clickable((By.ID, button = ctx.wait.until(EC.element_to_be_clickable((By.ID,
"select-exchange"))) "select-exchange")))
except NoSuchElementException: except NoSuchElementException:
logger.error("Selecting exchange impossible") logger.error("Selecting exchange impossible")
@ -226,10 +219,10 @@ def withdraw(client, amount_menuentry):
try: try:
# No need to wait: if 'button' above exists, then # No need to wait: if 'button' above exists, then
# menu elements do. # menu elements do.
dropdown = client.find_element(By.XPATH, xpath_menu) dropdown = ctx.client.find_element(By.XPATH, xpath_menu)
for option in dropdown.find_elements_by_tag_name("option"): for option in dropdown.find_elements_by_tag_name("option"):
if option.get_attribute("innerHTML") == amount_menuentry: if option.get_attribute("innerHTML") == amount_menuentry:
option = wait.until(EC.visibility_of(option)) option = ctx.wait.until(EC.visibility_of(option))
option.click() option.click()
break break
except NoSuchElementException: except NoSuchElementException:
@ -239,16 +232,16 @@ def withdraw(client, amount_menuentry):
button.click() button.click()
# Confirm exchange (in-wallet page) # Confirm exchange (in-wallet page)
try: try:
accept_exchange = wait.until(EC.element_to_be_clickable((By.XPATH, accept_exchange = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//button[@class='pure-button button-success']"))) "//button[@class='pure-button button-success']")))
except TimeoutException: except TimeoutException:
logger.error("Could not confirm exchange") logger.error("Could not confirm exchange")
return False return False
accept_exchange.click() accept_exchange.click()
try: try:
answer = wait.until(EC.element_to_be_clickable((By.XPATH, answer = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//input[@name='pin_0']"))) "//input[@name='pin_0']")))
question = wait.until(EC.element_to_be_clickable((By.XPATH, question = ctx.wait.until(EC.element_to_be_clickable((By.XPATH,
"//span[@class='captcha-question']/div"))) "//span[@class='captcha-question']/div")))
except NoSuchElementException: except NoSuchElementException:
@ -262,14 +255,14 @@ def withdraw(client, amount_menuentry):
try: try:
# No need to wait, if CAPTCHA elements exists # No need to wait, if CAPTCHA elements exists
# then submitting button has to. # then submitting button has to.
form = client.find_element(By.TAG_NAME, "form") form = ctx.client.find_element(By.TAG_NAME, "form")
except NoSuchElementException: except NoSuchElementException:
logger.error("Could not submit captcha answer") logger.error("Could not submit captcha answer")
return False return False
form.submit() form.submit()
# check outcome # check outcome
try: try:
wait.until(EC.presence_of_element_located((By.CLASS_NAME, ctx.wait.until(EC.presence_of_element_located((By.CLASS_NAME,
"informational-ok"))) "informational-ok")))
except NoSuchElementException: except NoSuchElementException:
logger.error("Withdrawal not completed") logger.error("Withdrawal not completed")
@ -290,34 +283,35 @@ parser.add_argument('--with-head',
action="store_true", dest="withhead") action="store_true", dest="withhead")
args = parser.parse_args() args = parser.parse_args()
client = client_setup(args)['client'] ctx = TestContext(client_setup(args)['client'])
wait = WebDriverWait(client, 20)
if not register(client): if not register(ctx):
print_log(client) print_log(ctx.client)
abort(client) abort(ctx.client)
if not withdraw(client, "10.00 TESTKUDOS"): if not withdraw(ctx, "10.00 TESTKUDOS"):
print_log(client) print_log(ctx.client)
abort(client) abort(ctx.client)
fulfillment_url_25 = buy_article(client, "25._The_Danger_of_Software_Patents") fulfillment_url_25 = buy_article(ctx,
"25._The_Danger_of_Software_Patents")
if not fulfillment_url_25: if not fulfillment_url_25:
print_log(client) print_log(ctx.client)
abort(client) abort(ctx.client)
client.delete_all_cookies() ctx.client.delete_all_cookies()
if not buy_article(client, "25._The_Danger_of_Software_Patents", fulfillment_url_25): if not buy_article(ctx, "25._The_Danger_of_Software_Patents",
print_log(client) fulfillment_url_25):
print_log(ctx.client)
logger.error("Could not replay payment") logger.error("Could not replay payment")
abort(client) abort(ctx.client)
if not make_donation(client, "1.0 TESTKUDOS"): if not make_donation(ctx, "1.0 TESTKUDOS"):
print_log(client) print_log(ctx.client)
abort(client) abort(ctx.client)
client.quit() ctx.client.quit()
kill_display() kill_display()
sys.exit(0) sys.exit(0)