|
| 1 | +import { IInteractionGroups } from '@secret-agent/core-interfaces/IInteractions'; |
| 2 | +import ISessionMeta from '@secret-agent/core-interfaces/ISessionMeta'; |
| 3 | +import { ILocationStatus, ILocationTrigger } from '@secret-agent/core-interfaces/Location'; |
| 4 | +import { IJsPath } from 'awaited-dom/base/AwaitedPath'; |
| 5 | +import { ICookie } from '@secret-agent/core-interfaces/ICookie'; |
| 6 | +import IWaitForElementOptions from '@secret-agent/core-interfaces/IWaitForElementOptions'; |
| 7 | +import IExecJsPathResult from '@secret-agent/core-interfaces/IExecJsPathResult'; |
| 8 | +import { IRequestInit } from 'awaited-dom/base/interfaces/official'; |
| 9 | +import IAttachedState from 'awaited-dom/base/IAttachedState'; |
| 10 | +import ISetCookieOptions from '@secret-agent/core-interfaces/ISetCookieOptions'; |
| 11 | +import IWaitForOptions from '@secret-agent/core-interfaces/IWaitForOptions'; |
| 12 | +import IFrameMeta from '@secret-agent/core-interfaces/IFrameMeta'; |
| 13 | +import CoreCommandQueue from './CoreCommandQueue'; |
| 14 | + |
| 15 | +export default class CoreFrameEnvironment { |
| 16 | + public tabId: number; |
| 17 | + public frameId: string; |
| 18 | + public sessionId: string; |
| 19 | + public commandQueue: CoreCommandQueue; |
| 20 | + |
| 21 | + constructor(meta: ISessionMeta & { sessionName: string }, commandQueue: CoreCommandQueue) { |
| 22 | + const { tabId, sessionId, frameId, sessionName } = meta; |
| 23 | + this.tabId = tabId; |
| 24 | + this.sessionId = sessionId; |
| 25 | + this.frameId = frameId; |
| 26 | + const queueMeta = { |
| 27 | + sessionId, |
| 28 | + tabId, |
| 29 | + sessionName, |
| 30 | + frameId, |
| 31 | + }; |
| 32 | + this.commandQueue = commandQueue.createSharedQueue(queueMeta); |
| 33 | + } |
| 34 | + |
| 35 | + public async getFrameMeta(): Promise<IFrameMeta> { |
| 36 | + return await this.commandQueue.run('FrameEnvironment.meta'); |
| 37 | + } |
| 38 | + |
| 39 | + public async getChildFrameEnvironment(jsPath: IJsPath): Promise<IFrameMeta> { |
| 40 | + return await this.commandQueue.run('FrameEnvironment.getChildFrameEnvironment', jsPath); |
| 41 | + } |
| 42 | + |
| 43 | + public async execJsPath<T = any>(jsPath: IJsPath): Promise<IExecJsPathResult<T>> { |
| 44 | + return await this.commandQueue.run('FrameEnvironment.execJsPath', jsPath); |
| 45 | + } |
| 46 | + |
| 47 | + public async getJsValue<T>(expression: string): Promise<{ value: T; type: string }> { |
| 48 | + return await this.commandQueue.run('FrameEnvironment.getJsValue', expression); |
| 49 | + } |
| 50 | + |
| 51 | + public async fetch(request: string | number, init?: IRequestInit): Promise<IAttachedState> { |
| 52 | + return await this.commandQueue.run('FrameEnvironment.fetch', request, init); |
| 53 | + } |
| 54 | + |
| 55 | + public async createRequest(input: string | number, init?: IRequestInit): Promise<IAttachedState> { |
| 56 | + return await this.commandQueue.run('FrameEnvironment.createRequest', input, init); |
| 57 | + } |
| 58 | + |
| 59 | + public async getUrl(): Promise<string> { |
| 60 | + return await this.commandQueue.run('FrameEnvironment.getLocationHref'); |
| 61 | + } |
| 62 | + |
| 63 | + public async interact(interactionGroups: IInteractionGroups): Promise<void> { |
| 64 | + await this.commandQueue.run('FrameEnvironment.interact', ...interactionGroups); |
| 65 | + } |
| 66 | + |
| 67 | + public async getCookies(): Promise<ICookie[]> { |
| 68 | + return await this.commandQueue.run('FrameEnvironment.getCookies'); |
| 69 | + } |
| 70 | + |
| 71 | + public async setCookie( |
| 72 | + name: string, |
| 73 | + value: string, |
| 74 | + options?: ISetCookieOptions, |
| 75 | + ): Promise<boolean> { |
| 76 | + return await this.commandQueue.run('FrameEnvironment.setCookie', name, value, options); |
| 77 | + } |
| 78 | + |
| 79 | + public async removeCookie(name: string): Promise<boolean> { |
| 80 | + return await this.commandQueue.run('FrameEnvironment.removeCookie', name); |
| 81 | + } |
| 82 | + |
| 83 | + public async isElementVisible(jsPath: IJsPath): Promise<boolean> { |
| 84 | + return await this.commandQueue.run('FrameEnvironment.isElementVisible', jsPath); |
| 85 | + } |
| 86 | + |
| 87 | + public async waitForElement(jsPath: IJsPath, opts: IWaitForElementOptions): Promise<void> { |
| 88 | + await this.commandQueue.run('FrameEnvironment.waitForElement', jsPath, opts); |
| 89 | + } |
| 90 | + |
| 91 | + public async waitForLoad(status: ILocationStatus, opts: IWaitForOptions): Promise<void> { |
| 92 | + await this.commandQueue.run('FrameEnvironment.waitForLoad', status, opts); |
| 93 | + } |
| 94 | + |
| 95 | + public async waitForLocation(trigger: ILocationTrigger, opts: IWaitForOptions): Promise<void> { |
| 96 | + await this.commandQueue.run('FrameEnvironment.waitForLocation', trigger, opts); |
| 97 | + } |
| 98 | +} |
0 commit comments