@@ -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.
180195TITLE: changing the property
181196ACTION
182197const data = {
183198 color: window.getComputedStyle($0).color
184199}
185200STOP` ,
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.
194213TITLE: changing the property
195214ACTION
196215const originalWidth = $0.style.width;
@@ -204,14 +223,18 @@ const data = {
204223$0.style.width = originalWidth; // Restore original width
205224$0.style.height = originalHeight;
206225STOP` ,
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.
215238TITLE: changing the property
216239ACTION
217240const 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
227250STOP` ,
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.
236262TITLE: changing the property
237263ACTION
238264await setElementStyles($0, { 'background-color': 'blue' });
239265await setElementStyles($0.parentElement, { 'background-color': 'green' });
240266STOP` ,
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} ) ;
0 commit comments