Skip to content

Commit 247400d

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[AI Assistance] Add test for correct iframe
Fixed: 368523861 Change-Id: I8258e512df4c579d4f066f4e1689af9b8aceb675 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6039219 Commit-Queue: Nikolay Vitkov <[email protected]> Reviewed-by: Ergün Erdoğmuş <[email protected]> Auto-Submit: Nikolay Vitkov <[email protected]>
1 parent c04b3dd commit 247400d

File tree

5 files changed

+110
-21
lines changed

5 files changed

+110
-21
lines changed

test/e2e/freestyler/freestyler_test.ts

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@ describe('Freestyler', function() {
8383
}, messages);
8484
}
8585

86-
async function inspectNode(selector: string): Promise<void> {
86+
async function inspectNode(selector: string, iframeId?: string): Promise<void> {
8787
const {frontend} = getBrowserAndPages();
8888
await click(CONSOLE_TAB_SELECTOR);
8989
await frontend.locator('aria/Console prompt').click();
90-
await frontend.keyboard.type(`inspect(document.querySelector(${JSON.stringify(selector)}))`);
90+
let inspectText = `inspect(document.querySelector(${JSON.stringify(selector)}))`;
91+
if (iframeId) {
92+
inspectText = `inspect(document.querySelector('iframe#${iframeId}').contentDocument.querySelector((${
93+
JSON.stringify(selector)})))`;
94+
}
95+
await frontend.keyboard.type(inspectText);
9196
await frontend.keyboard.press('Enter');
9297
}
9398

@@ -155,7 +160,15 @@ describe('Freestyler', function() {
155160
})) as Array<Log>;
156161
}
157162

158-
async function runWithMessages(query: string, messages: string[]): Promise<Array<Log>> {
163+
async function runAiAssistance(options: {
164+
query: string,
165+
messages: string[],
166+
resource?: string,
167+
node?: string,
168+
iframeId?: string,
169+
}) {
170+
const {messages, query, resource = '../resources/recorder/recorder.html', node = 'div', iframeId} = options;
171+
159172
await setupMocks(
160173
{
161174
aidaAvailability: {},
@@ -164,9 +177,9 @@ describe('Freestyler', function() {
164177
},
165178
},
166179
messages);
167-
await goToResource('../resources/recorder/recorder.html');
180+
await goToResource(resource);
168181

169-
await inspectNode('div');
182+
await inspectNode(node, iframeId);
170183
await openFreestyler();
171184
await turnOnAiAssistance();
172185
await enableDebugModeForFreestyler();
@@ -175,22 +188,28 @@ describe('Freestyler', function() {
175188
}
176189

177190
it('gets data about elements', async () => {
178-
const result = await runWithMessages('Change the background color for this element to blue', [
179-
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
191+
const result = await runAiAssistance({
192+
query: 'Change the background color for this element to blue',
193+
messages: [
194+
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
180195
TITLE: changing the property
181196
ACTION
182197
const data = {
183198
color: window.getComputedStyle($0).color
184199
}
185200
STOP`,
186-
'ANSWER: changed styles',
187-
]);
201+
'ANSWER: changed styles',
202+
],
203+
});
188204
assert.deepStrictEqual(result.at(-1)!.request.input, 'OBSERVATION: {"color":"rgb(0, 0, 0)"}');
189205
});
190206

191207
it('gets handles trailing ;', async () => {
192-
const result = await runWithMessages('Change the background color for this element to blue', [
193-
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
208+
const result = await runAiAssistance(
209+
{
210+
query: 'Change the background color for this element to blue',
211+
messages: [
212+
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
194213
TITLE: changing the property
195214
ACTION
196215
const originalWidth = $0.style.width;
@@ -204,14 +223,18 @@ const data = {
204223
$0.style.width = originalWidth; // Restore original width
205224
$0.style.height = originalHeight;
206225
STOP`,
207-
'ANSWER: changed styles',
208-
]);
226+
'ANSWER: changed styles',
227+
],
228+
},
229+
);
209230
assert.deepStrictEqual(result.at(-1)!.request.input, 'OBSERVATION: {"aspectRatio":"auto"}');
210231
});
211232

212233
it('gets handles comments', async () => {
213-
const result = await runWithMessages('Change the background color for this element to blue', [
214-
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
234+
const result = await runAiAssistance({
235+
query: 'Change the background color for this element to blue',
236+
messages: [
237+
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
215238
TITLE: changing the property
216239
ACTION
217240
const originalWidth = $0.style.width;
@@ -225,21 +248,25 @@ const data = {
225248
$0.style.width = originalWidth; // Restore original width
226249
$0.style.height = originalHeight; // Restore original height
227250
STOP`,
228-
'ANSWER: changed styles',
229-
]);
251+
'ANSWER: changed styles',
252+
],
253+
});
230254
assert.deepStrictEqual(result.at(-1)!.request.input, 'OBSERVATION: {"aspectRatio":"auto"}');
231255
});
232256

233257
it('modifies the inline styles using the extension functions', async () => {
234-
await runWithMessages('Change the background color for this element to blue', [
235-
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
258+
await runAiAssistance({
259+
query: 'Change the background color for this element to blue',
260+
messages: [
261+
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
236262
TITLE: changing the property
237263
ACTION
238264
await setElementStyles($0, { 'background-color': 'blue' });
239265
await setElementStyles($0.parentElement, { 'background-color': 'green' });
240266
STOP`,
241-
'ANSWER: changed styles',
242-
]);
267+
'ANSWER: changed styles',
268+
],
269+
});
243270

244271
const {target} = getBrowserAndPages();
245272
await target.bringToFront();
@@ -250,4 +277,31 @@ STOP`,
250277
window.getComputedStyle(document.querySelector('body')).backgroundColor === 'rgb(0, 128, 0)';
251278
});
252279
});
280+
281+
it('executes in the correct realm', async () => {
282+
const result = await runAiAssistance({
283+
query: 'What is the document title',
284+
messages: [
285+
`THOUGHT: I can get the title via web API
286+
TITLE: getting the document title
287+
ACTION
288+
289+
// TODO: Enable once this stop crashing the page
290+
// if(window.self === window.top){
291+
// throw new Error('Access from non frame')
292+
// }
293+
294+
const data = {
295+
title: document.title,
296+
};
297+
STOP`,
298+
'ANSWER: Title collected',
299+
],
300+
resource: '../resources/freestyler/index.html',
301+
node: 'div',
302+
iframeId: 'iframe',
303+
});
304+
305+
assert.deepStrictEqual(result.at(-1)!.request.input, 'OBSERVATION: {"title":"I have a title"}');
306+
});
253307
});

test/e2e/resources/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ group("resources") {
1717
"elements",
1818
"emulation",
1919
"extensions",
20+
"freestyler",
2021
"host",
2122
"inline_editor",
2223
"issues",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright 2020 The Chromium Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
import("../../../../scripts/build/ninja/copy.gni")
6+
7+
copy_to_gen("freestyler") {
8+
sources = [
9+
"iframe.html",
10+
"index.html",
11+
]
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!--
2+
Copyright 2024 The Chromium Authors. All rights reserved.
3+
Use of this source code is governed by a BSD-style license that can be
4+
found in the LICENSE file.
5+
-->
6+
<!doctype html>
7+
<head>
8+
<title>I have a title</title>
9+
</head>
10+
<body>
11+
<div>I am an iframe</div>
12+
</body>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
Copyright 2024 The Chromium Authors. All rights reserved.
3+
Use of this source code is governed by a BSD-style license that can be
4+
found in the LICENSE file.
5+
-->
6+
<!doctype html>
7+
<body>
8+
<h1>I am an top-level</h1>
9+
<iframe id="iframe" src="./iframe.html"></iframe>
10+
</body>

0 commit comments

Comments
 (0)