-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_generated_model.py
More file actions
210 lines (169 loc) · 5.73 KB
/
example_generated_model.py
File metadata and controls
210 lines (169 loc) · 5.73 KB
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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
Example of a generated PyOsmo model for website testing.
This is a demonstration of what the model creator generates.
Base URL: https://example.com
Generated: Example model for demonstration
"""
try:
import requests
except ImportError:
raise ImportError('Model requires requests library. Install with: pip install requests')
from pyosmo import Osmo
from pyosmo.decorators import guard, step
class ExampleWebsiteModel:
"""
Model-based testing for https://example.com
This is an example generated model showing the structure and
patterns used by the PyOsmo model creator.
"""
def __init__(self):
"""Initialize the model state."""
self.base_url = 'https://example.com'
self.session = requests.Session()
self.logged_in = False
self.current_user: str | None = None
self.current_page = 'home'
self.response = None
self.last_error: str | None = None
def before_test(self):
"""Called before each test run."""
self.session = requests.Session()
self.logged_in = False
self.current_page = 'home'
self.current_user = None
self.last_error = None
print('Starting new test run')
def after_test(self):
"""Called after each test run."""
self.session.close()
print('Test run completed')
def after(self):
"""Called after each step - verification point."""
if self.response is not None:
# Verify response is valid
assert self.response.status_code < 500, f'Server error: {self.response.status_code}'
# Could add more sophisticated error checking here
if self.response.status_code == 200:
pass
# --- Form Submission Actions ---
@step
@guard(lambda self: not self.logged_in)
def submit_login(self):
"""Submit the login form."""
data = {
'username': 'testuser',
'password': 'testpassword123',
}
self.response = self.session.post(
'https://example.com/auth/login',
data=data,
)
# Update login state on success
if self.response.status_code == 200:
self.logged_in = True
self.current_user = data.get('username')
self.current_page = 'login'
print('Executed: submit_login')
@step
@guard(lambda self: not self.logged_in)
def submit_register(self):
"""Submit the registration form."""
data = {
'username': 'testuser',
'email': 'test@example.com',
'password': 'testpassword123',
'confirm_password': 'testpassword123',
}
self.response = self.session.post(
'https://example.com/auth/register',
data=data,
)
self.current_page = 'register'
print('Executed: submit_register')
@step
@guard(lambda self: self.logged_in)
def submit_logout(self):
"""Submit the logout form."""
data = {}
self.response = self.session.post(
'https://example.com/auth/logout',
data=data,
)
# Update logout state
self.logged_in = False
self.current_user = None
self.current_page = 'logout'
print('Executed: submit_logout')
@step
def submit_contact(self):
"""Submit the contact form."""
data = {
'name': 'Test User',
'email': 'test@example.com',
'message': 'test_message',
}
self.response = self.session.post(
'https://example.com/contact',
data=data,
)
self.current_page = 'contact'
print('Executed: submit_contact')
@step
def submit_search(self):
"""Submit the search form."""
data = {
'q': 'test_q',
}
self.response = self.session.get(
'https://example.com/search',
params=data,
)
self.current_page = 'search'
print('Executed: submit_search')
# --- Navigation Actions ---
@step
def navigate_to_home(self):
"""Navigate to home."""
self.response = self.session.get('https://example.com')
self.current_page = 'home'
print('Navigated to: home')
@step
def navigate_to_about(self):
"""Navigate to about."""
self.response = self.session.get('https://example.com/about')
self.current_page = 'about'
print('Navigated to: about')
@step
def navigate_to_contact(self):
"""Navigate to contact."""
self.response = self.session.get('https://example.com/contact')
self.current_page = 'contact'
print('Navigated to: contact')
@step
@guard(lambda self: not self.logged_in)
def navigate_to_login(self):
"""Navigate to login."""
self.response = self.session.get('https://example.com/login')
self.current_page = 'login'
print('Navigated to: login')
@step
@guard(lambda self: self.logged_in)
def navigate_to_dashboard(self):
"""Navigate to dashboard."""
self.response = self.session.get('https://example.com/dashboard')
self.current_page = 'dashboard'
print('Navigated to: dashboard')
def main():
"""Run the model with PyOsmo."""
model = ExampleWebsiteModel()
osmo = Osmo(model).weighted_algorithm().stop_after_steps(100).run_tests(1)
print(f'Starting test generation for {model.base_url}')
osmo.generate()
# Print summary
stats = osmo.history.statistics()
print('\n' + '=' * 50)
print('Test generation complete!')
print(f'Steps executed: {stats.total_steps}')
print(f'Unique steps: {len(stats.step_coverage)}')
if __name__ == '__main__':
main()