| 4 | import { WdaService } from "./wda-service"; |
| 5 | |
| 6 | class WdaControlClient { |
| 7 | service: WdaService; |
| 8 | sessionId: string = "0"; |
| 9 | |
| 10 | constructor(port: number) { |
| 11 | this.service = new WdaService(port); |
| 12 | } |
| 13 | |
| 14 | async createWdaSession(): Promise<void> { |
| 15 | try { |
| 16 | const postData = { capabilities: {} }; |
| 17 | const response = await this.service.apiCall(WdaEndpoints.CREATE_SESSION, HttpMethod.POST, postData); |
| 18 | const session: Session = response.data; |
| 19 | if (session?.sessionId) { |
| 20 | this.sessionId = session.sessionId; |
| 21 | } else { |
| 22 | throw new Error("Invalid session data received"); |
| 23 | } |
| 24 | } catch (error: any) { |
| 25 | logger.error(`Failed to create WDA session: ${error.message || error}`); |
| 26 | throw new Error("Failed to create WDA session"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | async deleteWdaSession(): Promise<Session> { |
| 31 | try { |
| 32 | const response = await this.service.apiCall(WdaEndpoints.DELETE_SESSION, HttpMethod.DELETE, null, { sessionId: this.sessionId }); |
| 33 | logger.info("WDA session deleted", response.data); |
| 34 | const session: Session = response.data; |
| 35 | if (session) { |
| 36 | return session; |
| 37 | } else { |
| 38 | throw new Error(response.data?.value?.message || "Failed to delete WDA session"); |
| 39 | } |
| 40 | } catch (error: any) { |
| 41 | logger.error(`Failed to delete WDA session: ${error.message || error}`); |
| 42 | throw new Error("Failed to delete WDA session"); |
| 43 | } finally { |
| 44 | this.sessionId = "0"; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | async performCommand(command: Command): Promise<{ success: boolean }> { |
| 49 | const params = { sessionId: this.sessionId }; |
| 50 | try { |
| 51 | let endpoint: WdaEndpoints; |
| 52 | let method: HttpMethod; |
| 53 | let postData: any = command.values; |
| 54 | switch (command.cmd) { |
| 55 | case WdaCommands.OPEN_URL: |
| 56 | endpoint = WdaEndpoints.OPEN_URL; |
| 57 | method = HttpMethod.POST; |
| 58 | break; |
| 59 | case WdaCommands.TAP: |
| 60 | endpoint = WdaEndpoints.CUSTOM_TAP; |
| 61 | method = HttpMethod.POST; |
| 62 | break; |
| 63 | case WdaCommands.TEXT_INPUT: |
nothing calls this directly
no outgoing calls
no test coverage detected