@redhat-developer/page-objects 1.23.0 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,6 +60,40 @@ export declare class TextEditor extends Editor {
60
60
  * @returns Promise resolving to ContentAssist object when opening, void otherwise
61
61
  */
62
62
  toggleContentAssist(open: boolean): Promise<ContentAssist | void>;
63
+ /**
64
+ * Find out whether this editor currently has VS Code keyboard focus
65
+ * @returns Promise resolving to true when the editor's text input is focused
66
+ */
67
+ hasFocus(): Promise<boolean>;
68
+ /**
69
+ * Bring VS Code keyboard focus into this editor without moving the cursor.
70
+ *
71
+ * Editor commands (select all, clipboard actions, the `:Ln,Col` go-to prompt)
72
+ * and the status bar cursor position all target whichever editor VS Code has
73
+ * focused, and the editor's zero-width `native-edit-context` input element only
74
+ * accepts keys while it is the focused element. With several editor groups open
75
+ * this makes the page object act on its own editor instance instead of on the
76
+ * group that happened to be focused before.
77
+ *
78
+ * @returns Promise resolving once the editor has focus
79
+ */
80
+ focus(): Promise<void>;
81
+ /**
82
+ * Open the command palette with this editor focused, so editor commands
83
+ * (select all, clipboard actions, the `:Ln,Col` go-to prompt) run against
84
+ * this editor instance.
85
+ *
86
+ * Focus is moved into this editor first, then the palette is opened with the
87
+ * global `Ctrl/Cmd+Shift+P` chord. `Workbench.openCommandPrompt()` cannot be
88
+ * reused here: while any webview editor is open it sends the key to the first
89
+ * active tab of whatever group, which need not be this editor's group.
90
+ */
91
+ private openCommandPrompt;
92
+ /**
93
+ * Execute a command from the command palette opened on this editor
94
+ * @param command id or title of the command
95
+ */
96
+ private executeCommand;
63
97
  /**
64
98
  * Get all text from the editor
65
99
  * @returns Promise resolving to editor text
@@ -113,6 +113,76 @@ class TextEditor extends Editor_1.Editor {
113
113
  }
114
114
  });
115
115
  }
116
+ /**
117
+ * Find out whether this editor currently has VS Code keyboard focus
118
+ * @returns Promise resolving to true when the editor's text input is focused
119
+ */
120
+ async hasFocus() {
121
+ const container = await this.findElement(TextEditor.locators.TextEditor.editorContainer);
122
+ const klass = (await container.getAttribute('class')) ?? '';
123
+ return klass.split(/\s+/).includes('focused');
124
+ }
125
+ /**
126
+ * Bring VS Code keyboard focus into this editor without moving the cursor.
127
+ *
128
+ * Editor commands (select all, clipboard actions, the `:Ln,Col` go-to prompt)
129
+ * and the status bar cursor position all target whichever editor VS Code has
130
+ * focused, and the editor's zero-width `native-edit-context` input element only
131
+ * accepts keys while it is the focused element. With several editor groups open
132
+ * this makes the page object act on its own editor instance instead of on the
133
+ * group that happened to be focused before.
134
+ *
135
+ * @returns Promise resolving once the editor has focus
136
+ */
137
+ async focus() {
138
+ if (await this.hasFocus()) {
139
+ return;
140
+ }
141
+ const inputarea = await this.findElement(TextEditor.locators.Editor.inputArea);
142
+ await this.getDriver().executeScript('arguments[0].focus()', inputarea);
143
+ await this.getWaitHelper().forCondition(() => this.hasFocus(), {
144
+ timeout: 2000,
145
+ pollInterval: 50,
146
+ message: 'Editor did not receive focus',
147
+ });
148
+ }
149
+ /**
150
+ * Open the command palette with this editor focused, so editor commands
151
+ * (select all, clipboard actions, the `:Ln,Col` go-to prompt) run against
152
+ * this editor instance.
153
+ *
154
+ * Focus is moved into this editor first, then the palette is opened with the
155
+ * global `Ctrl/Cmd+Shift+P` chord. `Workbench.openCommandPrompt()` cannot be
156
+ * reused here: while any webview editor is open it sends the key to the first
157
+ * active tab of whatever group, which need not be this editor's group.
158
+ */
159
+ async openCommandPrompt() {
160
+ await this.focus();
161
+ await this.getDriver()
162
+ .actions()
163
+ .keyDown(AbstractElement_1.AbstractElement.ctlKey)
164
+ .keyDown(selenium_webdriver_1.Key.SHIFT)
165
+ .sendKeys('p')
166
+ .keyUp(selenium_webdriver_1.Key.SHIFT)
167
+ .keyUp(AbstractElement_1.AbstractElement.ctlKey)
168
+ .perform();
169
+ return await __1.InputBox.create();
170
+ }
171
+ /**
172
+ * Execute a command from the command palette opened on this editor
173
+ * @param command id or title of the command
174
+ */
175
+ async executeCommand(command) {
176
+ const prompt = await this.openCommandPrompt();
177
+ await prompt.setText(`>${command}`);
178
+ const quickPicks = await Promise.all((await prompt.getQuickPicks()).map((item) => item.getLabel()));
179
+ if (quickPicks.includes(command)) {
180
+ await prompt.selectQuickPick(command);
181
+ }
182
+ else {
183
+ await prompt.confirm();
184
+ }
185
+ }
116
186
  /**
117
187
  * Get all text from the editor
118
188
  * @returns Promise resolving to editor text
@@ -128,12 +198,14 @@ class TextEditor extends Editor_1.Editor {
128
198
  // workaround issue https://github.com/redhat-developer/vscode-extension-tester/issues/835
129
199
  // do not fail if clipboard is empty
130
200
  }
201
+ // The status bar position, the select-all/copy commands and the cursor
202
+ // restore below all act on the focused editor: make sure it is this one.
203
+ await self.focus();
131
204
  // Store current position
132
- const [line, col] = await this.getCoordinates();
205
+ const [line, col] = await self.getCoordinates();
133
206
  // Select/copy contents
134
- const bench = new __1.Workbench();
135
- await bench.executeCommand('editor.action.selectAll');
136
- await bench.executeCommand('editor.action.clipboardCopyAction');
207
+ await self.executeCommand('editor.action.selectAll');
208
+ await self.executeCommand('editor.action.clipboardCopyAction');
137
209
  // Wait for clipboard operation to complete
138
210
  await self.getWaitHelper().forCondition(async () => {
139
211
  try {
@@ -150,10 +222,10 @@ class TextEditor extends Editor_1.Editor {
150
222
  }
151
223
  try {
152
224
  // Restore original cursor position
153
- await this.setCursor(line, col);
225
+ await self.setCursor(line, col);
154
226
  }
155
227
  catch {
156
- await this.sendKeys(selenium_webdriver_1.Key.UP);
228
+ await self.sendKeys(selenium_webdriver_1.Key.UP);
157
229
  }
158
230
  return text;
159
231
  });
@@ -174,10 +246,10 @@ class TextEditor extends Editor_1.Editor {
174
246
  // workaround issue https://github.com/redhat-developer/vscode-extension-tester/issues/835
175
247
  // do not fail if clipboard is empty
176
248
  }
249
+ await this.focus();
177
250
  const inputarea = await this.findElement(TextEditor.locators.Editor.inputArea);
178
251
  clipboard.writeSync(text);
179
- const bench = new __1.Workbench();
180
- await bench.executeCommand('editor.action.selectAll');
252
+ await this.executeCommand('editor.action.selectAll');
181
253
  await inputarea.sendKeys(selenium_webdriver_1.Key.chord(TextEditor.ctlKey, 'v'));
182
254
  if (originalClipboard.length > 0) {
183
255
  clipboard.writeSync(originalClipboard);
@@ -358,7 +430,7 @@ class TextEditor extends Editor_1.Editor {
358
430
  * @returns Promise resolving when the cursor has reached the given coordinates
359
431
  */
360
432
  async setCursor(line, column, timeout = 2_500) {
361
- const input = await new __1.Workbench().openCommandPrompt();
433
+ const input = await this.openCommandPrompt();
362
434
  await input.setText(`:${line},${column}`);
363
435
  try {
364
436
  await this.getWaitHelper().forCondition(async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redhat-developer/page-objects",
3
- "version": "1.23.0",
3
+ "version": "1.24.0",
4
4
  "description": "Page Object API implementation for a VS Code editor used by ExTester framework.",
5
5
  "main": "out/index.js",
6
6
  "types": "out/index.d.ts",
@@ -46,8 +46,8 @@
46
46
  "fs-extra": "^11.4.0"
47
47
  },
48
48
  "peerDependencies": {
49
- "selenium-webdriver": "^4.48.0",
49
+ "selenium-webdriver": "^4.49.0",
50
50
  "typescript": ">=4.6.2"
51
51
  },
52
- "gitHead": "2333228475aff111f91b91d06e50a5b98e371886"
52
+ "gitHead": "ff2802ca28fb11e04d1511e645d555cb67709eb3"
53
53
  }