From 8e0611a83e9715b3a950f974b03f3b40e51da671 Mon Sep 17 00:00:00 2001 From: aryangupta701 Date: Sun, 6 Aug 2023 18:09:01 +0530 Subject: [PATCH] added frame switches recording Signed-off-by: aryangupta701 --- source/ContentScript/Recorder.ts | 40 ++++++------------- source/ContentScript/index.ts | 2 +- source/Popup/index.tsx | 2 +- source/types/zestScript/ZestStatement.ts | 21 ++++++++-- test/ContentScript/integrationTests.test.ts | 20 ++++++++++ test/ContentScript/unitTests.test.ts | 12 ++++++ test/ContentScript/webpages/interactions.html | 1 + test/ContentScript/webpages/testFrame.html | 15 +++++++ 8 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 test/ContentScript/webpages/testFrame.html diff --git a/source/ContentScript/Recorder.ts b/source/ContentScript/Recorder.ts index b3a796d..148698f 100644 --- a/source/ContentScript/Recorder.ts +++ b/source/ContentScript/Recorder.ts @@ -24,6 +24,7 @@ import { ZestStatementElementClick, ZestStatementElementSendKeys, ZestStatementLaunchBrowser, + ZestStatementSwichToFrame, } from '../types/zestScript/ZestStatement'; import {getPath} from './util'; @@ -45,38 +46,23 @@ class Recorder { }); } - handleFrameSwitches(level: number, frame: number): void { - if (this.curLevel === level && this.curFrame === frame) { - // do nothing - } else if (this.curLevel > level) { + handleFrameSwitches(level: number, frameIndex: number): void { + if (this.curLevel === level && this.curFrame === frameIndex) { + return; + } + if (this.curLevel > level) { while (this.curLevel > level) { + this.sendZestScriptToZAP(new ZestStatementSwichToFrame(-1)); this.curLevel -= 1; - console.log( - 'Switched to level: ', - this.curLevel, - 'Frame:', - this.curFrame - ); - // switch to parent frame } - this.curFrame = frame; - console.log( - 'Switched to level: ', - this.curLevel, - 'Frame:', - this.curFrame - ); - // switch to frame + this.curFrame = frameIndex; } else { this.curLevel += 1; - this.curFrame = frame; - console.log( - 'Switched to level: ', - this.curLevel, - 'Frame:', - this.curFrame - ); - // switch to frame number 'frame' + this.curFrame = frameIndex; + this.sendZestScriptToZAP(new ZestStatementSwichToFrame(frameIndex)); + } + if (this.curLevel !== level) { + console.log('Error in switching frames'); } } diff --git a/source/ContentScript/index.ts b/source/ContentScript/index.ts index c2cf237..4d4133d 100644 --- a/source/ContentScript/index.ts +++ b/source/ContentScript/index.ts @@ -275,4 +275,4 @@ export { ReportedEvent, injectScript, enableExtension, -}; \ No newline at end of file +}; diff --git a/source/Popup/index.tsx b/source/Popup/index.tsx index b0bdcc2..f28d1ed 100644 --- a/source/Popup/index.tsx +++ b/source/Popup/index.tsx @@ -195,4 +195,4 @@ recordButton?.addEventListener('click', toggleRecording); configureButton?.addEventListener('click', openOptionsPage); saveScript?.addEventListener('click', handleSaveScript); scriptNameInput?.addEventListener('input', handleScriptNameChange); -windowHandleCloseInput?.addEventListener('click', handleWindowHandleClose); \ No newline at end of file +windowHandleCloseInput?.addEventListener('click', handleWindowHandleClose); diff --git a/source/types/zestScript/ZestStatement.ts b/source/types/zestScript/ZestStatement.ts index c4b58db..cc20c64 100644 --- a/source/types/zestScript/ZestStatement.ts +++ b/source/types/zestScript/ZestStatement.ts @@ -175,15 +175,30 @@ class ZestStatementWindowClose extends ZestStatement { class ZestStatementSwichToFrame extends ZestStatement { frameIndex: number; - constructor(frameIndex: number) { - super('ZestSwitchToFrame'); + frameName: string; + + windowHandle: string; + + constructor( + frameIndex: number, + frameName = '', + windowHandle = 'windowHandle1' + ) { + super('ZestClientSwitchToFrame'); this.frameIndex = frameIndex; + this.frameName = frameName; + this.windowHandle = windowHandle; } toJSON(): string { return JSON.stringify({ - elementType: this.elementType, + windowHandle: this.windowHandle, frameIndex: this.frameIndex, + frameName: this.frameName, + parent: this.frameIndex === -1, + index: this.index, + enabled: true, + elementType: this.elementType, }); } } diff --git a/test/ContentScript/integrationTests.test.ts b/test/ContentScript/integrationTests.test.ts index 6735b97..b1b0a85 100644 --- a/test/ContentScript/integrationTests.test.ts +++ b/test/ContentScript/integrationTests.test.ts @@ -219,6 +219,26 @@ function integrationTests( // Then expect(actualOutcome).toBe('test-name.zst'); }); + + test('Should record frame switches', async () => { + // Given / When + server = getFakeZapServer(actualData, _JSONPORT); + const context = await driver.getContext(_JSONPORT, true); + await driver.setEnable(false); + const page = await context.newPage(); + await page.goto( + `http://localhost:${_HTTPPORT}/webpages/interactions.html` + ); + const frame = await page.frame('frame1'); + frame?.click('#test-btn'); + await page.waitForLoadState('networkidle'); + await page.waitForTimeout(1000); + await page.close(); + // Then + expect(JSON.stringify(Array.from(actualData))).toBe( + '["{\\"action\\":{\\"action\\":\\"reportZestScript\\"},\\"body\\":{\\"scriptJson\\":\\"{\\"windowHandle\\":\\"windowHandle1\\",\\"frameIndex\\":0,\\"frameName\\":\\"\\",\\"parent\\":false,\\"index\\":1,\\"enabled\\":true,\\"elementType\\":\\"ZestClientSwitchToFrame\\"}\\",\\"apikey\\":\\"not set\\"}}","{\\"action\\":{\\"action\\":\\"reportZestScript\\"},\\"body\\":{\\"scriptJson\\":\\"{\\"windowHandle\\":\\"windowHandle1\\",\\"type\\":\\"id\\",\\"element\\":\\"test-btn\\",\\"index\\":2,\\"enabled\\":true,\\"elementType\\":\\"ZestClientElementClick\\"}\\",\\"apikey\\":\\"not set\\"}}"]' + ); + }); } } diff --git a/test/ContentScript/unitTests.test.ts b/test/ContentScript/unitTests.test.ts index dfa1e4f..4485a09 100644 --- a/test/ContentScript/unitTests.test.ts +++ b/test/ContentScript/unitTests.test.ts @@ -28,6 +28,7 @@ import { ElementLocator, ZestStatementElementClick, ZestStatementElementSendKeys, + ZestStatementSwichToFrame, } from '../../source/types/zestScript/ZestStatement'; jest.mock('webextension-polyfill'); @@ -458,3 +459,14 @@ test('should return correct path for element with XPath', () => { expect(path2.type).toBe('xpath'); expect(path2.element).toBe('/html/body/div/button[2]'); }); + +test('should generate valid frame switch statement', () => { + const zestStatementSwichToFrame = new ZestStatementSwichToFrame( + 0, + 'testvalue' + ); + + expect(zestStatementSwichToFrame.toJSON()).toBe( + '{"windowHandle":"windowHandle1","frameIndex":0,"frameName":"testvalue","parent":false,"index":-1,"enabled":true,"elementType":"ZestClientSwitchToFrame"}' + ); +}); diff --git a/test/ContentScript/webpages/interactions.html b/test/ContentScript/webpages/interactions.html index 9f0d6f7..43c701a 100644 --- a/test/ContentScript/webpages/interactions.html +++ b/test/ContentScript/webpages/interactions.html @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/test/ContentScript/webpages/testFrame.html b/test/ContentScript/webpages/testFrame.html new file mode 100644 index 0000000..88cbc84 --- /dev/null +++ b/test/ContentScript/webpages/testFrame.html @@ -0,0 +1,15 @@ + + + + Document + + + + + +` \ No newline at end of file