def testCreditLogin(self):
        """Check login/logout credit system"""
        home_page = HomePage(self.driver)
        home_page.open(self.URL)
        if home_page.default_alert_pops():
            home_page.close_default_alert()
        home_page.click_login()
        home_page.type_username(self.UserName)
        home_page.type_password(self.Password)
        home_page.submit_login()
        if home_page.default_alert_pops():
            home_page.close_default_alert()
        if home_page.logout_button_show() and home_page.update_pwd_button_show(
        ):
            logger.info("User: {} login system successfully!".format(
                self.UserName))
        else:
            logger.error("User: {} failed to login system!".format(
                self.UserName))
            raise AssertionError("Failed to login system!")

        home_page.click_logout()
        if home_page.default_alert_pops():
            home_page.close_default_alert()
        if home_page.login_button_show():
            logger.info("User: {} logout system successfully!".format(
                self.UserName))
        else:
            logger.error("User: {} failed to logout system!".format(
                self.UserName))
            raise AssertionError("Failed to logout system!")
 def find_element_which_displayed(self, *loc):
     try:
         return WebDriverWait(self.driver, 10,
                              1).until(ec.presence_of_element_located(loc))
     except TimeoutException or NoSuchElementException:
         logger.error(
             "The element: {} has not shown in current page!".format(
                 str(loc)))
         return False
 def find_elements(self, *loc):
     try:
         return WebDriverWait(self.driver, 10, 1).until(
             ec.presence_of_all_elements_located(loc))
     except TimeoutException or NoSuchElementException:
         logger.error(
             "The elements:{} have not been shown in current page!".format(
                 str(loc)))
         return False
 def capture_snapshot(self, file_name):
     try:
         base_dir = os.getcwd()
         file_path = base_dir + "/report/" + file_name
         file_path = os.path.abspath(file_path)
         self.driver.get_screenshot_as_file(file_path)
     except Exception as err:
         logger.error(err)
         logger.error("Failed to save snapshot: %s" % file_name)
 def find_element_which_clickable(self, *loc):
     try:
         return WebDriverWait(self.driver, 10,
                              1).until(ec.element_to_be_clickable(loc))
     except TimeoutException or NoSuchElementException:
         logger.error(
             "The element: {} has not shown in current page or clickable!".
             format(str(loc)))
         return False
 def get_nodes(self, target_node):
     """
     get node obj
     :param target_node: target node
     :return: list of node obj
     """
     try:
         return self.root.findall(".//" + target_node)
     except Exception as e:
         logger.error(e)
 def send_keys(self, value, clear_first=True, click_first=True, *loc):
     try:
         if click_first:
             self.find_element(*loc).click()
         if clear_first:
             self.find_element(*loc).clear()
         self.find_element(*loc).send_keys(value)
     except Exception as e:
         logger.error("Element with locator: %s has not been found!" %
                      str(loc))
         logger.error(e)
 def __init__(self, file_name):
     """
     Constructor
     :param file_name: full path of xml file
     """
     self.file_name = file_name
     try:
         self.tree = ElementTree(file=self.file_name)
         self.root = self.tree.getroot()
     except Exception as e:
         logger.error(e)
 def setUp(self):
     self.driver = initialize_browser()
     # preparing test data
     try:
         file_obj = ParseYaml('project.yml')
         data_dict = file_obj.get_yaml_dict()
         self.URL = data_dict['URL']
         self.UserName = data_dict['UserName']
         self.Password = data_dict['Password']
     except Exception as e:
         logger.error("Failed to open data file!")
         logger.error(e)
 def get_node_text(self, parent_node, target_node):
     """
     get one or more child node text under the same parent node
     :param parent_node: parent node
     :param target_node: target node
     :return: dic of target node: key=tag name of parent node, value=text content of target node
     """
     try:
         dic_node_value = {}
         for nd in self.root.iter(parent_node):
             dic_node_value[nd.get('name')] = nd.find(target_node).text
         return dic_node_value
     except Exception as e:
         logger.error(e)
 def get_nodes_text_by_parent_name(self, parent_name, target_node):
     """
     get text list of target nodes
     :param parent_name: 'name' attribute value of parent node
     :param target_node: target node
     :return: text list of target nodes
     """
     try:
         list_text = []
         for nd in self.root.findall(".//*[@name='" + parent_name + "']//" +
                                     target_node):
             list_text.append(nd.text)
         return list_text
     except Exception as e:
         logger.error(e)
Beispiel #12
0
    def set_email(self, ret_code):
        """
        set email service, attachment, etc
        :param ret_code: return code from parse_report_html
        :return: N/A
        """
        try:
            # third party SMTP service
            mail_host = MAIL_HOST
            mail_port = MAIL_PORT
            mail_user = MAIL_USER
            mail_pwd = MAIL_PWD
            sender = MAIL_SENDER
            receivers = MAIL_RECEIVERS

            # email attachment
            message = MIMEMultipart()
            message['From'] = sender
            message['To'] = ";".join(receivers)
            if ret_code == 0:
                subject = '全部成功√√√{}'.format(MAIL_SUBJECT)
            elif ret_code == 10010:
                subject = '解析报告失败!!!{}'.format(MAIL_SUBJECT)
            else:
                subject = '失败用例数:{}!!!{}'.format(ret_code, MAIL_SUBJECT)
            message['Subject'] = Header(subject, 'utf-8')

            # email body
            message.attach(
                MIMEText('附件为{},请查收'.format(MAIL_SUBJECT), 'plain', 'utf-8'))

            # email attachment
            att1 = MIMEText(
                open(self.report_file_path, 'rb').read(), 'base64', 'utf-8')
            att1["Content-Type"] = 'application/octet-stream'
            att1[
                "Content-Disposition"] = 'attachment; filename="automation_test_report.html"'
            message.attach(att1)

            obj_smtp = smtplib.SMTP()
            obj_smtp.connect(mail_host, mail_port)
            obj_smtp.login(mail_user, mail_pwd)
            obj_smtp.sendmail(sender, receivers, message.as_string())
            obj_smtp.quit()
            logger.info("Pass to send email")

        except Exception as e:
            logger.error(e)
Beispiel #13
0
    def setUp(self):
        global loc_links
        self.driver = initialize_browser()

        # prepare test data - fetch page links locator
        try:
            data_file = os.path.dirname(os.path.dirname(__file__)) + "/data/credit_home_links_check.csv"
            data_file = os.path.abspath(data_file)
            data = csv.reader(open(data_file, 'r'))
        except:
            logger.error("Failed to open data file!")

        for line in data:
            if line[0] != "section" and line[0] != "":
                loc_links.append(line[1])
        logger.info("The links are {}".format(loc_links))
Beispiel #14
0
    def parse_report_html(self):
        """
        judge whether all test pass or not
        :return: return code, num_fail_case or 10010 - parsing html file fail
        """
        try:

            with open(self.report_file_path, 'r', encoding='utf-8') as f:
                soup = BeautifulSoup(f.read(), "html.parser")
                ret = soup.select('tr#total_row td')
                # number of cases marked as fail or error
                num_fail_case = int(ret[3].text.strip()) + int(
                    ret[4].text.strip())
                return num_fail_case

        except Exception as e:
            logger.error(e)
            return 10010
    def get_attr_values(self, node_path, attr_name):
        """
        get the attribute value of some node
        :param node_path: node path where attribute locates
        :param attr_name:  attribute name
        :return: list of attribute values
        """
        try:
            attr_values = []
            nodes = self.root.findall(node_path)

            if len(nodes) > 0:
                for node in nodes:
                    attr_values.append(node.get(attr_name))

            return attr_values

        except Exception as e:
            logger.error(e)
Beispiel #16
0
    def exec_sql(self, driver, sql):
        if driver == "MYSQL":
            try:
                sql_result = SqlDriver(host=self.host,
                                       port=self.port,
                                       user=self.user,
                                       password=self.password,
                                       database=self.database).exec_mysql(sql)
                return sql_result
            except Exception as e:
                logger.error(e)

        elif driver == "MSSQL":
            try:
                sql_result = SqlDriver(host=self.host,
                                       port=self.port,
                                       user=self.user,
                                       password=self.password,
                                       database=self.database).exec_mssql(sql)
                return sql_result
            except Exception as e:
                logger.error(e)
Beispiel #17
0
    def exec_mysql(self, sql):
        try:

            conn = pymysql.connect(host=self.host,
                                   port=self.port,
                                   user=self.user,
                                   password=self.password,
                                   database=self.database,
                                   charset='utf8')
            cur = conn.cursor()
            if cur:
                logger.info(u"Execute SQL Statement |%s|" % sql)
                cur.execute(sql)
                rows = cur.fetchall()
                if len(rows) == 0:
                    logger.warning(u"No SQL Query Data Returned")
                return rows
            else:
                logger.error(u"Failed to Connect Database")
            conn.close()
        except Exception as e:
            logger.error(e)