import { Page } from 'puppeteer' ; import { Base } from './base' ; import { Cookie , Cookies } from '../../console-api' ; export class TwilioConsole extends Base { private static _loginForm = 'form[method="post"' ; assert = { } ; private readonly _baseUrl : string ; private readonly _flexBaseUrl : string ; constructor ( page , { flex , twilioConsole } ) { super ( page ) ; this . _baseUrl = twilioConsole ; this . _flexBaseUrl = flex ; } param port private static _createLocalhostUrl = ( port ) => `http://localhost:${ port }&localPort=${ port }` ; param cookies param flexBaseUrl param flexPath param accountSid async login ( cookies , flexPath , accountSid , localhostPort ) { const redirectUrl = this . _flexBaseUrl . includes ( 'localhost' ) ? TwilioConsole . _createLocalhostUrl ( localhostPort ) : this . _flexBaseUrl ; const path = `console/flex/service-login/${ accountSid }/?path=/${ flexPath }&referer=${ redirectUrl }` ; await this . goto ( { baseUrl : this . _baseUrl , path } ) ; await this . elementVisible ( TwilioConsole . _loginForm , `Twilio Console's Login form` ) ; const requiredCookies = [ { name : Cookie . visitor , value : cookies [ Cookie . visitor ] } , { name : Cookie . sIdentity , value : cookies [ Cookie . sIdentity ] } , ] ; if ( cookies . identity ) { requiredCookies . push ( { name : Cookie . identity , value : cookies [ Cookie . identity ] as string } ) ; } await this . page . setCookie ( ... requiredCookies ) ; await this . goto ( { baseUrl : this . _baseUrl , path } ) ; } }