Skip to content

Examples

qa_pytest_examples

__all__ = ['CombinedConfiguration', 'CombinedSteps', 'Credentials', 'RabbitMqSelfConfiguration', 'SwaggerPetstoreConfiguration', 'SwaggerPetstoreCredentials', 'SwaggerPetstorePet', 'SwaggerPetstoreSteps', 'TerminalXConfiguration', 'TerminalXCredentials', 'TerminalXSteps', 'TerminalXUser'] module-attribute

CombinedConfiguration

Bases: SwaggerPetstoreConfiguration, TerminalXConfiguration

Combined configuration that inherits settings from both SwaggerPetstoreConfiguration and TerminalXConfiguration. Useful for scenarios requiring both sets of configuration.

Source code in qa-pytest-examples/src/qa_pytest_examples/combined_configuration.py
12
13
14
15
16
17
18
class CombinedConfiguration(
        SwaggerPetstoreConfiguration, TerminalXConfiguration):
    """
    Combined configuration that inherits settings from both SwaggerPetstoreConfiguration
    and TerminalXConfiguration. Useful for scenarios requiring both sets of configuration.
    """
    pass

CombinedSteps

Bases: SwaggerPetstoreSteps[CombinedConfiguration], TerminalXSteps[CombinedConfiguration]

Combined steps implementation that inherits step definitions from both SwaggerPetstoreSteps and TerminalXSteps, using CombinedConfiguration. Useful for scenarios requiring both sets of step logic in a single test suite.

Source code in qa-pytest-examples/src/qa_pytest_examples/combined_steps.py
 6
 7
 8
 9
10
11
12
13
14
class CombinedSteps(
        SwaggerPetstoreSteps[CombinedConfiguration],
        TerminalXSteps[CombinedConfiguration]):
    """
    Combined steps implementation that inherits step definitions from both
    SwaggerPetstoreSteps and TerminalXSteps, using CombinedConfiguration.
    Useful for scenarios requiring both sets of step logic in a single test suite.
    """
    pass

Credentials dataclass

Represents user credentials with username and password.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/credentials.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@dataclass(frozen=True)
class Credentials:
    """
    Represents user credentials with username and password.
    """

    username: str
    password: str

    @classmethod
    def from_(cls, colon_separated: str):
        """
        Creates a Credentials instance from a colon-separated string.

        Args:
            colon_separated (str): String in the format 'username:password'.

        Returns:
            Credentials: The created credentials instance.
        """
        return cls(*colon_separated.split(":"))

password instance-attribute

username instance-attribute

__init__(username, password)

from_(colon_separated) classmethod

Creates a Credentials instance from a colon-separated string.

Parameters:

Name Type Description Default
colon_separated str

String in the format 'username:password'.

required

Returns:

Name Type Description
Credentials

The created credentials instance.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/credentials.py
17
18
19
20
21
22
23
24
25
26
27
28
@classmethod
def from_(cls, colon_separated: str):
    """
    Creates a Credentials instance from a colon-separated string.

    Args:
        colon_separated (str): String in the format 'username:password'.

    Returns:
        Credentials: The created credentials instance.
    """
    return cls(*colon_separated.split(":"))

RabbitMqSelfConfiguration

Bases: RabbitMqConfiguration

Configuration for self-contained RabbitMQ test scenarios. Inherits all settings from RabbitMqConfiguration and can be extended for test-specific overrides or additional configuration.

Source code in qa-pytest-examples/src/qa_pytest_examples/rabbitmq_self_configuration.py
 6
 7
 8
 9
10
11
12
class RabbitMqSelfConfiguration(RabbitMqConfiguration):
    """
    Configuration for self-contained RabbitMQ test scenarios.
    Inherits all settings from RabbitMqConfiguration and can be extended for
    test-specific overrides or additional configuration.
    """
    pass

SwaggerPetstoreConfiguration

Bases: RestConfiguration

Configuration for Swagger Petstore REST API tests. Inherits from RestConfiguration.

Source code in qa-pytest-examples/src/qa_pytest_examples/swagger_petstore_configuration.py
 8
 9
10
11
12
13
class SwaggerPetstoreConfiguration(RestConfiguration):
    """
    Configuration for Swagger Petstore REST API tests.
    Inherits from RestConfiguration.
    """
    pass

SwaggerPetstoreCredentials dataclass

Bases: Credentials

Credentials for Swagger Petstore users. Inherits from Credentials.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/swagger_petstore_credentials.py
10
11
12
13
14
15
16
@dataclass(frozen=True)
class SwaggerPetstoreCredentials(Credentials):
    """
    Credentials for Swagger Petstore users.
    Inherits from Credentials.
    """
    pass

__init__(username, password)

SwaggerPetstorePet dataclass

Represents a pet in the Swagger Petstore API.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/swagger_petstore_pet.py
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
@dataclass(eq=True, frozen=True)
@to_string()
class SwaggerPetstorePet:
    """
    Represents a pet in the Swagger Petstore API.
    """
    name: str
    status: str

    @staticmethod
    def random(range: range = range(1)) -> Iterator['SwaggerPetstorePet']:
        """
        Generates a random SwaggerPetstorePet with a unique name and 'available' status.

        Returns:
            SwaggerPetstorePet: The generated pet.
        """
        for _ in range:
            yield SwaggerPetstorePet(name=str(uuid4()), status="available")

    @staticmethod
    def from_(response: Response) -> Iterator['SwaggerPetstorePet']:
        """
        Parses a response and yields SwaggerPetstorePet objects for each valid pet entry.

        Args:
            response (Response): The HTTP response containing pet data.
        Returns:
            Iterator[SwaggerPetstorePet]: Iterator over parsed pets.
        """
        return (
            SwaggerPetstorePet(name=pet["name"], status=pet["status"])
            for pet in response.json()
            if "name" in pet and "status" in pet
        )

name instance-attribute

status instance-attribute

__init__(name, status)

from_(response) staticmethod

Parses a response and yields SwaggerPetstorePet objects for each valid pet entry.

Parameters:

Name Type Description Default
response Response

The HTTP response containing pet data.

required

Returns: Iterator[SwaggerPetstorePet]: Iterator over parsed pets.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/swagger_petstore_pet.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@staticmethod
def from_(response: Response) -> Iterator['SwaggerPetstorePet']:
    """
    Parses a response and yields SwaggerPetstorePet objects for each valid pet entry.

    Args:
        response (Response): The HTTP response containing pet data.
    Returns:
        Iterator[SwaggerPetstorePet]: Iterator over parsed pets.
    """
    return (
        SwaggerPetstorePet(name=pet["name"], status=pet["status"])
        for pet in response.json()
        if "name" in pet and "status" in pet
    )

random(range=range(1)) staticmethod

Generates a random SwaggerPetstorePet with a unique name and 'available' status.

Returns:

Name Type Description
SwaggerPetstorePet Iterator['SwaggerPetstorePet']

The generated pet.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/swagger_petstore_pet.py
24
25
26
27
28
29
30
31
32
33
@staticmethod
def random(range: range = range(1)) -> Iterator['SwaggerPetstorePet']:
    """
    Generates a random SwaggerPetstorePet with a unique name and 'available' status.

    Returns:
        SwaggerPetstorePet: The generated pet.
    """
    for _ in range:
        yield SwaggerPetstorePet(name=str(uuid4()), status="available")

SwaggerPetstoreSteps

Bases: RestSteps[TConfiguration]

BDD-style step definitions for Swagger Petstore API operations.

Type Parameters

TConfiguration: The configuration type, must be a SwaggerPetstoreConfiguration.

Source code in qa-pytest-examples/src/qa_pytest_examples/swagger_petstore_steps.py
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
class SwaggerPetstoreSteps[TConfiguration: SwaggerPetstoreConfiguration](
        RestSteps[TConfiguration]):
    """
    BDD-style step definitions for Swagger Petstore API operations.

    Type Parameters:
        TConfiguration: The configuration type, must be a SwaggerPetstoreConfiguration.
    """

    @Context.traced
    def swagger_petstore(self, client: requests.Session):
        """
        Sets the REST session to use for subsequent steps.

        Args:
            client (requests.Session): The HTTP client session.
        Returns:
            Self: The current step instance for chaining.
        """
        self._rest_session = client
        return self

    @Context.traced
    def adding(self, pet: SwaggerPetstorePet) -> Self:
        """
        Adds a pet to the Swagger Petstore via the API.

        Args:
            pet (SwaggerPetstorePet): The pet to add.
        Returns:
            Self: The current step instance for chaining.
        """
        return self.invoking(Request(
            method=HttpMethod.POST,
            url=self.configured.resource_uri(path="pet"),
            json=asdict(pet)
        ))

    @Context.traced
    def the_available_pets(self, by_rule: Matcher
                           [Iterator[SwaggerPetstorePet]]) -> Self:
        """
        Asserts that the available pets match the provided matcher rule.

        Args:
            by_rule (Matcher[Iterator[SwaggerPetstorePet]]): Matcher for the available pets iterator.
        Returns:
            Self: The current step instance for chaining.
        """
        return self.the_invocation(Request(
            method=HttpMethod.GET,
            url=self.configured.resource_uri(path="pet/findByStatus"),
            params={"status": "available"}),
            adapted_object(
                lambda response: SwaggerPetstorePet.from_(response),
                by_rule))

adding(pet)

Adds a pet to the Swagger Petstore via the API.

Parameters:

Name Type Description Default
pet SwaggerPetstorePet

The pet to add.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/swagger_petstore_steps.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@Context.traced
def adding(self, pet: SwaggerPetstorePet) -> Self:
    """
    Adds a pet to the Swagger Petstore via the API.

    Args:
        pet (SwaggerPetstorePet): The pet to add.
    Returns:
        Self: The current step instance for chaining.
    """
    return self.invoking(Request(
        method=HttpMethod.POST,
        url=self.configured.resource_uri(path="pet"),
        json=asdict(pet)
    ))

swagger_petstore(client)

Sets the REST session to use for subsequent steps.

Parameters:

Name Type Description Default
client Session

The HTTP client session.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/swagger_petstore_steps.py
29
30
31
32
33
34
35
36
37
38
39
40
@Context.traced
def swagger_petstore(self, client: requests.Session):
    """
    Sets the REST session to use for subsequent steps.

    Args:
        client (requests.Session): The HTTP client session.
    Returns:
        Self: The current step instance for chaining.
    """
    self._rest_session = client
    return self

the_available_pets(by_rule)

Asserts that the available pets match the provided matcher rule.

Parameters:

Name Type Description Default
by_rule Matcher[Iterator[SwaggerPetstorePet]]

Matcher for the available pets iterator.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/swagger_petstore_steps.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@Context.traced
def the_available_pets(self, by_rule: Matcher
                       [Iterator[SwaggerPetstorePet]]) -> Self:
    """
    Asserts that the available pets match the provided matcher rule.

    Args:
        by_rule (Matcher[Iterator[SwaggerPetstorePet]]): Matcher for the available pets iterator.
    Returns:
        Self: The current step instance for chaining.
    """
    return self.the_invocation(Request(
        method=HttpMethod.GET,
        url=self.configured.resource_uri(path="pet/findByStatus"),
        params={"status": "available"}),
        adapted_object(
            lambda response: SwaggerPetstorePet.from_(response),
            by_rule))

TerminalXConfiguration

Bases: SeleniumConfiguration

Configuration for TerminalX Selenium-based tests. Provides access to users and random user selection.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_configuration.py
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
class TerminalXConfiguration(SeleniumConfiguration):
    """
    Configuration for TerminalX Selenium-based tests.
    Provides access to users and random user selection.
    """

    @cached_property
    @final
    def users(self) -> List[TerminalXUser]:
        """
        Returns the list of TerminalX users from the configuration parser.

        Returns:
            List[TerminalXUser]: The list of users.
        """
        users_section = self.parser["users"]
        return [
            TerminalXUser(TerminalXCredentials.from_(
                username_password), name=key)
            for key, username_password in users_section.items()
        ]

    @final
    @property
    def random_user(self) -> TerminalXUser:
        """
        Returns a random user from the list of users.

        Returns:
            TerminalXUser: A randomly selected user.
        """
        return random.choice(self.users)

random_user property

Returns a random user from the list of users.

Returns:

Name Type Description
TerminalXUser TerminalXUser

A randomly selected user.

users cached property

Returns the list of TerminalX users from the configuration parser.

Returns:

Type Description
List[TerminalXUser]

List[TerminalXUser]: The list of users.

TerminalXCredentials dataclass

Bases: Credentials

Credentials for TerminalX users. Inherits from Credentials.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/terminalx_credentials.py
10
11
12
13
14
15
16
@dataclass(frozen=True)
class TerminalXCredentials(Credentials):
    """
    Credentials for TerminalX users.
    Inherits from Credentials.
    """
    pass

__init__(username, password)

TerminalXSteps

Bases: SeleniumSteps[TConfiguration]

BDD-style step definitions for TerminalX UI operations using Selenium.

Type Parameters

TConfiguration: The configuration type, must be a TerminalXConfiguration.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
 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
class TerminalXSteps[TConfiguration: TerminalXConfiguration](
        SeleniumSteps[TConfiguration]):
    """
    BDD-style step definitions for TerminalX UI operations using Selenium.

    Type Parameters:
        TConfiguration: The configuration type, must be a TerminalXConfiguration.
    """
    @Context.traced
    def terminalx(self, driver: WebDriver) -> Self:
        """
        Sets the Selenium WebDriver and navigates to the landing page.

        Args:
            driver (WebDriver): The Selenium WebDriver instance.
        Returns:
            Self: The current step instance for chaining.
        """
        self._web_driver = driver
        self._web_driver.get(self.configured.landing_page)
        return self

    def clicking_login(self) -> Self:
        """
        Clicks the login button on the TerminalX landing page.

        Returns:
            Self: The current step instance for chaining.
        """
        return self.clicking(By.xpath("//div[contains(text(), 'התחברות')]"))

    @Context.traced
    def clicking_search(self) -> Self:
        """
        Clicks the search button in the TerminalX header.

        Returns:
            Self: The current step instance for chaining.
        """
        return self.clicking(
            By.xpath("//button[@data-test-id='qa-header-search-button']"))

    def submitting_login(self) -> Self:
        """
        Clicks the submit button on the login form.

        Returns:
            Self: The current step instance for chaining.
        """
        return self.clicking(By.xpath("//button[contains(text(), 'התחברות')]"))

    @Context.traced
    def logging_in_with(self, credentials: TerminalXCredentials) -> Self:
        """
        Logs in using the provided credentials.

        Args:
            credentials (TerminalXCredentials): The credentials to use for login.
        Returns:
            Self: The current step instance for chaining.
        """
        return (self.clicking_login()
                .and_.typing(
                    By.id("qa-login-email-input"), credentials.username)
                .and_.typing(
                    By.id("qa-login-password-input"), credentials.password)
                .and_.submitting_login())

    @Context.traced
    def the_user_logged_in(self, by_rule: Matcher[str]) -> Self:
        """
        Asserts that the user is logged in by checking the profile button text.

        Args:
            by_rule (Matcher[str]): Matcher for the user name text.
        Returns:
            Self: The current step instance for chaining.
        """
        return self.the_element(
            By.xpath(
                "//button[@data-test-id='qa-header-profile-button']/span[2]"),
            adapted_object(lambda element: element.text, by_rule))

    @Context.traced
    def searching_for(self, text: str) -> Self:
        """
        Types the given text into the search box.

        Args:
            text (str): The text to search for.
        Returns:
            Self: The current step instance for chaining.
        """
        return self.typing(
            By.xpath("//input[@data-test-id='qa-search-box-input']"),
            text)

    @Context.traced
    def the_search_hints(self, by_rule: Matcher[Iterator[str]]) -> Self:
        """
        Asserts that the search hints match the provided matcher rule.

        Args:
            by_rule (Matcher[Iterator[str]]): Matcher for the search hints iterator.
        Returns:
            Self: The current step instance for chaining.
        """
        return self.the_elements(
            By.xpath("(//ul[@class='list_3tWy'])[2]/li/div/div/a"),
            adapted_iterator(lambda element: element.text, by_rule))

clicking_login()

Clicks the login button on the TerminalX landing page.

Returns:

Name Type Description
Self Self

The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
38
39
40
41
42
43
44
45
def clicking_login(self) -> Self:
    """
    Clicks the login button on the TerminalX landing page.

    Returns:
        Self: The current step instance for chaining.
    """
    return self.clicking(By.xpath("//div[contains(text(), 'התחברות')]"))

Clicks the search button in the TerminalX header.

Returns:

Name Type Description
Self Self

The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
47
48
49
50
51
52
53
54
55
56
@Context.traced
def clicking_search(self) -> Self:
    """
    Clicks the search button in the TerminalX header.

    Returns:
        Self: The current step instance for chaining.
    """
    return self.clicking(
        By.xpath("//button[@data-test-id='qa-header-search-button']"))

logging_in_with(credentials)

Logs in using the provided credentials.

Parameters:

Name Type Description Default
credentials TerminalXCredentials

The credentials to use for login.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@Context.traced
def logging_in_with(self, credentials: TerminalXCredentials) -> Self:
    """
    Logs in using the provided credentials.

    Args:
        credentials (TerminalXCredentials): The credentials to use for login.
    Returns:
        Self: The current step instance for chaining.
    """
    return (self.clicking_login()
            .and_.typing(
                By.id("qa-login-email-input"), credentials.username)
            .and_.typing(
                By.id("qa-login-password-input"), credentials.password)
            .and_.submitting_login())

searching_for(text)

Types the given text into the search box.

Parameters:

Name Type Description Default
text str

The text to search for.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
@Context.traced
def searching_for(self, text: str) -> Self:
    """
    Types the given text into the search box.

    Args:
        text (str): The text to search for.
    Returns:
        Self: The current step instance for chaining.
    """
    return self.typing(
        By.xpath("//input[@data-test-id='qa-search-box-input']"),
        text)

submitting_login()

Clicks the submit button on the login form.

Returns:

Name Type Description
Self Self

The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
58
59
60
61
62
63
64
65
def submitting_login(self) -> Self:
    """
    Clicks the submit button on the login form.

    Returns:
        Self: The current step instance for chaining.
    """
    return self.clicking(By.xpath("//button[contains(text(), 'התחברות')]"))

terminalx(driver)

Sets the Selenium WebDriver and navigates to the landing page.

Parameters:

Name Type Description Default
driver WebDriver

The Selenium WebDriver instance.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
24
25
26
27
28
29
30
31
32
33
34
35
36
@Context.traced
def terminalx(self, driver: WebDriver) -> Self:
    """
    Sets the Selenium WebDriver and navigates to the landing page.

    Args:
        driver (WebDriver): The Selenium WebDriver instance.
    Returns:
        Self: The current step instance for chaining.
    """
    self._web_driver = driver
    self._web_driver.get(self.configured.landing_page)
    return self

the_search_hints(by_rule)

Asserts that the search hints match the provided matcher rule.

Parameters:

Name Type Description Default
by_rule Matcher[Iterator[str]]

Matcher for the search hints iterator.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
113
114
115
116
117
118
119
120
121
122
123
124
125
@Context.traced
def the_search_hints(self, by_rule: Matcher[Iterator[str]]) -> Self:
    """
    Asserts that the search hints match the provided matcher rule.

    Args:
        by_rule (Matcher[Iterator[str]]): Matcher for the search hints iterator.
    Returns:
        Self: The current step instance for chaining.
    """
    return self.the_elements(
        By.xpath("(//ul[@class='list_3tWy'])[2]/li/div/div/a"),
        adapted_iterator(lambda element: element.text, by_rule))

the_user_logged_in(by_rule)

Asserts that the user is logged in by checking the profile button text.

Parameters:

Name Type Description Default
by_rule Matcher[str]

Matcher for the user name text.

required

Returns: Self: The current step instance for chaining.

Source code in qa-pytest-examples/src/qa_pytest_examples/terminalx_steps.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@Context.traced
def the_user_logged_in(self, by_rule: Matcher[str]) -> Self:
    """
    Asserts that the user is logged in by checking the profile button text.

    Args:
        by_rule (Matcher[str]): Matcher for the user name text.
    Returns:
        Self: The current step instance for chaining.
    """
    return self.the_element(
        By.xpath(
            "//button[@data-test-id='qa-header-profile-button']/span[2]"),
        adapted_object(lambda element: element.text, by_rule))

TerminalXUser dataclass

Represents a TerminalX user with credentials and a display name.

Source code in qa-pytest-examples/src/qa_pytest_examples/model/terminalx_user.py
10
11
12
13
14
15
16
17
@dataclass(frozen=True)
class TerminalXUser:
    """
    Represents a TerminalX user with credentials and a display name.
    """

    credentials: TerminalXCredentials
    name: str

credentials instance-attribute

name instance-attribute

__init__(credentials, name)