Exemplo n.º 1
0
    def find_element(self,
                     find_method: str,
                     path_to_elem: str,
                     more_than_1_elem: bool = False) -> WebElement:

        log_client = LogsClient(output_file=self.log_output_file,
                                project_dir=project_dir,
                                file_name=file_name,
                                log_run_uuid=self.log_run_uuid)

        try:

            log_client.set_msg(
                log_type="info",
                log_msg=
                f"trying to reach element by {find_method} method at path: {path_to_elem}"
            )

            if more_than_1_elem:

                if find_method == "class":

                    web_elem = self.chrome.find_elements_by_class_name(
                        name=path_to_elem)

            else:

                if find_method == "xpath":

                    web_elem = self.chrome.find_element_by_xpath(
                        xpath=path_to_elem)

                elif find_method == "class":

                    web_elem = self.chrome.find_element_by_class_name(
                        name=path_to_elem)

            log_client.set_msg(log_type="info",
                               log_msg="element was reached successfully")

            sleep(0.1)

            return web_elem

        except NoSuchElementException:

            log_client.set_msg(
                log_type="error",
                log_msg=
                f"error while trying to find elem at path: {path_to_elem}")

        except Exception as e:

            log_client.set_msg(
                log_type="error",
                log_msg=f"the following error occurred with args: {e.args}")
Exemplo n.º 2
0
    def _set_chrome_options(self, options: List) -> ChromeOptions:
        """Static method to create a ChromeOptions class with options.
        Args:
            options: list of options as defined in __init___ method
        Returns:
            instance of ChromeOptions with options defined in options parameter
        """
        log_client = LogsClient(output_file=self.log_output_file,
                                project_dir=project_dir,
                                file_name=file_name,
                                log_run_uuid=self.log_run_uuid)

        log_client.set_msg(log_type="info",
                           log_msg="setting chrome options")

        try:

            chrome_options = ChromeOptions()

            for option in options:
                if isinstance(option, dict):
                    chrome_options.add_experimental_option("prefs", option)
                else:
                    chrome_options.add_argument(option)

            return chrome_options

        except Exception as e:

            log_client.set_msg(log_type="error",
                               log_msg=f"the following error occurred with args: {e.args}")

            assert False, "breaking code execution, see log file to track error"
Exemplo n.º 3
0
    def action_on_elem(self,
                       web_elem: WebElement,
                       action: str,
                       content: str = ""):

        log_client = LogsClient(output_file=self.log_output_file,
                                project_dir=project_dir,
                                file_name=file_name,
                                log_run_uuid=self.log_run_uuid)

        log_client.set_msg(
            log_type="info",
            log_msg=f"action: {action} on web element: {web_elem}")

        try:

            if action == "click":

                web_elem.click()

            elif action == "send_keys":

                web_elem.send_keys(content)

            sleep(2)

        except NoSuchElementException:

            log_client.set_msg(
                log_type="error",
                log_msg=
                f"error while trying to perform action: {action} at web element: {web_elem}"
            )

        except Exception as e:

            log_client.set_msg(
                log_type="error",
                log_msg=f"the following error occurred with args: {e.args}")
Exemplo n.º 4
0
from pathlib import Path
import io
import os
from typing import Dict, Any
from logs.logs_generator import LogsClient
import uuid
from time import sleep
from selenium.webdriver import Chrome
from utils.config_vars import FINANCIAL_DATA_FILES_OUTPUT_DIR
import pytz

local_project_root_dir = Path(__file__).resolve().parents[5]
uuid_4 = uuid.uuid4()

log_client = LogsClient(output_file="bank_acc_balance.log",
                        project_dir=local_project_root_dir,
                        file_name=os.path.basename(__file__),
                        log_run_uuid=uuid_4)


def append_data_to_json(data_to_append: Dict[str, Any], file):

    try:

        log_client.set_msg(log_type="info",
                           log_msg="appending data to existing json file")

        new_file_content = {}

        for content in file:

            content_json = json.loads(content)
Exemplo n.º 5
0
    def get_initial_page(self):

        log_client = LogsClient(output_file=self.log_output_file,
                                project_dir=project_dir,
                                file_name=file_name,
                                log_run_uuid=self.log_run_uuid)

        try:

            # delete cookies and go to defined url
            log_client.set_msg(log_type="info",
                               log_msg="deleting browser cookies")

            self.chrome.delete_all_cookies()

            sleep(2)

            log_client.set_msg(log_type="info",
                               log_msg=f"going to url: {self.url}")

            self.chrome.get(self.url)
            sleep(5)

            # get agency text box
            web_elem_xpath = '//*[@id="cooperativa"]'

            agency = self.find_element(find_method="xpath",
                                       path_to_elem=web_elem_xpath)

            sleep(5)

            # fill in agency
            log_client.set_msg(log_type="info", log_msg="filling bank agency")

            self.action_on_elem(web_elem=agency,
                                action="send_keys",
                                content=self.agency)

            sleep(5)

            # get account text box
            web_elem_xpath = '//*[@id="conta"]'

            account = self.find_element(find_method="xpath",
                                        path_to_elem=web_elem_xpath)

            # fill in account
            log_client.set_msg(log_type="info", log_msg="filling bank account")

            self.action_on_elem(web_elem=account,
                                action="send_keys",
                                content=self.account)
            sleep(5)

            # loop over password length
            log_client.set_msg(log_type="info",
                               log_msg="filling bank password")

            log_client.set_msg(log_type="info",
                               log_msg=f"filling password characters")
            for i, character in enumerate(self.password, 1):

                web_elem_path = "tecla"

                numbered_btns = self.find_element(find_method="class",
                                                  path_to_elem=web_elem_path,
                                                  more_than_1_elem=True)

                # when there's match between password character and web page character, then click on it
                for number in numbered_btns:
                    if number.text == character:
                        self.action_on_elem(web_elem=number, action="click")

            # login after filling in the password
            web_elem_xpath = '//*[@id="buttons"]/input[1]'

            login_btn = self.find_element(find_method="xpath",
                                          path_to_elem=web_elem_xpath)

            log_client.set_msg(log_type="info", log_msg="logging in")

            self.action_on_elem(web_elem=login_btn, action="click")
            sleep(5)

            return self.chrome

        except Exception as e:

            log_client.set_msg(
                log_type="error",
                log_msg=f"the following error occurred with args: {e.args}")

            assert False, "breaking code execution, see log file to track error"