closing client and display upon each exception

This commit is contained in:
Marcello Stanisci 2017-03-26 17:10:59 +02:00
parent cad2ce3a08
commit 2a1ece8417
No known key found for this signature in database
GPG Key ID: 8D526861953F4C0F

View File

@ -92,6 +92,8 @@ def make_donation(client, amount_menuentry=None):
form = client.find_element(By.TAG_NAME, "form") form = client.find_element(By.TAG_NAME, "form")
except NoSuchElementException: except NoSuchElementException:
logger.error('No donation form found') logger.error('No donation form found')
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
if amount_menuentry: if amount_menuentry:
xpath_menu = '//select[@id="taler-donation"]' xpath_menu = '//select[@id="taler-donation"]'
@ -105,6 +107,8 @@ def make_donation(client, amount_menuentry=None):
break break
except NoSuchElementException: except NoSuchElementException:
logger.error("value '" + str(amount_value) + "' is not offered by this shop to donate, please adapt it") logger.error("value '" + str(amount_value) + "' is not offered by this shop to donate, please adapt it")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
form.submit() # amount and receiver chosen form.submit() # amount and receiver chosen
wait = WebDriverWait(client, 10) wait = WebDriverWait(client, 10)
@ -113,12 +117,16 @@ def make_donation(client, amount_menuentry=None):
logger.info("confirm_taler: %s" % confirm_taler.get_attribute("outerHTML")) logger.info("confirm_taler: %s" % confirm_taler.get_attribute("outerHTML"))
except NoSuchElementException: except NoSuchElementException:
logger.error('Could not trigger contract on donation shop') logger.error('Could not trigger contract on donation shop')
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
confirm_taler.click() # Taler as payment option chosen confirm_taler.click() # Taler as payment option chosen
try: try:
confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']"))) confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='accept']")))
except TimeoutException: except TimeoutException:
logger.error('Could not confirm payment on donation shop') logger.error('Could not confirm payment on donation shop')
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
confirm_pay.click() confirm_pay.click()
@ -127,6 +135,8 @@ def check_article(client, title):
client.find_element(By.XPATH, "//h1[contains(., '%s')]" % title.replace("_", " ")) client.find_element(By.XPATH, "//h1[contains(., '%s')]" % title.replace("_", " "))
except NoSuchElementException: except NoSuchElementException:
logger.error("Article not correctly bought") logger.error("Article not correctly bought")
client.quit()
display.stop()
return False return False
return True return True
@ -151,6 +161,8 @@ def buy_article(client, title, fulfillment_url=None):
teaser.click() teaser.click()
except (NoSuchElementException, TimeoutException): except (NoSuchElementException, TimeoutException):
logger.error('Could not choose "Foreword" chapter on blog') logger.error('Could not choose "Foreword" chapter on blog')
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
time.sleep(1) time.sleep(1)
try: try:
@ -158,6 +170,8 @@ def buy_article(client, title, fulfillment_url=None):
logger.info("Pay button turned clickable") logger.info("Pay button turned clickable")
except TimeoutException: except TimeoutException:
logger.error('Could not confirm payment on blog') logger.error('Could not confirm payment on blog')
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
confirm_pay.click() confirm_pay.click()
time.sleep(3) time.sleep(3)
@ -173,12 +187,16 @@ def register(client):
register_link = client.find_element(By.XPATH, "//a[@href='/accounts/register/']") register_link = client.find_element(By.XPATH, "//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")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
register_link.click() register_link.click()
try: try:
client.find_element(By.TAG_NAME, "form") client.find_element(By.TAG_NAME, "form")
except NoSuchElementException: except NoSuchElementException:
logger.error("Register form not found") logger.error("Register form not found")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
register = """\ register = """\
@ -194,6 +212,8 @@ def register(client):
button = client.find_element(By.ID, "select-exchange") button = client.find_element(By.ID, "select-exchange")
except NoSuchElementException: except NoSuchElementException:
logger.error("Selecting exchange impossible") logger.error("Selecting exchange impossible")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
# when button is gotten, the browser is in the profile page # when button is gotten, the browser is in the profile page
# so the function can return # so the function can return
@ -213,6 +233,8 @@ def withdraw(client, amount_menuentry=None):
button = client.find_element(By.ID, "select-exchange") button = client.find_element(By.ID, "select-exchange")
except NoSuchElementException: except NoSuchElementException:
logger.error("Selecting exchange impossible") logger.error("Selecting exchange impossible")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
if amount_menuentry: if amount_menuentry:
xpath_menu = '//select[@id="reserve-amount"]' xpath_menu = '//select[@id="reserve-amount"]'
@ -225,6 +247,8 @@ def withdraw(client, amount_menuentry=None):
break break
except NoSuchElementException: except NoSuchElementException:
logger.error("value '" + str(amount_value) + "' is not offered by this bank to withdraw, please adapt it") logger.error("value '" + str(amount_value) + "' is not offered by this bank to withdraw, please adapt it")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
# confirm amount # confirm amount
logger.info("About to confirm amount") logger.info("About to confirm amount")
@ -238,6 +262,8 @@ def withdraw(client, amount_menuentry=None):
button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]"))) button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[1]")))
except TimeoutException: except TimeoutException:
logger.error("Could not confirm exchange") logger.error("Could not confirm exchange")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
# This click returns the captcha page (put wait?) # This click returns the captcha page (put wait?)
logger.info("About to confirm exchange") logger.info("About to confirm exchange")
@ -247,6 +273,8 @@ def withdraw(client, amount_menuentry=None):
question = client.find_element(By.XPATH, "//span[@class='captcha-question']/div") question = client.find_element(By.XPATH, "//span[@class='captcha-question']/div")
except NoSuchElementException: except NoSuchElementException:
logger.error("Captcha page not gotten or malformed") logger.error("Captcha page not gotten or malformed")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
questionTok = question.text.split() questionTok = question.text.split()
op1 = int(questionTok[2]) op1 = int(questionTok[2])
@ -257,6 +285,8 @@ def withdraw(client, amount_menuentry=None):
form = client.find_element(By.TAG_NAME, "form") form = client.find_element(By.TAG_NAME, "form")
except NoSuchElementException: except NoSuchElementException:
logger.error("Could not submit captcha answer (therefore trigger withdrawal)") logger.error("Could not submit captcha answer (therefore trigger withdrawal)")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
form.submit() form.submit()
# check outcome # check outcome
@ -264,6 +294,8 @@ def withdraw(client, amount_menuentry=None):
client.find_element(By.CLASS_NAME, "informational-ok") client.find_element(By.CLASS_NAME, "informational-ok")
except NoSuchElementException: except NoSuchElementException:
logger.error("Withdrawal not completed") logger.error("Withdrawal not completed")
client.quit()
display.stop()
sys.exit(1) sys.exit(1)
logger.info("Withdrawal completed") logger.info("Withdrawal completed")