forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_settings.py
More file actions
294 lines (253 loc) Β· 14.5 KB
/
Copy pathtest_settings.py
File metadata and controls
294 lines (253 loc) Β· 14.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
E2E: Settings configuration test (GitHub token)
This test navigates to OpenHands, configures the LLM API key if prompted,
then ensures the GitHub token is set in Settings β Integrations and that the
home screen shows the repository selector.
"""
import os
from playwright.sync_api import Page, expect
def test_github_token_configuration(page: Page, base_url: str):
"""
Test the GitHub token configuration flow:
1. Navigate to OpenHands
2. Configure LLM API key if needed
3. Check if GitHub token is already configured
4. If not, navigate to settings and configure it
5. Verify the token is saved and repository selection is available
"""
# Create test-results directory if it doesn't exist
os.makedirs('test-results', exist_ok=True)
# Use default URL if base_url is not provided
if not base_url:
base_url = 'http://localhost:12000'
# Navigate to the OpenHands application
print(f'Step 1: Navigating to OpenHands application at {base_url}...')
page.goto(base_url)
page.wait_for_load_state('networkidle', timeout=30000)
# Take initial screenshot
page.screenshot(path='test-results/token_01_initial_load.png')
print('Screenshot saved: token_01_initial_load.png')
# Step 1.5: Handle any initial modals that might appear (LLM API key configuration)
try:
# Check for AI Provider Configuration modal
config_modal = page.locator('text=AI Provider Configuration')
if config_modal.is_visible(timeout=5000):
print('AI Provider Configuration modal detected')
# Fill in the LLM API key if available
llm_api_key_input = page.locator('[data-testid="llm-api-key-input"]')
if llm_api_key_input.is_visible(timeout=3000):
llm_api_key = os.getenv('LLM_API_KEY', 'test-key')
llm_api_key_input.fill(llm_api_key)
print(f'Filled LLM API key (length: {len(llm_api_key)})')
# Click the Save button
save_button = page.locator('button:has-text("Save")')
if save_button.is_visible(timeout=3000):
save_button.click()
page.wait_for_timeout(2000)
print('Saved LLM API key configuration')
# Check for Privacy Preferences modal
privacy_modal = page.locator('text=Your Privacy Preferences')
if privacy_modal.is_visible(timeout=5000):
print('Privacy Preferences modal detected')
confirm_button = page.locator('button:has-text("Confirm Preferences")')
if confirm_button.is_visible(timeout=3000):
confirm_button.click()
page.wait_for_timeout(2000)
print('Confirmed privacy preferences')
except Exception as e:
print(f'Error handling initial modals: {e}')
page.screenshot(path='test-results/token_01_5_modal_error.png')
print('Screenshot saved: token_01_5_modal_error.png')
# Step 2: Check if GitHub token is already configured or needs to be set
print('Step 2: Checking if GitHub token is configured...')
try:
# First, check if we're already on the home screen with repository selection
# This means the GitHub token is already configured in ~/.openhands/settings.json
connect_to_provider = page.locator('text=Connect to a Repository')
if connect_to_provider.is_visible(timeout=3000):
print('Found "Connect to a Repository" section')
# Check if we need to configure a provider (GitHub token)
navigate_to_settings_button = page.locator(
'[data-testid="navigate-to-settings-button"]'
)
if navigate_to_settings_button.is_visible(timeout=3000):
print('GitHub token not configured. Need to navigate to settings...')
# Click the Settings button to navigate to the settings page
navigate_to_settings_button.click()
page.wait_for_load_state('networkidle', timeout=10000)
page.wait_for_timeout(3000) # Wait for navigation to complete
# We should now be on the /settings/integrations page
print('Navigated to settings page, looking for GitHub token input...')
# Check if we're on the settings page with the integrations tab
settings_screen = page.locator('[data-testid="settings-screen"]')
if settings_screen.is_visible(timeout=5000):
print('Settings screen is visible')
# Make sure we're on the Integrations tab
integrations_tab = page.locator('text=Integrations')
if integrations_tab.is_visible(timeout=3000):
# Check if we need to click the tab
if not page.url.endswith('/settings/integrations'):
print('Clicking Integrations tab...')
integrations_tab.click()
page.wait_for_load_state('networkidle')
page.wait_for_timeout(2000)
# Now look for the GitHub token input
github_token_input = page.locator(
'[data-testid="github-token-input"]'
)
if github_token_input.is_visible(timeout=5000):
print('Found GitHub token input field')
# Fill in the GitHub token from environment variable
github_token = os.getenv('GITHUB_TOKEN', '')
if github_token:
# Clear the field first, then fill it
github_token_input.clear()
github_token_input.fill(github_token)
print(
f'Filled GitHub token from environment variable (length: {len(github_token)})'
)
# Verify the token was filled
filled_value = github_token_input.input_value()
if filled_value:
print(
f'Token field now contains value of length: {len(filled_value)}'
)
else:
print(
'WARNING: Token field appears to be empty after filling'
)
# Look for the Save Changes button and ensure it's enabled
save_button = page.locator('[data-testid="submit-button"]')
if save_button.is_visible(timeout=3000):
# Check if button is enabled
is_disabled = save_button.is_disabled()
print(
f'Save Changes button found, disabled: {is_disabled}'
)
if not is_disabled:
print('Clicking Save Changes button...')
save_button.click()
# Wait for the save operation to complete
try:
# Wait for the button to show "Saving..." (if it does)
page.wait_for_timeout(1000)
# Wait for the save to complete - button should be disabled again
page.wait_for_function(
'document.querySelector(\'[data-testid="submit-button"]\').disabled === true',
timeout=10000,
)
print(
'Save operation completed - form is now clean'
)
except Exception:
print(
'Save operation completed (timeout waiting for form clean state)'
)
# Navigate back to home page after successful save
print('Navigating back to home page...')
page.goto(base_url)
page.wait_for_load_state('networkidle')
page.wait_for_timeout(
5000
) # Wait longer for providers to be updated
else:
print(
'Save Changes button is disabled - form may be invalid'
)
else:
print('Save Changes button not found')
else:
print('No GitHub token found in environment variables')
else:
print('GitHub token input field not found on settings page')
# Take a screenshot to see what's on the page
page.screenshot(path='test-results/token_02_settings_debug.png')
print('Debug screenshot saved: token_02_settings_debug.png')
else:
print('Settings screen not found')
else:
# Branch 2: GitHub token is already configured, repository selection is available
print(
'GitHub token is already configured, repository selection is available'
)
# Check if we need to update the token by going to settings manually
settings_button = page.locator('button:has-text("Settings")')
if settings_button.is_visible(timeout=3000):
print(
'Settings button found, clicking to navigate to settings page...'
)
settings_button.click()
page.wait_for_load_state('networkidle', timeout=10000)
page.wait_for_timeout(3000) # Wait for navigation to complete
# Navigate to the Integrations tab
integrations_tab = page.locator('text=Integrations')
if integrations_tab.is_visible(timeout=3000):
print('Clicking Integrations tab...')
integrations_tab.click()
page.wait_for_load_state('networkidle')
page.wait_for_timeout(2000)
# Now look for the GitHub token input
github_token_input = page.locator(
'[data-testid="github-token-input"]'
)
if github_token_input.is_visible(timeout=5000):
print('Found GitHub token input field')
# Fill in the GitHub token from environment variable
github_token = os.getenv('GITHUB_TOKEN', '')
if github_token:
# Clear the field first, then fill it
github_token_input.clear()
github_token_input.fill(github_token)
print(
f'Filled GitHub token from environment variable (length: {len(github_token)})'
)
# Look for the Save Changes button and ensure it's enabled
save_button = page.locator(
'[data-testid="submit-button"]'
)
if (
save_button.is_visible(timeout=3000)
and not save_button.is_disabled()
):
print('Clicking Save Changes button...')
save_button.click()
page.wait_for_timeout(3000)
# Navigate back to home page
print('Navigating back to home page...')
page.goto(base_url)
page.wait_for_load_state('networkidle')
page.wait_for_timeout(3000)
else:
print(
'GitHub token input field not found, going back to home page'
)
page.goto(base_url)
page.wait_for_load_state('networkidle')
else:
print('Integrations tab not found, going back to home page')
page.goto(base_url)
page.wait_for_load_state('networkidle')
else:
print('Settings button not found, continuing with existing token')
else:
print('Could not find "Connect to a Repository" section')
page.screenshot(path='test-results/token_03_after_settings.png')
print('Screenshot saved: token_03_after_settings.png')
except Exception as e:
print(f'Error checking GitHub token configuration: {e}')
page.screenshot(path='test-results/token_04_error.png')
print('Screenshot saved: token_04_error.png')
# Step 3: Verify we're back on the home screen with repository selection available
print('Step 3: Verifying repository selection is available...')
# Wait for the home screen to load
home_screen = page.locator('[data-testid="home-screen"]')
expect(home_screen).to_be_visible(timeout=15000)
print('Home screen is visible')
# Look for the repository dropdown/selector
repo_dropdown = page.locator('[data-testid="repo-dropdown"]')
expect(repo_dropdown).to_be_visible(timeout=15000)
print('Repository dropdown is visible')
# Success - we've verified the GitHub token configuration
print('GitHub token configuration verified successfully')
page.screenshot(path='test-results/token_05_success.png')
print('Screenshot saved: token_05_success.png')