|
5 | 5 | * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { By, Setting, SettingsEditor } from 'vscode-extension-tester'; |
| 8 | +import { By, Setting, SettingsEditor, WebElement } from 'vscode-extension-tester'; |
9 | 9 | import { executeQuickPick } from '../ui-interaction/commandPrompt'; |
10 | 10 | import { Duration, findElementByText, log, pause } from '../core/miscellaneous'; |
11 | 11 | import { getBrowser } from '../ui-interaction/workbench'; |
@@ -145,16 +145,80 @@ export async function isBooleanSettingEnabled( |
145 | 145 | } |
146 | 146 |
|
147 | 147 | /** |
148 | | - * Sets the value of a specific setting in the settings editor. |
149 | | - * |
| 148 | + * Sets the value of a specified setting in VSCode. |
150 | 149 | * @param id - The unique identifier of the setting to be updated. |
151 | 150 | * @param value - The new value to set for the specified setting. |
152 | 151 | * @param isWorkspace - True if the setting is a workspace setting; false if it's a user setting. |
153 | 152 | * @returns A promise that resolves when the setting value has been updated. |
154 | 153 | */ |
155 | 154 | export const setSettingValue = async (id: string, value: string | boolean, isWorkspace: boolean): Promise<void> => { |
156 | 155 | await (isWorkspace ? inWorkspaceSettings() : inUserSettings()); |
157 | | - const settingsEditor = new SettingsEditor(); |
158 | | - const logLevelSetting = await settingsEditor.findSettingByID(id); |
159 | | - await logLevelSetting?.setValue(value); |
| 156 | + try { |
| 157 | + // Try the original implementation using RedHat page objects |
| 158 | + const settingsEditor = new SettingsEditor(); |
| 159 | + const logLevelSetting = await settingsEditor.findSettingByID(id); |
| 160 | + await logLevelSetting?.setValue(value); |
| 161 | + } catch (error) { |
| 162 | + // Fallback for VS Code 1.90.0+ where .native-edit-context selector doesn't exist |
| 163 | + log(`Primary findSettingByID failed, using fallback implementation for VS Code 1.90.0+: ${error}`); |
| 164 | + |
| 165 | + try { |
| 166 | + const browser = getBrowser(); |
| 167 | + |
| 168 | + // Alternative selectors for the search input in newer VS Code versions |
| 169 | + const searchSelectors = [ |
| 170 | + '.inputarea.monaco-mouse-cursor-text', |
| 171 | + 'input[placeholder*="Search settings"]', |
| 172 | + ]; |
| 173 | + |
| 174 | + let searchInput: WebElement | undefined; |
| 175 | + for (const selector of searchSelectors) { |
| 176 | + try { |
| 177 | + searchInput = await browser.findElement(By.css(selector)); |
| 178 | + break; |
| 179 | + } catch { |
| 180 | + // Try next selector |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if (!searchInput) { |
| 185 | + throw new Error('Could not find settings search input with any known selector'); |
| 186 | + } |
| 187 | + |
| 188 | + // Clear and search for the setting |
| 189 | + await searchInput.clear(); |
| 190 | + await searchInput.sendKeys(id); |
| 191 | + await pause(Duration.seconds(1)); |
| 192 | + |
| 193 | + // Find the setting by its title (last part of the ID) |
| 194 | + const title = id.split('.').pop(); |
| 195 | + const settingElement = await findElementByText('div', 'textContent', title); |
| 196 | + |
| 197 | + if (!settingElement) { |
| 198 | + throw new Error(`Could not find setting element for: ${title}`); |
| 199 | + } |
| 200 | + |
| 201 | + // Find the input/control within the setting element and set the value |
| 202 | + const parent = await settingElement.findElement(By.xpath('..')); |
| 203 | + const settingContainer = await parent.findElement(By.xpath('..')); |
| 204 | + |
| 205 | + if (typeof value === 'boolean') { |
| 206 | + const checkbox = await settingContainer.findElement(By.css('input[type="checkbox"]')); |
| 207 | + const isChecked = await checkbox.isSelected(); |
| 208 | + if (isChecked !== value) { |
| 209 | + await checkbox.click(); |
| 210 | + } |
| 211 | + } else { |
| 212 | + const textInput = await settingContainer.findElement(By.css('input[type="text"], textarea')); |
| 213 | + await textInput.clear(); |
| 214 | + await textInput.sendKeys(value.toString()); |
| 215 | + } |
| 216 | + |
| 217 | + log(`Successfully set setting ${id} to ${value} using fallback implementation`); |
| 218 | + |
| 219 | + } catch (fallbackError) { |
| 220 | + log(`Both primary and fallback implementations failed: ${fallbackError}`); |
| 221 | + throw new Error(`Failed to set setting ${id}: ${fallbackError}`); |
| 222 | + } |
| 223 | + } |
160 | 224 | }; |
0 commit comments