def execute(self, driver, config): try: if self.config_key == None: self.param = "" else: if self.config_key in config.params: self.param = config.params[self.config_key] else: raise Exception( "Cannot execute command %s as there was no key named %s in the config params" % (self.command, self.config_key)) self.driver = driver result = Result(passed=True, message="Execute %s %s" % (self.__repr__(), self.param)) logging.debug("Execute : %s %s" % (self.__repr__(), self.param)) if self.command == self.NAVIGATE: self.driver.get(self.param) elif self.command == self.CLICK: WebElement(self.driver, self.element.locators).click() elif self.command == self.SENDKEYS: WebElement(self.driver, self.element.locators).send_keys(self.param) elif self.command == self.VERIFY: WebElement(self.driver, self.element.locators).highlight() else: raise ValueError("Command not supported: %s" % self.command) except Exception as e: logging.debug("Exception : %s" % str(e)) result = Result(passed=False, message="Command raised an exception %s" % str(e), exception=str(e)) finally: self.execution_results.append(result) return result
def test_inequality(self): self.driver.get("http://www.google.com/") locators1 = [Locator(by=By.NAME,value="z"),Locator(by=By.CLASS_NAME,value="foo")] locators2 = [Locator(by=By.NAME,value="bar"),Locator(by=By.NAME,value="q")] element1 = WebElement(self.driver, locators1) element2 = WebElement(self.driver, locators2) assert element1 != element2 assert element2 != element1
def test_element_equal(self): self.driver.get("http://www.google.com/") locators1 = [Locator(by=By.NAME,value="q")] locators2 = [Locator(by=By.NAME,value="q")] element1 = WebElement(self.driver, locators1) element2 = WebElement(self.driver, locators2) assert element1 == element2 assert element2 == element1
def test_missing_locator_fails(self): self.driver.get("http://www.google.com/") locators1 = [Locator(by=By.NAME,value="z"),Locator(by=By.CLASS_NAME,value="foo")] locators2 = [Locator(by=By.NAME,value="bar"),Locator(by=By.NAME,value="q")] element1 = WebElement(self.driver, locators1) element2 = WebElement(self.driver, locators2) with self.assertRaises(AssertionError): assert element1 == element2 assert element2 == element1
def test_element_highlight(self): self.driver.get("http://www.google.com") locators = [ Locator(by=By.NAME, value="q"), Locator(by=By.NAME, value="btnK") ] WebElement(self.driver, locators).highlight()
def crawl_state(self, state): for element in state.elements: try: state.initialize_state(self.driver) print "Loaded initial state %s" % state.id webelement = WebElement(self.driver, element.locators) if webelement.is_displayed(): webelement.click() new_state = state_builder.get_current_state(self.driver) if new_state == state: print "looks like this is the same state" else: new_state.save() print 'new state found at %s %s' % (self.driver.current_url,new_state.id) click_action = action_builder.get_click_action(element, state, new_state) new_state.init_actions = state.init_actions new_state.init_actions.append(click_action) new_state.save() state.actions.append(click_action) state.save() else: logging.error("Could not reproduce state %s element %s not present" % (state, element)) except Exception as e: print "Couldn't crawl element %s %s" % (element.locators, str(e)) return state
def find_login_link(self, start_state): for element in start_state.elements: if element.type == ElementType.LOGIN: field = WebElement(self.driver, element.locators) if field.is_clickable(): return element return None
def get_web_elements(self, driver): webelements = [] for element in self.elements: webelement = WebElement(driver, element.locators) webelement.element_state = element webelements.append(webelement) return webelements
def test_multiple_locator(self): self.driver.get("http://www.google.com") locators = [ Locator(by=By.CLASS_NAME, value="Invalid"), Locator(by=By.NAME, value="q") ] WebElement(self.driver, locators).send_keys("ters")
def test_diff_elements(self): self.driver.get("http://www.google.com") locators = [ Locator(by=By.NAME, value="q"), Locator(by=By.NAME, value="btnK"), Locator(by=By.ID, value="lst-ib") ] WebElement(self.driver, locators).send_keys("ters")
def find_submit(self, start_state, element_types): for element in start_state.elements: if element.type in element_types: field = WebElement(self.driver, element.locators) if field.is_clickable(): return element raise Exception("Could find %s field in state : %s" % (element_types, start_state.id))
def is_state_present(self, driver): logging.debug("Is state Present: %s %s" % (self.id, self.url)) for element in self.elements: if not WebElement(driver,element.locators).is_present(5): logging.debug("Element not present: %s" % element.locators) return False else: logging.debug("Element is present: %s" % element.locators) return True
def find_input(self, start_state, element_types): for element in start_state.elements: if "<input" in element.html: field = WebElement(self.driver, element.locators) field.highlight(color="green") if field.is_editable() and element.type in element_types: return element # raise Exception("Could find %s field in state : %s" % (element_types,start_state.id)) return None
def test_get_missing_elements(self): self.url = "http://www.google.com" self.driver.get(self.url) state = state_builder.get_current_state(self.driver) state.elements.append( WebElement(self.driver, [Locator(by=By.CSS_SELECTOR, value="INVALID")])) missing_elements = state.get_missing_elements(self.driver) assert len(missing_elements) == 1
def build_element(driver, element): builder = LocatorBuilder(driver, element) locators = builder.get_locators() if(len(locators)) > 0: new_element = ElementState(locators=locators, html=element.html, screenshot=element.screenshot_as_base64) new_element.set_location(element) # new_element.type = ElementMatcher().match(element) new_element.save() WebElement(driver,new_element.locators).highlight() return new_element
def test_remove_missing_elements(self): self.url = "http://www.google.com" self.driver.get(self.url) state = state_builder.get_current_state(self.driver) state.elements.append( WebElement(self.driver, [Locator(by=By.CSS_SELECTOR, value="INVALID")])) missing_elements = state.get_missing_elements(self.driver) for element in missing_elements: print "removing %s" % element state.remove_element(element) assert element not in state.elements
def get_state(self, driver): parser = PageParser(driver) locator_elements = [] elements = parser.get_all_elements() print "Found %s elements " % len(elements) for element in elements: builder = LocatorBuilder(driver, element) locators = builder.get_locators() if(len(locators)) > 0: new_element = ElementState(locators=locators, html=element.html[:255], screenshot=element.screenshot_as_base64) new_element.set_location(element) new_element.save() locator_elements.append(new_element) WebElement(driver,new_element.locators).highlight() screenshot = driver.get_screenshot_as_base64() state = State(elements=locator_elements,url=driver.current_url, html=driver.html, screenshot = screenshot) return state
def crawl_state(self, state): for element in state.elements[:20]: try: state.initialize_state(self.driver) print "Loaded initial state %s" % state.id webelement = WebElement(self.driver, element.locators) if webelement.is_displayed(): print "Clicking %s" % element webelement.highlight(length=2, color="red") webelement.click() domain = url_parser.get_domain_from_url( self.driver.current_url) if domain not in state.url: print "State is different domain" break if state.is_state_present(self.driver): print "looks like this is the same state" else: new_state = state_builder.get_current_state( self.driver) new_state.save() print 'new state found at %s %s' % ( self.driver.current_url, new_state.id) click_action = action_builder.get_click_action( element, state, new_state) new_state.init_actions = state.init_actions new_state.init_actions.append(click_action) new_state.save() state.actions.append(click_action) state.save() self.put(new_state) else: logging.error( "Could not reproduce state %s element %s not present" % (state, element)) except Exception as e: print "Couldn't crawl element %s %s" % (element.locators, str(e)) return state
def test_element_screenshot_string(self): self.driver.get("http://www.google.com") elment = WebElement(self.driver, [Locator(by=By.NAME, value="q")]) screenshot = elment.screenshot_as_base64 assert screenshot is not "" and not None
def test_find_parent(self): self.driver.get("http://www.google.com") elment = WebElement(self.driver, [Locator(by=By.NAME, value="q")]) parent = elment.find_parent() assert parent.get_attribute("id") == "gs_lc0"
def test_element_not_found(self): self.driver.get("http://www.google.com") with self.assertRaises(exceptions.NoSuchElementException): WebElement(self.driver, [Locator(by=By.CLASS_NAME, value="q")]).click()
def test_input_is_editable(self): self.driver.get("http://www.google.com") locators = [Locator(by=By.NAME, value="q")] element = WebElement(self.driver, locators) typer = ElementType(element) assert typer.is_editable()
def check_state(self, state): self.get_live_elements() for element in state.elements: webelement = WebElement(self.driver,element.locators).element self.live_elements.remove(webelement) return self.live_elements
def test_send_keys(self): self.driver.get("http://www.google.com/") command = Command(driver=self.driver, command=Command.SENDKEYS,element=ElementState(locators=[Locator(by=By.NAME,value="q")]),config_key="search") command.execute(self.driver, config=RunConfig(params={"search":"Something"})) assert WebElement(self.driver, [Locator(by=By.NAME,value="q")]).value == "Something"
def test_button_is_clickable(self): self.driver.get("http://www.google.com") locators = [Locator(by=By.LINK_TEXT, value="Images")] element = WebElement(self.driver, locators) typer = ElementType(element) assert typer.is_clickable()
def web_element(self, element): field = WebElement(self.driver, element.locators) return field
def verify_state(self,driver): logging.debug("Verifying state %s %s" % (self.id, self.url)) for element in self.elements: WebElement(driver,element.locators).highlight()
def get_missing_elements(self,driver): missing_elements = [] for element in self.elements: if not WebElement(driver,element.locators).is_present(): missing_elements.append(element) return missing_elements
def test_element_screenshot(self): self.driver.get("http://www.google.com") elment = WebElement(self.driver, [Locator(by=By.NAME, value="q")]) elment.screenshot(os.path.join(config.ROOT_DIR, "element.png"))
def test_element_html(self): self.driver.get("http://www.google.com") elment = WebElement(self.driver, [Locator(by=By.NAME, value="q")]) print elment.html