def run(self, webdriver, params): """ Wait for things :param webdriver: :param params: :return: """ if isinstance(params, dict): if 'visibility_of' in params: """ Wait for the visibility of an element """ if 'element' in params['visibility_of']: element = params['visibility_of']['element'] else: raise DysonError("Key \"element\" is required") timeout = int(constants.DEFAULT_TIMEOUT) # seconds if 'timeout' in params['visibility_of']: timeout = int(params['visibility_of']['timeout']) return self._wait_for(element, expected_conditions.visibility_of_element_located, timeout, webdriver) if 'invisibility_of' in params: """ Wait for the invisibility of an element """ if 'element' in params['invisibility_of']: element = params['invisibility_of']['element'] else: raise DysonError("Key \"element\" is required") timeout = int(constants.DEFAULT_TIMEOUT) # seconds if 'timeout' in params['invisibility_of']: timeout = int(params['invisibility_of']['timeout']) return self._wait_for(element, expected_conditions.invisibility_of_element_located, timeout, webdriver) if 'presence_of' in params: """ Wait for the presence of an element """ if 'element' in params['presence_of']: element = params['presence_of']['element'] else: raise DysonError("Key \"element\" is required") timeout = int(constants.DEFAULT_TIMEOUT) # seconds if 'timeout' in params['presence_of']: timeout = int(params['presence_of']['timeout']) return self._wait_for(element, expected_conditions.presence_of_element_located, timeout, webdriver) if 'title_to_be' in params: """ Wait for the title to be something """ title = params['title_to_be'] timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['title_to_be']: timeout = int(params['title_to_be']['timeout']) return WebDriverWait(webdriver, timeout).until( expected_conditions.title_is(title) ) if 'title_to_contain' in params: """ Wait for the title to contain """ title = params['title_to_contain'] timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['title_to_contain']: timeout = int(params['title_to_contain']['timeout']) return WebDriverWait(webdriver, timeout).until( expected_conditions.title_contains(title) ) if 'alert' in params: """ Wait for an alert to be present """ timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['alert']: timeout = int(params['alert']['timeout']) return WebDriverWait(webdriver, timeout).until( expected_conditions.alert_is_present() ) if 'text_to_be_present' in params: """ Wait for text to be present in an element """ timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['text_to_be_present']: timeout = int(params['text_to_be_present']['timeout']) if 'in_element' in params['text_to_be_present']: in_element = params['text_to_be_present']['in_element'] if 'text' in params['text_to_be_present']: text = in_element['text'] strategy, selector = translate_selector_to_by(in_element) return WebDriverWait(webdriver, timeout).until( expected_conditions.text_to_be_present_in_element( (strategy, selector), text ) ) else: raise DysonError("Key \"text\" is required") else: raise DysonError("Key \"in_element\" is required") if 'clickable' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['clickable']: timeout = int(params['clickable']['timeout']) return self._wait_for(params['clickable']['element'], expected_conditions.element_to_be_clickable, timeout, webdriver) if 'value_to_be' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['value_to_be']: timeout = int(params['value_to_be']['timeout']) if 'in_element' in params['value_to_be']: in_element = params['value_to_be']['in_element'] if 'value' in params['value_to_be']: value = in_element['value'] strategy, selector = translate_selector_to_by(in_element) return WebDriverWait(webdriver, timeout).until( expected_conditions.text_to_be_present_in_element_value( (strategy, selector), value ) ) else: raise DysonError("Key \"text\" is required") else: raise DysonError("Key \"in_element\" is required") if 'staleness_of' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['staleness_of']: timeout = int(params['staleness_of']['timeout']) if 'element' in params['staleness_of']: element = params['staleness_of']['element'] return self._wait_for(element, expected_conditions.staleness_of, timeout, webdriver) else: raise DysonError("Key \"element\" is required") if 'presence_of_all' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['presence_of_all']: timeout = int(params['presence_of_all']['timeout']) if 'elements' in params['presence_of_all']: elements = params['presence_of_all'] return self._wait_for(elements, expected_conditions.presence_of_all_elements_located, timeout, webdriver) else: raise DysonError("Key \"elements\" is required") if 'element_to_be_selected' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['element_to_be_selected']: timeout = int(params['element_to_be_selected']['timeout']) if 'element' in params['element_to_be_selected']: element = params['element_to_be_selected']['element'] return self._wait_for(element, expected_conditions.element_located_to_be_selected, timeout, webdriver) else: raise DysonError("Key \"element\" is required") if 'selection_state_to_be' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['selection_state_to_be']: timeout = int(params['selection_state_to_be']['timeout']) if 'in_element' in params['selection_state_to_be']: in_element = params['selection_state_to_be']['in_element'] if 'state' in params['selection_state_to_be']: state = to_boolean(params['selection_state_to_be']['state']) strategy, selector = translate_selector_to_by(in_element) return WebDriverWait(webdriver, timeout).until( expected_conditions.element_located_selection_state_to_be( (strategy, selector), state ) ) else: raise DysonError("Key \"state\" is required") else: raise DysonError("Key \"in_element\" is required") if 'frame_and_switch' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['frame_and_switch']: timeout = int(params['frame_and_switch']['timeout']) if 'frame' in params['frame_and_switch']: frame = params['frame_and_switch']['frame'] return self._wait_for(frame, expected_conditions.frame_to_be_available_and_switch_to_it, timeout, webdriver) else: raise DysonError("Key \"frame\" is required") else: self.fail("Invalid type. You must specify a valid action")
def fail(self, msg): print(msg, file=sys.stderr) if not to_boolean(constants.DEFAULT_SELENIUM_PERSIST): exit(2)
def run(self, webdriver, params): """ Wait for things :param webdriver: :param params: :return: """ if isinstance(params, dict): if 'visibility_of' in params: """ Wait for the visibility of an element """ if 'element' in params['visibility_of']: element = params['visibility_of']['element'] else: raise DysonError("Key \"element\" is required") timeout = int(constants.DEFAULT_TIMEOUT) # seconds if 'timeout' in params['visibility_of']: timeout = int(params['visibility_of']['timeout']) return self._wait_for( element, expected_conditions.visibility_of_element_located, timeout, webdriver) if 'invisibility_of' in params: """ Wait for the invisibility of an element """ if 'element' in params['invisibility_of']: element = params['invisibility_of']['element'] else: raise DysonError("Key \"element\" is required") timeout = int(constants.DEFAULT_TIMEOUT) # seconds if 'timeout' in params['invisibility_of']: timeout = int(params['invisibility_of']['timeout']) return self._wait_for( element, expected_conditions.invisibility_of_element_located, timeout, webdriver) if 'presence_of' in params: """ Wait for the presence of an element """ if 'element' in params['presence_of']: element = params['presence_of']['element'] else: raise DysonError("Key \"element\" is required") timeout = int(constants.DEFAULT_TIMEOUT) # seconds if 'timeout' in params['presence_of']: timeout = int(params['presence_of']['timeout']) return self._wait_for( element, expected_conditions.presence_of_element_located, timeout, webdriver) if 'title_to_be' in params: """ Wait for the title to be something """ title = params['title_to_be'] timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['title_to_be']: timeout = int(params['title_to_be']['timeout']) return WebDriverWait(webdriver, timeout).until( expected_conditions.title_is(title)) if 'title_to_contain' in params: """ Wait for the title to contain """ title = params['title_to_contain'] timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['title_to_contain']: timeout = int(params['title_to_contain']['timeout']) return WebDriverWait(webdriver, timeout).until( expected_conditions.title_contains(title)) if 'alert' in params: """ Wait for an alert to be present """ timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['alert']: timeout = int(params['alert']['timeout']) return WebDriverWait(webdriver, timeout).until( expected_conditions.alert_is_present()) if 'text_to_be_present' in params: """ Wait for text to be present in an element """ timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['text_to_be_present']: timeout = int(params['text_to_be_present']['timeout']) if 'in_element' in params['text_to_be_present']: in_element = params['text_to_be_present']['in_element'] if 'text' in params['text_to_be_present']: text = in_element['text'] strategy, selector = translate_selector_to_by( in_element) return WebDriverWait(webdriver, timeout).until( expected_conditions.text_to_be_present_in_element( (strategy, selector), text)) else: raise DysonError("Key \"text\" is required") else: raise DysonError("Key \"in_element\" is required") if 'clickable' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['clickable']: timeout = int(params['clickable']['timeout']) return self._wait_for( params['clickable']['element'], expected_conditions.element_to_be_clickable, timeout, webdriver) if 'value_to_be' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['value_to_be']: timeout = int(params['value_to_be']['timeout']) if 'in_element' in params['value_to_be']: in_element = params['value_to_be']['in_element'] if 'value' in params['value_to_be']: value = in_element['value'] strategy, selector = translate_selector_to_by( in_element) return WebDriverWait(webdriver, timeout).until( expected_conditions. text_to_be_present_in_element_value( (strategy, selector), value)) else: raise DysonError("Key \"text\" is required") else: raise DysonError("Key \"in_element\" is required") if 'staleness_of' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['staleness_of']: timeout = int(params['staleness_of']['timeout']) if 'element' in params['staleness_of']: element = params['staleness_of']['element'] return self._wait_for(element, expected_conditions.staleness_of, timeout, webdriver) else: raise DysonError("Key \"element\" is required") if 'presence_of_all' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['presence_of_all']: timeout = int(params['presence_of_all']['timeout']) if 'elements' in params['presence_of_all']: elements = params['presence_of_all'] return self._wait_for( elements, expected_conditions.presence_of_all_elements_located, timeout, webdriver) else: raise DysonError("Key \"elements\" is required") if 'element_to_be_selected' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['element_to_be_selected']: timeout = int(params['element_to_be_selected']['timeout']) if 'element' in params['element_to_be_selected']: element = params['element_to_be_selected']['element'] return self._wait_for( element, expected_conditions.element_located_to_be_selected, timeout, webdriver) else: raise DysonError("Key \"element\" is required") if 'selection_state_to_be' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['selection_state_to_be']: timeout = int(params['selection_state_to_be']['timeout']) if 'in_element' in params['selection_state_to_be']: in_element = params['selection_state_to_be']['in_element'] if 'state' in params['selection_state_to_be']: state = to_boolean( params['selection_state_to_be']['state']) strategy, selector = translate_selector_to_by( in_element) return WebDriverWait(webdriver, timeout).until( expected_conditions. element_located_selection_state_to_be( (strategy, selector), state)) else: raise DysonError("Key \"state\" is required") else: raise DysonError("Key \"in_element\" is required") if 'frame_and_switch' in params: timeout = int(constants.DEFAULT_TIMEOUT) if 'timeout' in params['frame_and_switch']: timeout = int(params['frame_and_switch']['timeout']) if 'frame' in params['frame_and_switch']: frame = params['frame_and_switch']['frame'] return self._wait_for( frame, expected_conditions. frame_to_be_available_and_switch_to_it, timeout, webdriver) else: raise DysonError("Key \"frame\" is required") else: self.fail("Invalid type. You must specify a valid action")
def run(self, webdriver, params): """ Validate things :param webdriver: :param params: allowed validations: - title - is - is_not - present - not_present - text_of - is - is_not - value_of - is - is_not - is_checked - is_unchecked :return: """ if params and isinstance(params, dict): if 'title' in params: """ Validate the title_shouldbe of the page """ actual_title = webdriver.title title_shouldbe = params['title'] if 'is' in title_shouldbe: if actual_title != title_shouldbe: self.fail("Title is not \"%s\". Actual: \"%s\"" % (title_shouldbe, actual_title)) elif 'is_not' in title_shouldbe: if actual_title == title_shouldbe: self.fail("Title is \"%s\" when it shouldn't be" % title_shouldbe) if 'present' in params: """ Validate the presence of an element """ strategy, selector = translate_selector( params['present'], webdriver) return strategy(selector) if 'not_present' in params: """ Validate the absence of an element """ element = None strategy, selector = translate_selector( params['not_present'], webdriver) return strategy(selector) if 'text_of' in params: """ Validate text (or innerText) of an element """ text_of = params['text_of'] if 'element' not in text_of: raise DysonError("Key \"element\" is required") if 'is' in text_of: strategy, selector = translate_selector( text_of['element'], webdriver) actual_text = strategy(selector).text if actual_text != text_of['is']: self.fail( "Text of %s is not \"%s\". Actual: \"%s\"" % (text_of['element'], text_of['is'], actual_text)) else: return actual_text elif 'is_not' in text_of: strategy, selector = translate_selector( text_of['element'], webdriver) actual_text = strategy(selector).text if actual_text == text_of['is_not']: self.fail("Text of %s is \"%s\" when it shouldn't be" % (text_of['element'], text_of['is_not'])) else: return actual_text if 'value_of' in params: """ Validate value attribute of an element """ value_of = params['value_of'] if 'element' not in value_of: raise DysonError("Key \"element\" is required") if 'is' in value_of: strategy, selector = translate_selector( value_of['element'], webdriver) actual_value = strategy(selector).get_attribute('value') if actual_value != value_of['is']: self.fail("Value of %s is not \"%s\". Actual: \"%s\"" % (value_of['element'], value_of['is'], actual_value)) else: return actual_value elif 'is_not' in value_of: strategy, selector = translate_selector( value_of['element'], webdriver) actual_value = strategy(selector).get_attribute('value') if actual_value == value_of['is']: self.fail( "Value of %s is \"%s\" when it shouldn't be" % (value_of['element'], value_of['is'])) else: return actual_value if 'is_checked' in params: """ Validate that a checkbox / radio button is checked """ element = params['is_checked'] strategy, selector = translate_selector(element, webdriver) status = strategy(selector).is_selected() if not status: self.fail("Element %s is not checked" % element) else: return status if 'is_not_checked' in params: """ Validate that a checkbox / radio button is unchecked """ element = params['is_not_checked'] strategy, selector = translate_selector(element, webdriver) status = strategy(selector).is_selected() if status: self.fail("Element %s is checked" % element) else: return status elif isinstance(params, string_types): result = eval(params) if not to_boolean(result): self.fail("\"%s\" has a result of False" % params) return result
def run(self, webdriver, params): """ Validate things :param webdriver: :param params: allowed validations: - title - is - is_not - present - not_present - text_of - is - is_not - value_of - is - is_not - is_checked - is_unchecked :return: """ if params and isinstance(params, dict): if 'title' in params: """ Validate the title_shouldbe of the page """ actual_title = webdriver.title title_shouldbe = params['title'] if 'is' in title_shouldbe: if actual_title != title_shouldbe: self.fail("Title is not \"%s\". Actual: \"%s\"" % (title_shouldbe, actual_title)) elif 'is_not' in title_shouldbe: if actual_title == title_shouldbe: self.fail("Title is \"%s\" when it shouldn't be" % title_shouldbe) if 'present' in params: """ Validate the presence of an element """ strategy, selector = translate_selector(params['present'], webdriver) return strategy(selector) if 'not_present' in params: """ Validate the absence of an element """ element = None strategy, selector = translate_selector(params['not_present'], webdriver) return strategy(selector) if 'text_of' in params: """ Validate text (or innerText) of an element """ text_of = params['text_of'] if 'element' not in text_of: raise DysonError("Key \"element\" is required") if 'is' in text_of: strategy, selector = translate_selector(text_of['element'], webdriver) actual_text = strategy(selector).text if actual_text != text_of['is']: self.fail("Text of %s is not \"%s\". Actual: \"%s\"" % (text_of['element'], text_of['is'], actual_text)) else: return actual_text elif 'is_not' in text_of: strategy, selector = translate_selector(text_of['element'], webdriver) actual_text = strategy(selector).text if actual_text == text_of['is_not']: self.fail("Text of %s is \"%s\" when it shouldn't be" % ( text_of['element'], text_of['is_not'] )) else: return actual_text if 'value_of' in params: """ Validate value attribute of an element """ value_of = params['value_of'] if 'element' not in value_of: raise DysonError("Key \"element\" is required") if 'is' in value_of: strategy, selector = translate_selector(value_of['element'], webdriver) actual_value = strategy(selector).get_attribute('value') if actual_value != value_of['is']: self.fail("Value of %s is not \"%s\". Actual: \"%s\"" % ( value_of['element'], value_of['is'], actual_value )) else: return actual_value elif 'is_not' in value_of: strategy, selector = translate_selector(value_of['element'], webdriver) actual_value = strategy(selector).get_attribute('value') if actual_value == value_of['is']: self.fail("Value of %s is \"%s\" when it shouldn't be" % ( value_of['element'], value_of['is'] )) else: return actual_value if 'is_checked' in params: """ Validate that a checkbox / radio button is checked """ element = params['is_checked'] strategy, selector = translate_selector(element, webdriver) status = strategy(selector).is_selected() if not status: self.fail("Element %s is not checked" % element) else: return status if 'is_not_checked' in params: """ Validate that a checkbox / radio button is unchecked """ element = params['is_not_checked'] strategy, selector = translate_selector(element, webdriver) status = strategy(selector).is_selected() if status: self.fail("Element %s is checked" % element) else: return status elif isinstance(params, string_types): result = eval(params) if not to_boolean(result): self.fail("\"%s\" has a result of False" % params) return result