class Action(db.Document, Executable): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) name = db.StringField(required=True) start_state = db.ReferenceField("State", required=True) end_state = db.ReferenceField("State", required=True) steps = db.ListField(db.EmbeddedDocumentField(Command), required=False) execution_results = [] def execute(self, driver, config): logging.debug("Executing Action %s" % self.name) # if not self.start_state.is_state_present(driver): # result =Result(step_results=self.execution_results,passed=False,message="State %s not present" % self.start_state) # result.failed_state = self.start_state # result.actual_state = self.start_state.get_current_state(driver) # result.actual_state.save() # result.html = driver.html # result.screenshot = driver.get_screenshot_as_base64() # return result result = Executable.execute(self, driver, config) if not result.passed: result.failed_state = self.start_state result.actual_state = self.start_state.get_current_state(driver) result.actual_state.save() result.html = driver.html result.screenshot = driver.get_screenshot_as_base64() return result
class ParkingLot(db.Document): pid = db.IntField(unique=True) parking_lot_name = db.StringField(max_length=100) cost = db.ReferenceField(Cost, dbref=False) two_wheeler_capacity = db.IntField() four_wheeler_capacity = db.IntField() heavy_vehicle_capacity = db.IntField() current_two_wheeler = db.IntField(default=0) current_four_wheeler = db.IntField(default=0) current_heavy_vehicle = db.IntField(default=0) transactions = db.ListField( db.ReferenceField(Transaction, dbref=False, reverse_delete_rule=NULLIFY)) def __str__(self): return "Parking: {} two_wheeler :{}, four_wheeler: {}, heavy_vehicle: {}".format( self.parking_lot_name, self.cost.two_wheeler, self.cost.four_wheeler, self.cost.heavy_vehicle) def get_dict(self): return { 'pid': self.pid, 'parking_lot_name': self.parking_lot_name, 'transactions': self.transactions } def save(self, *args, **kwargs): if self.pid == None: try: self.pid = self.__class__.objects.order_by('-pid')[0].pid + 1 except IndexError: self.pid = ParkingLot.objects.count() + 1 super(ParkingLot, self).save(*args, **kwargs)
class Result(db.EmbeddedDocument): total_indents = 0 created_at = db.DateTimeField(default=datetime.datetime.now, required=True) passed = db.BooleanField(required=True) message = db.StringField(required=True) exception = db.StringField(max_length=255, required=False) step_results = db.ListField(db.EmbeddedDocumentField('self')) failed_state = db.ReferenceField('State', required=False) actual_state = db.ReferenceField('State', required=False) html = db.StringField(required=False) screenshot = db.StringField(required=False) def __str__(self): Result.total_indents += 1 message = "Executable Passed=%s %s \r\n" % (self.passed, self.message) for result in self.step_results: for indent in range(0, Result.total_indents): message += " " message += str(result) Result.total_indents -= 1 return message def __repr__(self): return "Result(%s,%s)" % (self.passed, self.message)
class Page(db.Document): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) url = db.StringField(max_length=255, required=False) default_state = db.ReferenceField(State, required=True) states = db.ListField(db.ReferenceField(State)) meta = { 'allow_inheritance': True, 'indexes': ['-created_at'], 'ordering': ['-created_at'] }
class Customer(db.Document): cid = db.IntField(unique=True) email = db.StringField(max_length=255, unique=True) password = db.StringField(max_length=500) first_name = db.StringField(max_length=100) last_name = db.StringField(max_length=100) contact_no = db.IntField(min_value=1000000000, max_value=9999999999) address = db.StringField(max_length=500) created_on = db.DateTimeField() modified_on = db.DateTimeField() latest_transaction_cost = db.IntField() driving_licence_link = db.URLField() QR_CODE_DATA = db.StringField(max_length=200) vehicles = db.ListField( db.ReferenceField(Vehicle, dbref=False, reverse_delete_rule=NULLIFY)) transactions = db.ListField( db.ReferenceField(Transaction, dbref=False, reverse_delete_rule=NULLIFY)) def __unicode__(self): return str(self.cid) def get_dict(self): return { 'cid': self.cid, 'first_name': self.first_name, 'last_name': self.last_name, 'contact_no': self.contact_no, 'address': self.address, 'latest_transaction_cost': latest_transaction_cost, 'driving_licence_link': self.driving_licence_link } def __repr__(self): return 'cid ' + str(self.cid) def save(self, *args, **kwargs): if self.cid == None: try: self.cid = self.__class__.objects.order_by('-cid')[0].cid + 1 except IndexError: self.cid = Customer.objects.count() + 1 if not self.created_on: self.created_on = datetime.now() self.modified_on = datetime.now() self.password = hashlib.sha1(self.password).hexdigest() if not self.QR_CODE_DATA: self.QR_CODE_DATA = hashlib.sha1( str(self.cid) + str(self.created_on)).hexdigest() super(Customer, self).save(*args, **kwargs)
class Command(db.EmbeddedDocument, Executable): NAVIGATE = "Navigate" CLICK = "Click" SENDKEYS = "SendKeys" VERIFY = "Verify" COMMANDS = (NAVIGATE, CLICK, SENDKEYS, VERIFY) created_at = db.DateTimeField(default=datetime.datetime.now, required=True) command = db.StringField(required=True, choices=COMMANDS) element = db.ReferenceField(ElementState, required=False) config_key = db.StringField(required=False) execution_results = [] def get_steps(self): return self execution_results = [] 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 __repr__(self): return "Command(%s %s %s)" % (str(self.command), str( self.element), self.param)
class User(db.Document, UserMixin): email = db.StringField(max_length=255) password = db.StringField(max_length=500) active = db.BooleanField(default=True) confirmed_at = db.DateTimeField() roles = db.ListField(db.ReferenceField(Role), default=[]) def save(self, *args, **kwargs): self.password = encrypt_password(self.password) self.confirmed_at = datetime.now() super(User, self).save(*args, **kwargs)
class SuiteRun(db.Document, Executable): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) name = db.StringField(max_length=255, required=False) suite = db.ReferenceField(Suite, required=True) config = db.ReferenceField(SuiteConfig, required=True) start_after = db.DateTimeField(default=datetime.datetime.now, required=True) started_time = db.DateTimeField(default=datetime.datetime.now, required=True) ended_time = db.DateTimeField(default=datetime.datetime.now, required=True) status = db.StringField(max_length=25,required=True, choices=Status.ALL_STATUS) message = db.StringField(max_length=255,required=True) user = db.StringField(max_length=255, required=True) suite_results = db.ListField(db.EmbeddedDocumentField(Result), required=False) meta = { 'allow_inheritance': True, 'indexes': ['-created_at'], 'ordering': ['-created_at'] } def execute(self, driver): logging.debug("Executing Suite %s" % self.name) self.driver = driver suite_result = Result(passed=True,message="Passed",exception="Passed") for test in self.tests: test_result = test.execute(driver) suite_result.step_results.append(test_result) if not test_result.passed: suite_result.passed = False suite_result.message = str(self.__class__) suite_result.exception = test_result.exception suite_result.failed_state = test_result.failed_state suite_result.actual_state = test_result.actual_state suite_result.html = self.driver.page_source suite_result.screenshot = self.driver.get_screenshot_as_base64() self.suite_results.append(suite_result) self.cascade_save() return suite_result
class FoodCost(db.Document): category = db.StringField(maxlength=255) menu_item = db.StringField(maxlength=255) url = db.StringField(maxlength=255) created_on_date = db.StringField(maxlength=255) last_updated_on = db.StringField(maxlength=255) portions_size = db.StringField(maxlength=255) cost_per_portion = db.DecimalField(precision=2) sales_price = db.DecimalField(precision=2) food_cost_percent = db.StringField() recipe_quantity = db.IntField() total_recipe_cost = db.DecimalField(precision=2) total_gross_sales = db.DecimalField(precision=2) ingredients = db.ListField(db.DictField(db.ReferenceField(Ingredient))) def __repr__(self): return "<MongoEngine Document: FoodCost>"
class Test(db.Document, Executable): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) name = db.StringField(max_length=255, required=False) actions = db.ListField(db.ReferenceField(Action)) meta = { 'allow_inheritance': True, 'indexes': ['-created_at'], 'ordering': ['-created_at'] } def execute(self, driver, config): logging.debug("Executing Test %s" % self.name) self.steps = self.actions suite_results = Executable.execute(self, driver, config) return suite_results def get_steps(self): return self.actions
class Transaction(db.Document): cost = db.ReferenceField(Cost, dbref=False) pid = db.IntField() QR_CODE_DATA = db.StringField() total_cost = db.IntField() entry_time_stamp = db.DateTimeField() exit_time_stamp = db.DateTimeField() active = db.BooleanField(default=False) def __str__(self): return "total_cost: {}, cost: {}, entry: {}, exit: {}".format( self.total_cost, self.cost, self.entry_time_stamp, self.exit_time_stamp) def get_dict(self): return { 'total_cost': self.total_cost, 'entry_time_stamp': self.entry_time_stamp, 'exit_time_stamp': self.exit_time_stamp, 'pid': self.pid }
class State(db.Document): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) html = db.StringField(required=False) screenshot = db.StringField(required=False) elements = db.ListField(db.ReferenceField(ElementState)) actions = db.ListField(db.ReferenceField(Action)) init_actions = db.ListField(db.ReferenceField(Action), required=False) url = db.StringField(max_length=255, required=True) name = db.StringField(max_length=255, required=False) meta = { 'allow_inheritance': True, 'indexes': ['-created_at','-url'], 'ordering': ['-created_at', '-url'], } def __eq__(self, other): if other is None: return False for element in self.elements: if element not in other.elements: return False return True def __sub__(self, other): """ Returns a State representing the difference in elements :param other: :return: """ new_elements = [] for element in self.elements: if element not in other.elements: new_elements.append(element) return State(elements=new_elements, url=self.url) def __add__(self, other): """ Combines two states together :param other: :return: """ all_elements = self.elements.extend(other.elements) return State(elements=all_elements,url=self.url) def __div__(self, other): """ Returns the elements not shared with the second state :param other: :return: """ new_elements = [] for element in self.elements: if element in other.elements: new_elements.append(element) return State(elements=new_elements,url=self.url) def __repr__(self): return "State(url=%s) %s Elements %s" % (self.url, len(self.elements),self.elements) 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 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 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 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 get_replaced_elements(self,driver, new_state): replaced_elements = [] for element in new_state.elements: if element not in self.elements: replaced_elements.apppend(element) return replaced_elements def update_element(self, old_element, new_element): self.elements.remove(old_element) self.elements.append(new_element) def remove_element(self, element): self.elements.remove(element) def add_element(self, element): self.elements.append(element) def get_html_info(self): self.element_html = [] for element in self.elements: self.element_html.append(element.html) def initialize_state(self, driver,config): for action in self.init_actions: action.execute(driver,config) self.verify_state(driver) def get_current_state(self, driver): try: return self.get_state(driver) except Exception as e: logging.exception(str(e)) return self.get_state(driver) 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 get_blank_state(self): return State.objects(url="").first()