Skip to content

ui_security

UISecurity

Bases: Security

Class for UI automation security-related functionality.

Source code in libs\cafex_ui\src\cafex_ui\ui_security.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class UISecurity(Security):
    """Class for UI automation security-related functionality."""

    def __init__(self):
        self.logger_class = CoreLogger(name=__name__)
        self.logger = self.logger_class.get_logger()

    def build_appium_url(
            self,
            mobile_platform: str,
            browserstack_username: str = None,
            browserstack_access_key: str = None,
            ip_address: str = None,
            port_number: str = None,
    ) -> str:
        """Builds the Appium URL."""

        try:
            if mobile_platform.lower() == "browserstack":
                return f"http://{browserstack_username}:{browserstack_access_key}@hub-cloud.browserstack.com/wd/hub"
            return f"http://{ip_address}:{port_number}/wd/hub"
        except Exception as e:
            self.logger.exception("Error in build_appium_url method: %s", e)
            return ""

    @staticmethod
    def start_bs_local(bs_local_obj: Local, local_args: dict) -> Local | Exception:
        """Starts BrowserStack Local Server."""
        try:
            if use_secured_password():
                local_args["key"] = decrypt_password(local_args["key"])
            for attempt in range(3):
                try:
                    bs_local_obj.start(**local_args)
                    if bs_local_obj.isRunning():
                        print("BrowserStack Local started successfully.")
                        return bs_local_obj
                except Exception as e:
                    print(f"Attempt {attempt + 1} failed: {e}")
                    time.sleep(5)  # Wait for 5 seconds before retrying
            raise Exception("Failed to start BrowserStack Local after maximum retries.")
        except Exception as e:
            print("Error in start_browserstack_local method-->" + str(e))
            raise e

    @staticmethod
    def mobile_upload_browserstack_app(
            browserstack_user_name: str,
            browserstack_access_key: str,
            appcenter_app_url: str,
            custom_id: str,
            browserstack_url: str,
    ) -> requests.Response | Exception:
        """This method sends a request to the BrowserStack API to upload a
        mobile app.

        Args:
            browserstack_user_name (str): The username for accessing the BrowserStack API.
            browserstack_access_key (str): The access key for accessing the BrowserStack API.
            appcenter_app_url (str): The URL of the app to be uploaded.
            custom_id (str): The custom ID for the app.
            browserstack_url (str): The URL of the BrowserStack API.

        Returns:
            response: The response from the BrowserStack API.
        """
        try:
            if use_secured_password():
                browserstack_access_key = decrypt_password(browserstack_access_key)
            payload = {
                "data": '{"url": "' + appcenter_app_url + '","custom_id": "' + custom_id + '"}'
            }
            response = requests.post(
                browserstack_url,
                auth=HTTPBasicAuth(browserstack_user_name, browserstack_access_key),
                data=payload,
                timeout=30,
            )
            return response
        except Exception as e:
            print("Error in uploading mobile broswerstack app-->" + str(e))
            return e

    @staticmethod
    def get_browser_stack_devices_list(browserstack_user_name: str, browserstack_access_key: str, browserstack_url: str
                                       ):
        """This method sends a request to the BrowserStack API and retrieves
        the response.

        Args:
            browserstack_user_name (str): The username for accessing the BrowserStack API.
            browserstack_access_key (str): The access key for accessing the BrowserStack API.
            browserstack_url (str): The URL of the BrowserStack API.

        Returns:
            response: The response from the BrowserStack API in JSON format.
        """
        try:
            response = requests.get(
                browserstack_url,
                auth=HTTPBasicAuth(browserstack_user_name, browserstack_access_key),
                timeout=30,
            )
            return response
        except Exception as e:
            print("Error in getting browser stack available devices list-->" + str(e))
            return e

    def create_browserstack_webdriver(
            self, browserstack_user_name: str, browserstack_access_key: str, options: object | list
    ) -> tuple | None:
        try:
            browserstack_url = self.__create_browserstack_url(
                browserstack_user_name, browserstack_access_key
            )
            browserstack_webdriver = webdriver.Remote(
                command_executor=browserstack_url, options=options
            )
            response = browserstack_webdriver.execute_script(
                'browserstack_executor: {"action": "getSessionDetails"}'
            )
            return browserstack_webdriver, response
        except Exception as e:
            print("Error in creating browserstack webdriver--> " + str(e))
            return None

    @staticmethod
    def __create_browserstack_url(user_name: str, access_key: str) -> str | None:
        try:
            if use_secured_password():
                access_key = decrypt_password(access_key)
            browserstack_url = f"https://{user_name}:{access_key}@hub-cloud.browserstack.com/wd/hub"
            return browserstack_url
        except Exception as e:
            print("Error in creating browserstack webdriver--> " + str(e))
            return None

    @staticmethod
    def get_browser_stack_browsers_list(
            browserstack_url: str,
            browserstack_user_name: str,
            browserstack_access_key: str,
    ) -> requests.Response | Exception:
        """This method sends a request to the BrowserStack API and retrieves
        the response.

        Args:
            browserstack_user_name (str): The username for accessing the BrowserStack API.
            browserstack_access_key (str): The access key for accessing the BrowserStack API.
            browserstack_url (str): The URL of the BrowserStack API.

        Returns:
            response: The response from the BrowserStack API in JSON format.
        """
        try:
            if use_secured_password():
                browserstack_access_key = decrypt_password(browserstack_access_key)
            response = requests.get(
                browserstack_url,
                auth=HTTPBasicAuth(browserstack_user_name, browserstack_access_key),
                timeout=30,
            )
            return response
        except Exception as e:
            print("Error in getting browser stack available browsers list-->" + str(e))
            return e

build_appium_url(mobile_platform, browserstack_username=None, browserstack_access_key=None, ip_address=None, port_number=None)

Builds the Appium URL.

Source code in libs\cafex_ui\src\cafex_ui\ui_security.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def build_appium_url(
        self,
        mobile_platform: str,
        browserstack_username: str = None,
        browserstack_access_key: str = None,
        ip_address: str = None,
        port_number: str = None,
) -> str:
    """Builds the Appium URL."""

    try:
        if mobile_platform.lower() == "browserstack":
            return f"http://{browserstack_username}:{browserstack_access_key}@hub-cloud.browserstack.com/wd/hub"
        return f"http://{ip_address}:{port_number}/wd/hub"
    except Exception as e:
        self.logger.exception("Error in build_appium_url method: %s", e)
        return ""

get_browser_stack_browsers_list(browserstack_url, browserstack_user_name, browserstack_access_key) staticmethod

This method sends a request to the BrowserStack API and retrieves the response.

Parameters:

Name Type Description Default
browserstack_user_name str

The username for accessing the BrowserStack API.

required
browserstack_access_key str

The access key for accessing the BrowserStack API.

required
browserstack_url str

The URL of the BrowserStack API.

required

Returns:

Name Type Description
response Response | Exception

The response from the BrowserStack API in JSON format.

Source code in libs\cafex_ui\src\cafex_ui\ui_security.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
@staticmethod
def get_browser_stack_browsers_list(
        browserstack_url: str,
        browserstack_user_name: str,
        browserstack_access_key: str,
) -> requests.Response | Exception:
    """This method sends a request to the BrowserStack API and retrieves
    the response.

    Args:
        browserstack_user_name (str): The username for accessing the BrowserStack API.
        browserstack_access_key (str): The access key for accessing the BrowserStack API.
        browserstack_url (str): The URL of the BrowserStack API.

    Returns:
        response: The response from the BrowserStack API in JSON format.
    """
    try:
        if use_secured_password():
            browserstack_access_key = decrypt_password(browserstack_access_key)
        response = requests.get(
            browserstack_url,
            auth=HTTPBasicAuth(browserstack_user_name, browserstack_access_key),
            timeout=30,
        )
        return response
    except Exception as e:
        print("Error in getting browser stack available browsers list-->" + str(e))
        return e

get_browser_stack_devices_list(browserstack_user_name, browserstack_access_key, browserstack_url) staticmethod

This method sends a request to the BrowserStack API and retrieves the response.

Parameters:

Name Type Description Default
browserstack_user_name str

The username for accessing the BrowserStack API.

required
browserstack_access_key str

The access key for accessing the BrowserStack API.

required
browserstack_url str

The URL of the BrowserStack API.

required

Returns:

Name Type Description
response

The response from the BrowserStack API in JSON format.

Source code in libs\cafex_ui\src\cafex_ui\ui_security.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@staticmethod
def get_browser_stack_devices_list(browserstack_user_name: str, browserstack_access_key: str, browserstack_url: str
                                   ):
    """This method sends a request to the BrowserStack API and retrieves
    the response.

    Args:
        browserstack_user_name (str): The username for accessing the BrowserStack API.
        browserstack_access_key (str): The access key for accessing the BrowserStack API.
        browserstack_url (str): The URL of the BrowserStack API.

    Returns:
        response: The response from the BrowserStack API in JSON format.
    """
    try:
        response = requests.get(
            browserstack_url,
            auth=HTTPBasicAuth(browserstack_user_name, browserstack_access_key),
            timeout=30,
        )
        return response
    except Exception as e:
        print("Error in getting browser stack available devices list-->" + str(e))
        return e

mobile_upload_browserstack_app(browserstack_user_name, browserstack_access_key, appcenter_app_url, custom_id, browserstack_url) staticmethod

This method sends a request to the BrowserStack API to upload a mobile app.

Parameters:

Name Type Description Default
browserstack_user_name str

The username for accessing the BrowserStack API.

required
browserstack_access_key str

The access key for accessing the BrowserStack API.

required
appcenter_app_url str

The URL of the app to be uploaded.

required
custom_id str

The custom ID for the app.

required
browserstack_url str

The URL of the BrowserStack API.

required

Returns:

Name Type Description
response Response | Exception

The response from the BrowserStack API.

Source code in libs\cafex_ui\src\cafex_ui\ui_security.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@staticmethod
def mobile_upload_browserstack_app(
        browserstack_user_name: str,
        browserstack_access_key: str,
        appcenter_app_url: str,
        custom_id: str,
        browserstack_url: str,
) -> requests.Response | Exception:
    """This method sends a request to the BrowserStack API to upload a
    mobile app.

    Args:
        browserstack_user_name (str): The username for accessing the BrowserStack API.
        browserstack_access_key (str): The access key for accessing the BrowserStack API.
        appcenter_app_url (str): The URL of the app to be uploaded.
        custom_id (str): The custom ID for the app.
        browserstack_url (str): The URL of the BrowserStack API.

    Returns:
        response: The response from the BrowserStack API.
    """
    try:
        if use_secured_password():
            browserstack_access_key = decrypt_password(browserstack_access_key)
        payload = {
            "data": '{"url": "' + appcenter_app_url + '","custom_id": "' + custom_id + '"}'
        }
        response = requests.post(
            browserstack_url,
            auth=HTTPBasicAuth(browserstack_user_name, browserstack_access_key),
            data=payload,
            timeout=30,
        )
        return response
    except Exception as e:
        print("Error in uploading mobile broswerstack app-->" + str(e))
        return e

start_bs_local(bs_local_obj, local_args) staticmethod

Starts BrowserStack Local Server.

Source code in libs\cafex_ui\src\cafex_ui\ui_security.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@staticmethod
def start_bs_local(bs_local_obj: Local, local_args: dict) -> Local | Exception:
    """Starts BrowserStack Local Server."""
    try:
        if use_secured_password():
            local_args["key"] = decrypt_password(local_args["key"])
        for attempt in range(3):
            try:
                bs_local_obj.start(**local_args)
                if bs_local_obj.isRunning():
                    print("BrowserStack Local started successfully.")
                    return bs_local_obj
            except Exception as e:
                print(f"Attempt {attempt + 1} failed: {e}")
                time.sleep(5)  # Wait for 5 seconds before retrying
        raise Exception("Failed to start BrowserStack Local after maximum retries.")
    except Exception as e:
        print("Error in start_browserstack_local method-->" + str(e))
        raise e