Skip to content

UI

UI test

This module defines the class BasePage to connect & set options web-driver.

BasePage

Initialize web-driver with defined options.

Attributes:

Name Type Description
PATH str

Set path to the driver from config_dict.

Source code in modules/ui/page_objects/base_page.py
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
class BasePage:
    """Initialize web-driver with defined options.

    Attributes:
        PATH (str): Set path to the driver from config_dict.
    """
    PATH = config_dict['UI']['driver_path']

    def __init__(self, headless=False, no_sandbox=False) -> None:
        """Init options & web-driver Chrome.

        Args:
            headless (bool): if `True` GUI is off.
            no_sandbox (bool): if `True` it run without the sandbox.
                Usefull to run apps with docker or k8s
                (without will be error).
        """
        self.chrome_options = Options()
        if headless:
            self.chrome_options.add_argument("--headless")
        if no_sandbox:
            self.chrome_options.add_argument("--no-sandbox")

        self.driver = webdriver.Chrome(
            service=Service(BasePage.PATH),
            options=self.chrome_options,
        )

    def close(self):
        """Provide a close method for the driver."""
        self.driver.close()

__init__(headless=False, no_sandbox=False)

Init options & web-driver Chrome.

Parameters:

Name Type Description Default
headless bool

if True GUI is off.

False
no_sandbox bool

if True it run without the sandbox. Usefull to run apps with docker or k8s (without will be error).

False
Source code in modules/ui/page_objects/base_page.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def __init__(self, headless=False, no_sandbox=False) -> None:
    """Init options & web-driver Chrome.

    Args:
        headless (bool): if `True` GUI is off.
        no_sandbox (bool): if `True` it run without the sandbox.
            Usefull to run apps with docker or k8s
            (without will be error).
    """
    self.chrome_options = Options()
    if headless:
        self.chrome_options.add_argument("--headless")
    if no_sandbox:
        self.chrome_options.add_argument("--no-sandbox")

    self.driver = webdriver.Chrome(
        service=Service(BasePage.PATH),
        options=self.chrome_options,
    )

close()

Provide a close method for the driver.

Source code in modules/ui/page_objects/base_page.py
40
41
42
def close(self):
    """Provide a close method for the driver."""
    self.driver.close()

This module defines the class SignInPage to test UI pages.

SignInPage

Bases: BasePage

Define const & methods for testing Login GitHub page

Source code in modules/ui/page_objects/sign_in_page.py
 7
 8
 9
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
class SignInPage(BasePage):
    """Define const & methods for testing Login GitHub page"""
    URL = 'https://github.com/login'

    def __init__(self, headless=None, no_sandbox=None) -> None:
        """Initialize parent class & define options
        for Chrome.driver (--headless, --no-sandbox)
        """
        super().__init__(headless, no_sandbox)

    def go_to(self) -> None:
        """Open sign-in page"""
        self.driver.get(SignInPage.URL)

    def try_login(self, username: str, password: str) -> None:
        """Try to login

        Args:
            username: login username
            password: user's password
        """
        # Search the login field by typing Username
        login_elem = self.driver.find_element(By.ID, "login_field")

        # Typing incorrect login
        login_elem.send_keys(username)

        # Search password field
        password_elem = self.driver.find_element(By.ID, "password")

        # Typing an incorrect password
        password_elem.send_keys(password)

        # Search the "Sign-in" button by attr. - name
        btn_elem = self.driver.find_element(By.NAME, "commit")

        # Emulation LCM (left click mouse)
        btn_elem.click()

    def check_title(self, expected_title: str) -> bool:
        """Get the content of the title tag &
        compare it with what is expected.
        """
        return self.driver.title == expected_title

__init__(headless=None, no_sandbox=None)

Initialize parent class & define options for Chrome.driver (--headless, --no-sandbox)

Source code in modules/ui/page_objects/sign_in_page.py
11
12
13
14
15
def __init__(self, headless=None, no_sandbox=None) -> None:
    """Initialize parent class & define options
    for Chrome.driver (--headless, --no-sandbox)
    """
    super().__init__(headless, no_sandbox)

check_title(expected_title)

Get the content of the title tag & compare it with what is expected.

Source code in modules/ui/page_objects/sign_in_page.py
46
47
48
49
50
def check_title(self, expected_title: str) -> bool:
    """Get the content of the title tag &
    compare it with what is expected.
    """
    return self.driver.title == expected_title

go_to()

Open sign-in page

Source code in modules/ui/page_objects/sign_in_page.py
17
18
19
def go_to(self) -> None:
    """Open sign-in page"""
    self.driver.get(SignInPage.URL)

try_login(username, password)

Try to login

Parameters:

Name Type Description Default
username str

login username

required
password str

user's password

required
Source code in modules/ui/page_objects/sign_in_page.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def try_login(self, username: str, password: str) -> None:
    """Try to login

    Args:
        username: login username
        password: user's password
    """
    # Search the login field by typing Username
    login_elem = self.driver.find_element(By.ID, "login_field")

    # Typing incorrect login
    login_elem.send_keys(username)

    # Search password field
    password_elem = self.driver.find_element(By.ID, "password")

    # Typing an incorrect password
    password_elem.send_keys(password)

    # Search the "Sign-in" button by attr. - name
    btn_elem = self.driver.find_element(By.NAME, "commit")

    # Emulation LCM (left click mouse)
    btn_elem.click()