Polishing Selenium code, and testing bookmarked fulfillment URL.

This commit is contained in:
Marcello Stanisci 2017-02-20 14:52:08 +01:00
parent 5ad34d8dfd
commit 22018d2562
No known key found for this signature in database
GPG Key ID: 8D526861953F4C0F

View File

@ -25,8 +25,8 @@ import json
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__) 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))
display.start() #display.start()
def client_setup(args): def client_setup(args):
"""Return a dict containing the driver and the extension's id""" """Return a dict containing the driver and the extension's id"""
@ -85,7 +85,8 @@ def switch_base():
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=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.
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")
@ -97,7 +98,6 @@ def make_donation(client, amount_menuentry=None):
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"):
# 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))
logger.info("Picked donation %s" % option.text) logger.info("Picked donation %s" % option.text)
@ -122,37 +122,36 @@ def make_donation(client, amount_menuentry=None):
sys.exit(1) sys.exit(1)
confirm_pay.click() 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")
return False
return True
def buy_article(client, title, fulfillment_url=None):
"""Buy article at blog.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
def buy_article(client):
"""Buy article at blog.test.taler.net. Assume the wallet has coins"""
client.get(parse.urljoin(taler_baseurl, "shop")) 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")]][7]'))) 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, '//h3/a[@href="/essay/Foreword"]')))
# NOTE: we need to scroll the browser a few inches deeper respect logger.info("Scrolling to article position: %s", teaser.location['y'])
# to the element which is to be clicked, otherwise we hit the lang client.execute_script("window.scrollBy(0, %s)" % teaser.location['y'])
# bar at the bottom.. teaser.click()
# Unfortunately, just retrieving the element to click and click it
# did NOT work.
actions = ActionChains(client)
time.sleep(2)
logger.info("Batching:..")
logger.info("..scroll page down")
actions.move_to_element(further_teaser)
time.sleep(2)
logger.info("..focus on article")
actions.move_to_element(teaser)
time.sleep(2)
logger.info("..click on article")
actions.click(teaser)
time.sleep(2)
logger.info("Performing batched actions")
actions.perform()
except (NoSuchElementException, TimeoutException): except (NoSuchElementException, TimeoutException):
logger.error('Could not choose "Foreword" chapter on blog') logger.error('Could not choose "Foreword" chapter on blog')
sys.exit(1) sys.exit(1)
# explicit get() is needed, it hangs (sometimes) otherwise
time.sleep(1) time.sleep(1)
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']")))
@ -161,13 +160,10 @@ def buy_article(client):
logger.error('Could not confirm payment on blog') logger.error('Could not confirm payment on blog')
sys.exit(1) sys.exit(1)
confirm_pay.click() confirm_pay.click()
# check here for good elements time.sleep(3)
try: if not check_article(client, title):
client.find_element(By.XPATH, "//h1[contains(., 'Foreword')]") return False
except NoSuchElementException: return client.current_url
logger.error("Article not correctly bought")
sys.exit(1)
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 until the
@ -289,7 +285,11 @@ logger.info("Making donations..")
time.sleep(2) 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) 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("ret: '%s'" % ret)
logger.info("Test passed") logger.info("Test passed")
client.close() client.close()
sys.exit(0) sys.exit(0)