finishing to fix returned values from Selenium

routines, plus general simplification of code.
This commit is contained in:
Marcello Stanisci 2017-11-02 13:47:32 +01:00
parent a55daa797e
commit 38185fb187
No known key found for this signature in database
GPG Key ID: 8D526861953F4C0F

View File

@ -27,12 +27,15 @@ logger = logging.getLogger(__name__)
taler_baseurl = os.environ.get('TALER_BASEURL', 'https://test.taler.net/') taler_baseurl = os.environ.get('TALER_BASEURL', 'https://test.taler.net/')
display = Display(visible=0, size=(1024, 768)) display = Display(visible=0, size=(1024, 768))
# Kill driver and display def kill_display():
def abort(client):
client.quit()
if hasattr(display, "old_display_var"): if hasattr(display, "old_display_var"):
print("Kill display")
display.stop() display.stop()
# All the operations we need upon errors.
def abort(client):
print_log(client)
client.quit()
kill_display()
sys.exit(1) sys.exit(1)
@ -55,9 +58,7 @@ def client_setup(args):
if args.remote: if args.remote:
client = webdriver.Remote(desired_capabilities=cap, command_executor=args.remote) client = webdriver.Remote(desired_capabilities=cap, command_executor=args.remote)
else: else:
logger.info("About to get chromed")
client = webdriver.Chrome(desired_capabilities=cap) client = webdriver.Chrome(desired_capabilities=cap)
logger.info("Got chromed")
client.get('https://taler.net') client.get('https://taler.net')
listener = """\ listener = """\
@ -75,15 +76,6 @@ def client_setup(args):
logger.info("Extension ID: %s" % str(ext_id)) logger.info("Extension ID: %s" % str(ext_id))
return {'client': client, 'ext_id': 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 print_log(client): def print_log(client):
print("--- Dumping browser log: ---") print("--- Dumping browser log: ---")
for log in client.get_log("browser"): for log in client.get_log("browser"):
@ -92,9 +84,10 @@ def print_log(client):
def switch_base(): def switch_base():
"""If 'test' is in TALER_BASEURL, then make it be 'demo', and viceversa. """If 'test' is in TALER_BASEURL, then make it be 'demo',
Used to trig currency mismatch errors. It assumes that the https://{test,demo}.taler.net and viceversa. Used to trigger currency mismatch errors.
layout for URLs is used""" It assumes that the https://{test,demo}.taler.net layout
for URLs is used"""
global taler_baseurl global taler_baseurl
url = parse.urlparse(taler_baseurl) url = parse.urlparse(taler_baseurl)
if url[1] == 'test.taler.net': if url[1] == 'test.taler.net':
@ -102,60 +95,61 @@ def switch_base():
if url[1] == 'demo.taler.net': if url[1] == 'demo.taler.net':
taler_baseurl = "https://test.taler.net" taler_baseurl = "https://test.taler.net"
def make_donation(client, amount_menuentry=None): def make_donation(client, amount_menuentry):
"""Make donation at donations.test.taler.net. Assume the wallet has coins. """Make donation at donations.test.taler.net. Assume
Just donate to the default receiver""" the wallet has coins. Just donate to the default receiver"""
client.get(parse.urljoin(taler_baseurl, "donations")) client.get(parse.urljoin(taler_baseurl, "donations"))
try: try:
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')
abort(client) return False
if amount_menuentry:
xpath_menu = '//select[@id="taler-donation"]' xpath_menu = '//select[@id="taler-donation"]'
try: try:
dropdown = client.find_element(By.XPATH, xpath_menu) dropdown = 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 = WebDriverWait(client, 10).until(EC.visibility_of(option)) option = WebDriverWait(client, 10).until(EC.visibility_of(option))
logger.info("Picked donation %s" % option.text)
option.click() option.click()
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("amount '" + str(amount_value) + "\
abort(client) ' is not offered by this shop to donate")
form.submit() # amount and receiver chosen return False
form.submit()
wait = WebDriverWait(client, 10) wait = WebDriverWait(client, 10)
try: try:
confirm_taler = wait.until(EC.element_to_be_clickable((By.ID, "select-payment-method"))) 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: except NoSuchElementException:
logger.error('Could not trigger contract on donation shop') logger.error('Could not trigger contract on donation shop')
abort(client) return False
confirm_taler.click() # Taler as payment option chosen confirm_taler.click()
try: try:
confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, confirm_pay = 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')
abort(client) return False
confirm_pay.click() confirm_pay.click()
return True
# 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(client, title):
try: try:
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 '%s' not shown on this (%s) page" % (title, client.current_url))
return False return False
return True return True
def buy_article(client, title, fulfillment_url=None): def buy_article(client, title, fulfillment_url=None):
"""Buy article at shop.test.taler.net. Assume the wallet has coins. """Buy article at shop.test.taler.net. Assume the wallet
Return False if some error occurs, the fulfillment URL otherwise""" has coins. Return False if some error occurs, the fulfillment
URL otherwise"""
if fulfillment_url: if fulfillment_url:
client.get(fulfillment_url) client.get(fulfillment_url)
@ -167,11 +161,10 @@ def buy_article(client, title, fulfillment_url=None):
wait = WebDriverWait(client, 20) wait = WebDriverWait(client, 20)
try: try:
teaser = wait.until(EC.element_to_be_clickable((By.XPATH, "//h3/a[@href=\"/essay/%s\"]" % title))) teaser = wait.until(EC.element_to_be_clickable((By.XPATH,
logger.info("Scrolling to article position: %s", teaser.location['y']) "//h3/a[@href=\"/essay/%s\"]" % title)))
client.execute_script("window.scrollBy(30, %s)" % teaser.location['y']) client.execute_script("window.scrollBy(30, %s)" % teaser.location['y'])
teaser.click() teaser.click()
# The article is not displayed in the home page.
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
@ -179,11 +172,8 @@ def buy_article(client, title, fulfillment_url=None):
try: try:
confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH, confirm_pay = wait.until(EC.element_to_be_clickable((By.XPATH,
"//button[@class='pure-button button-success']"))) "//button[@class='pure-button button-success']")))
logger.info("Pay button turned clickable")
# We clicked on the article but (for some reason) we can't
# confirm the contract.
except TimeoutException: except TimeoutException:
logger.error('Could not confirm payment 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)
@ -192,11 +182,12 @@ def buy_article(client, title, fulfillment_url=None):
return client.current_url return client.current_url
def register(client): def register(client):
"""Register a new user to the bank delaying its execution until the """Register a new user to the bank delaying its execution
profile page is shown""" until the profile page is shown"""
client.get(parse.urljoin(taler_baseurl, "bank")) client.get(parse.urljoin(taler_baseurl, "bank"))
try: try:
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")
return False return False
@ -212,10 +203,9 @@ def register(client):
form.username.value = '%s'; form.username.value = '%s';
form.password.value = 'test'; form.password.value = 'test';
form.submit(); form.submit();
""" % str(int(time.time())) # need fresh username """ % str(int(time.time()))
client.execute_script(register) client.execute_script(register)
# need implicit wait to be set up
try: try:
button = client.find_element(By.ID, "select-exchange") button = client.find_element(By.ID, "select-exchange")
except NoSuchElementException: except NoSuchElementException:
@ -226,7 +216,6 @@ def register(client):
def withdraw(client, amount_menuentry): def withdraw(client, amount_menuentry):
"""Register and withdraw (1) KUDOS for a fresh user""" """Register and withdraw (1) KUDOS for a fresh user"""
logger.info("Withdrawing..")
wait = WebDriverWait(client, 10) wait = WebDriverWait(client, 10)
# trigger withdrawal button # trigger withdrawal button
try: try:
@ -243,21 +232,18 @@ def withdraw(client, amount_menuentry):
option.click() option.click()
break break
except NoSuchElementException: except NoSuchElementException:
logger.error("amount '" + str(amount_value) + "' is not offered by this bank to withdraw") logger.error("amount '" + str(amount_value) + "' \
is not offered by this bank to withdraw")
return False return False
# confirm amount # confirm amount
logger.info("About to submit amount")
button.click() button.click()
logger.info("Exchange confirmation refreshed")
# Confirm exchange (in-wallet page) # Confirm exchange (in-wallet page)
try: try:
logger.info("Polling for the button")
accept_exchange = wait.until(EC.element_to_be_clickable((By.XPATH, accept_exchange = 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
logger.info("About to confirm exchange")
accept_exchange.click() accept_exchange.click()
try: try:
answer = client.find_element(By.XPATH, "//input[@name='pin_0']") answer = client.find_element(By.XPATH, "//input[@name='pin_0']")
@ -282,7 +268,6 @@ def withdraw(client, amount_menuentry):
except NoSuchElementException: except NoSuchElementException:
logger.error("Withdrawal not completed") logger.error("Withdrawal not completed")
return False return False
logger.info("Withdrawal completed")
return True return True
@ -300,31 +285,29 @@ parser.add_argument('--with-head',
action="store_true", dest="withhead") action="store_true", dest="withhead")
args = parser.parse_args() args = parser.parse_args()
logger.info("testing against " + taler_baseurl)
logger.info("Getting extension's ID..")
client = client_setup(args)['client'] client = client_setup(args)['client']
logger.info("Creating the browser driver..")
client.implicitly_wait(10) client.implicitly_wait(10)
if not register(client): if not register(client):
print_log(client)
abort(client) abort(client)
if not withdraw(client, "10.00 TESTKUDOS"): if not withdraw(client, "10.00 TESTKUDOS"):
print_log(client)
abort(client) abort(client)
time.sleep(2)
logger.info("Buying article..")
fulfillment_url_25 = buy_article(client, "25._The_Danger_of_Software_Patents") fulfillment_url_25 = buy_article(client, "25._The_Danger_of_Software_Patents")
fulfillment_url_41 = buy_article(client, "41._Avoiding_Ruinous_Compromises")
if not fulfillment_url_25:
abort(client)
client.delete_all_cookies() client.delete_all_cookies()
ret = buy_article(client, "25._The_Danger_of_Software_Patents", fulfillment_url_25)
logger.info("Donating..") if not buy_article(client, "25._The_Danger_of_Software_Patents", fulfillment_url_25):
make_donation(client, "1.0 TESTKUDOS") logger.error("Could not replay payment")
logger.info("Payment replayed: '%s'" % ret) abort(client)
logger.info("Test passed")
if not make_donation(client, "1.0 TESTKUDOS"):
abort(client)
client.quit() client.quit()
if hasattr(display, "old_display_var"): kill_display()
display.stop()
sys.exit(0) sys.exit(0)