Optimizing/fixing selenium tests.

This commit is contained in:
Marcello Stanisci 2017-02-15 17:07:45 +01:00
parent 2ca731a6b8
commit fdb272d505
No known key found for this signature in database
GPG Key ID: 8D526861953F4C0F

View File

@ -44,8 +44,9 @@ def client_setup(args):
else: else:
client = webdriver.Chrome(desired_capabilities=cap) client = webdriver.Chrome(desired_capabilities=cap)
client.get('https://taler.net') client.get('https://taler.net')
listener = """\ listener = """\
document.addEventListener('taler-id', function(evt){ document.addEventListener('taler-query-id-result', function(evt){
var html = document.getElementsByTagName('html')[0]; var html = document.getElementsByTagName('html')[0];
html.setAttribute('data-taler-wallet-id', evt.detail.id); html.setAttribute('data-taler-wallet-id', evt.detail.id);
}); });
@ -55,7 +56,9 @@ def client_setup(args):
""" """
client.execute_script(listener) client.execute_script(listener)
html = client.find_element(By.TAG_NAME, "html") html = client.find_element(By.TAG_NAME, "html")
return {'client': client, 'ext_id': html.get_attribute('data-taler-wallet-id')} 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): def is_error(client):
"""Return True in case of errors in the browser, False otherwise""" """Return True in case of errors in the browser, False otherwise"""
@ -80,7 +83,7 @@ def switch_base():
def make_donation(client, amount_menuentry=None): def make_donation(client, amount_menuentry=None):
"""Make donation at shop.test.taler.net. Assume the wallet has coins""" """Make donation at shop.test.taler.net. Assume the wallet has coins"""
client.get(parse.urljoin(taler_baseurl, "shop")) 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:
@ -103,15 +106,12 @@ def make_donation(client, amount_menuentry=None):
form.submit() # amount and receiver chosen form.submit() # amount and receiver chosen
wait = WebDriverWait(client, 10) wait = WebDriverWait(client, 10)
try: try:
confirm_taler = wait.until(EC.element_to_be_clickable((By.XPATH, "//form//input[@type='button']"))) confirm_taler = wait.until(EC.element_to_be_clickable((By.ID, "select-payment-method")))
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')
sys.exit(1) sys.exit(1)
confirm_taler.click() # Taler as payment option chosen confirm_taler.click() # Taler as payment option chosen
# explicit get() is needed, it hangs (sometimes) otherwise
time.sleep(3) #FIXME use better way to 'Ok' Taler at checkout page
client.get(client.current_url)
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:
@ -122,7 +122,7 @@ def make_donation(client, amount_menuentry=None):
def buy_article(client): def buy_article(client):
"""Buy article at blog.test.taler.net. Assume the wallet has coins""" """Buy article at blog.test.taler.net. Assume the wallet has coins"""
client.get(parse.urljoin(taler_baseurl, "blog")) client.get(parse.urljoin(taler_baseurl, "shop"))
wait = WebDriverWait(client, 10) wait = WebDriverWait(client, 10)
try: try:
further_teaser = wait.until(EC.element_to_be_clickable((By.XPATH, '//h3[a[starts-with(@href, "/essay")]][4]'))) further_teaser = wait.until(EC.element_to_be_clickable((By.XPATH, '//h3[a[starts-with(@href, "/essay")]][4]')))
@ -150,7 +150,7 @@ def buy_article(client):
confirm_pay.click() confirm_pay.click()
# check here for good elements # check here for good elements
try: try:
client.find_element(By.XPATH, "//h1[@class='book-title']") client.find_element(By.XPATH, "//h1[contains(., 'Foreword')]")
except NoSuchElementException: except NoSuchElementException:
logger.error("Article not correctly bought") logger.error("Article not correctly bought")
sys.exit(1) sys.exit(1)
@ -189,7 +189,7 @@ def register(client):
# 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
if not is_error(client): if not is_error(client):
logger.info('correctly registered at bank') logger.info('Correctly registered at bank')
else: else:
logger.error('User not registered at bank') logger.error('User not registered at bank')
@ -197,6 +197,8 @@ def register(client):
def withdraw(client, amount_menuentry=None): def withdraw(client, amount_menuentry=None):
"""Register and withdraw (1) KUDOS for a fresh user""" """Register and withdraw (1) KUDOS for a fresh user"""
register(client) register(client)
logger.info("Withdrawing..")
wait = WebDriverWait(client, 10)
# trigger withdrawal button # trigger withdrawal button
try: try:
button = client.find_element(By.ID, "select-exchange") button = client.find_element(By.ID, "select-exchange")
@ -206,11 +208,8 @@ def withdraw(client, amount_menuentry=None):
if amount_menuentry: if amount_menuentry:
xpath_menu = '//select[@id="reserve-amount"]' xpath_menu = '//select[@id="reserve-amount"]'
try: try:
# Tried the more suitable select_by_visible_text(),
# did not work.
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"):
# Tried option.text, did not work.
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))
option.click() option.click()
@ -218,17 +217,21 @@ def withdraw(client, amount_menuentry=None):
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")
sys.exit(1) sys.exit(1)
# confirm amount
logger.info("About to confirm amount")
button.click() button.click()
location = client.execute_script("return document.location.href") logger.info("Exchange confirmation refreshed")
client.get(location) # Confirm exchange (in-wallet page)
# Confirm xchg
wait = WebDriverWait(client, 10)
try: try:
logger.info("Polling for the button")
mybutton = client.find_element(By.XPATH, "//button[1]")
logger.info("Found button '%s'" % mybutton.text)
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 (therefore provide withdrawal needed data)") logger.error("Could not confirm exchange")
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")
button.click() button.click()
try: try:
answer = client.find_element(By.XPATH, "//input[@name='pin_0']") answer = client.find_element(By.XPATH, "//input[@name='pin_0']")
@ -267,12 +270,10 @@ ret = client_setup(args)
logger.info("Creating the browser driver..") logger.info("Creating the browser driver..")
client = ret['client'] client = ret['client']
client.implicitly_wait(10) client.implicitly_wait(10)
logger.info("Withdrawing..")
withdraw(client, "10.00 PUDOS") withdraw(client, "10.00 PUDOS")
# switch_base() # inducing error
logger.info("Making donations..") logger.info("Making donations..")
# FIXME: wait for coins via a more suitable way # FIXME: wait for coins more appropriately
time.sleep(3) time.sleep(2)
make_donation(client, "1.0 PUDOS") make_donation(client, "1.0 PUDOS")
logger.info("Buying article..") logger.info("Buying article..")
buy_article(client) buy_article(client)