Commit d6304178 by Csók Tamás

selenium: corrections and stability improvements

parent 01890687
...@@ -50,7 +50,7 @@ class SeleniumConfig: ...@@ -50,7 +50,7 @@ class SeleniumConfig:
client_name = 'test_%s' % random_pass client_name = 'test_%s' % random_pass
# Which webpage should selenium use (localhost is recommended) # Which webpage should selenium use (localhost is recommended)
host = 'https:127.0.0.1' host = 'https://127.0.0.1'
# In default the tests create a new user then delete it afteword # In default the tests create a new user then delete it afteword
# Disable this if selenium cannot acces the database # Disable this if selenium cannot acces the database
create_user = True create_user = True
......
...@@ -23,7 +23,8 @@ import re ...@@ -23,7 +23,8 @@ import re
import time import time
import urlparse import urlparse
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import (
NoSuchElementException, StaleElementReferenceException)
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select from selenium.webdriver.support.select import Select
...@@ -112,7 +113,8 @@ class CircleSeleniumMixin(object): ...@@ -112,7 +113,8 @@ class CircleSeleniumMixin(object):
self.wait_and_accept_operation() self.wait_and_accept_operation()
recent_deploy = self.recently(self.get_timeline_elements( recent_deploy = self.recently(self.get_timeline_elements(
"vm.Instance.deploy")) "vm.Instance.deploy"))
if not self.check_operation_result(recent_deploy): if not self.check_operation_result(
recent_deploy, "a[href*='#activity']"):
logger.warning("Selenium cannot deploy the " logger.warning("Selenium cannot deploy the "
"chosen template virtual machine") "chosen template virtual machine")
raise Exception('Cannot deploy the virtual machine') raise Exception('Cannot deploy the virtual machine')
...@@ -122,9 +124,13 @@ class CircleSeleniumMixin(object): ...@@ -122,9 +124,13 @@ class CircleSeleniumMixin(object):
By.CSS_SELECTOR, By.CSS_SELECTOR,
"a[href$='/op/shut_off/']")))) "a[href$='/op/shut_off/']"))))
self.wait_and_accept_operation() self.wait_and_accept_operation()
WebDriverWait(self.driver, self.conf.wait_max_sec).until(
ec.element_to_be_clickable((
By.CSS_SELECTOR, "a[href$='/op/deploy/']")))
recent_shut_off = self.recently(self.get_timeline_elements( recent_shut_off = self.recently(self.get_timeline_elements(
"vm.Instance.shut_off")) "vm.Instance.shut_off"))
if not self.check_operation_result(recent_shut_off): if not self.check_operation_result(
recent_shut_off, "a[href*='#activity']"):
logger.warning("Selenium cannot shut off the " logger.warning("Selenium cannot shut off the "
"chosen template virtual machine") "chosen template virtual machine")
raise Exception('Cannot shut off the virtual machine') raise Exception('Cannot shut off the virtual machine')
...@@ -234,7 +240,8 @@ class CircleSeleniumMixin(object): ...@@ -234,7 +240,8 @@ class CircleSeleniumMixin(object):
raise Exception( raise Exception(
'Cannot find the template\'s id') 'Cannot find the template\'s id')
def check_operation_result(self, operation_id, restore=True): def check_operation_result(self, operation_id, restore_selector=None,
restore=True):
""" """
Returns wheter the operation_id result is success (returns: boolean) Returns wheter the operation_id result is success (returns: boolean)
""" """
...@@ -254,19 +261,36 @@ class CircleSeleniumMixin(object): ...@@ -254,19 +261,36 @@ class CircleSeleniumMixin(object):
result = WebDriverWait(self.driver, self.conf.wait_max_sec).until( result = WebDriverWait(self.driver, self.conf.wait_max_sec).until(
ec.visibility_of_element_located(( ec.visibility_of_element_located((
By.ID, "activity_status"))) By.ID, "activity_status")))
logger.warning("%(id)s result text is '%(result)s'" % { logger.warning("%(id)s's result is '%(result)s'" % {
'id': operation_id, 'id': operation_id,
'result': result.text}) 'result': result.text})
if (result.text == "success"): if (result.text == "success"):
out = True out = True
elif (result.text == "wait"): elif (result.text == "wait"):
time.sleep(2) time.sleep(2)
out = self.check_operation_result(operation_id, False) out = self.check_operation_result(
operation_id=operation_id, restore=False)
else: else:
try:
result_text = WebDriverWait(
self.driver, self.conf.wait_max_sec).until(
ec.visibility_of_element_located((
By.ID, "activity_result_text")))
logger.warning(
"%(id)s's result text is: '%(result_text)s'" % {
'id': operation_id,
'result_text': result_text.text})
except:
logger.warning("Cannot read %(id)s's result text" % {
'id': operation_id})
out = False out = False
if restore: if restore:
logger.warning("Restoring to %s url" % url_save) logger.warning("Restoring to %s url" % url_save)
self.driver.get(url_save) self.driver.get(url_save)
if restore_selector is not None and restore_selector:
WebDriverWait(self.driver, self.conf.wait_max_sec).until(
ec.visibility_of_element_located((
By.CSS_SELECTOR, restore_selector)))
return out return out
except: except:
logger.exception("Selenium cannot check the" logger.exception("Selenium cannot check the"
...@@ -279,29 +303,48 @@ class CircleSeleniumMixin(object): ...@@ -279,29 +303,48 @@ class CircleSeleniumMixin(object):
second = self.conf.recently_sec second = self.conf.recently_sec
try: try:
if isinstance(timeline_dict, dict): if isinstance(timeline_dict, dict):
recent = None
for key, value in timeline_dict.iteritems(): for key, value in timeline_dict.iteritems():
time = datetime.strptime(key, '%Y-%m-%d %H:%M') if recent is None or int(key) > int(recent):
delta = datetime.now() - time recent = key
if len(timeline_dict) > 1:
logger.warning(
"Searching for most recent activity"
" from the received %(count)s pieces" % {
'count': len(timeline_dict)})
logger.warning("Found at %(id)s @ %(time)s" % {
'id': timeline_dict[recent],
'time': datetime.fromtimestamp(
int(recent)).strftime('%Y-%m-%d %H:%M:%S')})
logger.warning(
"Checking wheter %(id)s started in the"
" recent %(second)s seconds" % {
'id': timeline_dict[recent],
'second': second})
delta = datetime.now() - datetime.fromtimestamp(int(recent))
if delta.total_seconds() <= second: if delta.total_seconds() <= second:
return value return timeline_dict[recent]
except: except:
logger.exception("Selenium cannot filter" logger.exception("Selenium cannot filter timeline "
" timeline activities to recent") "activities to find most recent")
raise Exception( raise Exception(
'Cannot filter timeline activities to recent') 'Cannot filter timeline activities to find most recent')
def get_timeline_elements(self, code=None): def get_timeline_elements(self, code=None):
try: try:
if code is None: if code is None:
css_activity_selector = "div[data-activity-code]" css_activity_selector = "div[data-activity-code]"
code = "all activity" code_text = "all activity"
else: else:
code_text = code
css_activity_selector = ("div[data-activity-code=" css_activity_selector = ("div[data-activity-code="
"'%(code)s']" % { "'%(code)s']" % {
'code': code}) 'code': code_text})
WebDriverWait(self.driver, self.conf.wait_max_sec).until( self.click_on_link(WebDriverWait(
self.driver, self.conf.wait_max_sec).until(
ec.element_to_be_clickable(( ec.element_to_be_clickable((
By.CSS_SELECTOR, "a[href*='#activity']"))).click() By.CSS_SELECTOR, "a[href*='#activity']"))))
try:
activity_dict = {} activity_dict = {}
timeline = WebDriverWait( timeline = WebDriverWait(
self.driver, self.conf.wait_max_sec).until( self.driver, self.conf.wait_max_sec).until(
...@@ -309,16 +352,24 @@ class CircleSeleniumMixin(object): ...@@ -309,16 +352,24 @@ class CircleSeleniumMixin(object):
By.ID, "activity-timeline"))) By.ID, "activity-timeline")))
searched_activity = timeline.find_elements_by_css_selector( searched_activity = timeline.find_elements_by_css_selector(
css_activity_selector) css_activity_selector)
logger.warning("Found activity list for %s:" % code) logger.warning("Found activity list for %s:" % code_text)
for activity in searched_activity: for activity in searched_activity:
activity_id = activity.get_attribute('data-activity-id') activity_id = activity.get_attribute('data-activity-id')
activity_text = activity.text key = activity.get_attribute('timestamp')
key = re.search(
r'\d+-\d+-\d+ \d+:\d+,', activity_text).group()[:-1]
logger.warning("%(id)s @ %(activity)s" % { logger.warning("%(id)s @ %(activity)s" % {
'id': activity_id, 'id': activity_id,
'activity': key}) 'activity': datetime.fromtimestamp(
int(key)).strftime('%Y-%m-%d %H:%M:%S')})
activity_dict[key] = activity_id activity_dict[key] = activity_id
except StaleElementReferenceException:
logger.warning('Timeline changed while processing it')
return self.get_timeline_elements(code)
except:
logger.exception('Selenium cannot get timeline elemets')
raise Exception('Cannot get timeline elements')
if len(activity_dict) == 0:
logger.warning('Found activity list is empty')
raise Exception('Selenium did not found any activity')
return activity_dict return activity_dict
except: except:
logger.exception('Selenium cannot find the searched activity') logger.exception('Selenium cannot find the searched activity')
...@@ -355,6 +406,10 @@ class CircleSeleniumMixin(object): ...@@ -355,6 +406,10 @@ class CircleSeleniumMixin(object):
WebDriverWait(self.driver, self.conf.wait_max_sec).until( WebDriverWait(self.driver, self.conf.wait_max_sec).until(
ec.visibility_of_element_located(( ec.visibility_of_element_located((
By.ID, "_activity"))) By.ID, "_activity")))
self.click_on_link(WebDriverWait(
self.driver, self.conf.wait_max_sec).until(
ec.element_to_be_clickable((
By.CSS_SELECTOR, "a[href*='#activity']"))))
recent_remove_disk = self.recently( recent_remove_disk = self.recently(
self.get_timeline_elements( self.get_timeline_elements(
"vm.Instance.remove_disk")) "vm.Instance.remove_disk"))
...@@ -401,7 +456,8 @@ class CircleSeleniumMixin(object): ...@@ -401,7 +456,8 @@ class CircleSeleniumMixin(object):
vm_list[choice].click() vm_list[choice].click()
WebDriverWait(self.driver, self.conf.wait_max_sec).until( WebDriverWait(self.driver, self.conf.wait_max_sec).until(
ec.element_to_be_clickable(( ec.element_to_be_clickable((
By.CLASS_NAME, 'vm-create-start'))).click() By.CSS_SELECTOR,
"button[class*='vm-create-start']"))).click()
WebDriverWait(self.driver, self.conf.wait_max_sec).until( WebDriverWait(self.driver, self.conf.wait_max_sec).until(
ec.visibility_of_element_located(( ec.visibility_of_element_located((
By.CLASS_NAME, 'alert-success'))) By.CLASS_NAME, 'alert-success')))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment