Skip to content

session_

SessionStore

A singleton class for storing variables throughout a session.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
 1
 2
 3
 4
 5
 6
 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
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
class SessionStore:
    """A singleton class for storing variables throughout a session."""

    _instance = None

    def __new__(cls):
        """Ensures only one instance of SessionStore exists.

        Returns:
            SessionStore: The singleton instance.
        """
        if not cls._instance:
            cls._instance = super().__new__(cls)
            cls._instance.storage = {}
            cls._instance.reporting = {"tests": {}}
            cls._instance.current_test = None
            cls._instance.current_step = None
            cls._instance.failed_tests = set()
            cls._instance.error_messages = {}
            cls._instance.base_config = None
        return cls._instance

    def __setattr__(self, name, value):
        """Sets an attribute (session variable) on the SessionStore instance.

        Args:
            name (str): The name of the attribute (variable).
            value: The value to store.
        """
        if name == "storage":  # Protect the internal storage dictionary
            super().__setattr__(name, value)
        else:
            self.storage[name] = value

    def __getattr__(self, name):
        """Retrieves the value of an attribute (session variable).

        Args:
            name (str): The name of the attribute (variable).

        Returns:
            The value of the attribute.

        Raises:
            AttributeError: If the attribute does not exist.
        """
        return self.storage[name]

    def add_error_message(self, error_info: dict) -> None:
        """Add error message for current test.

        Args:
            error_info: Dictionary containing error details
                {
                    'message': str,  # Error message
                    'type': str,     # 'step' or 'assert'
                    'name': str,     # Step or assertion name
                    'phase': str     # test phase when error occurred
                }
        """
        if self.current_test:
            if self.current_test not in self.error_messages:
                self.error_messages[self.current_test] = []
            self.error_messages[self.current_test].append(error_info)

    def get_error_messages(self, test_id: str) -> list:
        """Get all error messages for a test."""
        return self.error_messages.get(test_id, [])

    def clear_error_messages(self, test_id: str) -> None:
        """Clear error messages for a test."""
        if test_id in self.error_messages:
            del self.error_messages[test_id]

    def mark_test_failed(self):
        """Mark current test as failed."""
        if self.current_test:
            self.failed_tests.add(self.current_test)

    def is_current_test_failed(self) -> bool:
        """Check if current test has failed."""
        return self.current_test in self.failed_tests

    def clear_current_test_status(self):
        """Clear the failure status of current test."""
        if self.current_test:
            self.failed_tests.discard(self.current_test)

__getattr__(name)

Retrieves the value of an attribute (session variable).

Parameters:

Name Type Description Default
name str

The name of the attribute (variable).

required

Returns:

Type Description

The value of the attribute.

Raises:

Type Description
AttributeError

If the attribute does not exist.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
35
36
37
38
39
40
41
42
43
44
45
46
47
def __getattr__(self, name):
    """Retrieves the value of an attribute (session variable).

    Args:
        name (str): The name of the attribute (variable).

    Returns:
        The value of the attribute.

    Raises:
        AttributeError: If the attribute does not exist.
    """
    return self.storage[name]

__new__()

Ensures only one instance of SessionStore exists.

Returns:

Name Type Description
SessionStore

The singleton instance.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def __new__(cls):
    """Ensures only one instance of SessionStore exists.

    Returns:
        SessionStore: The singleton instance.
    """
    if not cls._instance:
        cls._instance = super().__new__(cls)
        cls._instance.storage = {}
        cls._instance.reporting = {"tests": {}}
        cls._instance.current_test = None
        cls._instance.current_step = None
        cls._instance.failed_tests = set()
        cls._instance.error_messages = {}
        cls._instance.base_config = None
    return cls._instance

__setattr__(name, value)

Sets an attribute (session variable) on the SessionStore instance.

Parameters:

Name Type Description Default
name str

The name of the attribute (variable).

required
value

The value to store.

required
Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
23
24
25
26
27
28
29
30
31
32
33
def __setattr__(self, name, value):
    """Sets an attribute (session variable) on the SessionStore instance.

    Args:
        name (str): The name of the attribute (variable).
        value: The value to store.
    """
    if name == "storage":  # Protect the internal storage dictionary
        super().__setattr__(name, value)
    else:
        self.storage[name] = value

add_error_message(error_info)

Add error message for current test.

Parameters:

Name Type Description Default
error_info dict

Dictionary containing error details { 'message': str, # Error message 'type': str, # 'step' or 'assert' 'name': str, # Step or assertion name 'phase': str # test phase when error occurred }

required
Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def add_error_message(self, error_info: dict) -> None:
    """Add error message for current test.

    Args:
        error_info: Dictionary containing error details
            {
                'message': str,  # Error message
                'type': str,     # 'step' or 'assert'
                'name': str,     # Step or assertion name
                'phase': str     # test phase when error occurred
            }
    """
    if self.current_test:
        if self.current_test not in self.error_messages:
            self.error_messages[self.current_test] = []
        self.error_messages[self.current_test].append(error_info)

clear_current_test_status()

Clear the failure status of current test.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
84
85
86
87
def clear_current_test_status(self):
    """Clear the failure status of current test."""
    if self.current_test:
        self.failed_tests.discard(self.current_test)

clear_error_messages(test_id)

Clear error messages for a test.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
70
71
72
73
def clear_error_messages(self, test_id: str) -> None:
    """Clear error messages for a test."""
    if test_id in self.error_messages:
        del self.error_messages[test_id]

get_error_messages(test_id)

Get all error messages for a test.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
66
67
68
def get_error_messages(self, test_id: str) -> list:
    """Get all error messages for a test."""
    return self.error_messages.get(test_id, [])

is_current_test_failed()

Check if current test has failed.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
80
81
82
def is_current_test_failed(self) -> bool:
    """Check if current test has failed."""
    return self.current_test in self.failed_tests

mark_test_failed()

Mark current test as failed.

Source code in libs\cafex_core\src\cafex_core\singletons_\session_.py
75
76
77
78
def mark_test_failed(self):
    """Mark current test as failed."""
    if self.current_test:
        self.failed_tests.add(self.current_test)