Skip to content

Commit

Permalink
feat: support to fill popup auth dialog schickling#457
Browse files Browse the repository at this point in the history
  • Loading branch information
skiloop committed Sep 29, 2018
1 parent 774223e commit b256d3c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Chromeless provides TypeScript typings.
- [`setCookies(cookie: Cookie)`](#api-setcookies-one)
- [`setCookies(cookies: Cookie[])`](#api-setcookies-many)
- [`deleteCookies(name: string)`](#api-deletecookies)
- [`authenticate(username: string, password: string, patterns?: RequestPattern[])`](#api-authenticate)
- [`clearCookies()`](#api-clearcookies)


Expand Down Expand Up @@ -770,3 +771,22 @@ __Example__
```js
await chromeless.setFileInput('.uploader', '/User/Me/Documents/img.jpg')
```
---------------------------------------

<a name="api-authenticate" />

### authenticate(username: string, password: string, patterns?: RequestPattern[]): Chromeless<T>

provide authenticate for pop up dialog

RequestPattern is defined as [here](https://chromedevtools.github.io/devtools-protocol/tot/Network#type-RequestPattern)


__Example__

```js
await chromeless
.authenticate('username', 'Passw0Rd',[{urlPattern: '*'}])
.goto('https://github.com')
.screenshot()
```
11 changes: 11 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PdfOptions,
DeviceMetrics,
ScreenshotOptions,
RequestPattern,
} from './types'
import { getDebugOption } from './util'
import { isArray } from 'util'
Expand Down Expand Up @@ -198,6 +199,16 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

authenticate(
username: string,
password: string,
patterns?: RequestPattern[],
): Chromeless<T> {
this.queue.enqueue({ type: 'authenticate', username, password, patterns })

return this
}

setExtraHTTPHeaders(headers: Headers): Chromeless<T> {
this.queue.enqueue({ type: 'setExtraHTTPHeaders', headers })

Expand Down
43 changes: 43 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CookieQuery,
PdfOptions,
ScreenshotOptions,
RequestPattern,
} from '../types'
import {
nodeExists,
Expand Down Expand Up @@ -123,6 +124,12 @@ export default class LocalRuntime {
return this.clearInput(command.selector)
case 'setFileInput':
return this.setFileInput(command.selector, command.files)
case 'authenticate':
return this.authenticate(
command.username,
command.password,
command.patterns,
)
default:
throw new Error(`No such command: ${JSON.stringify(command)}`)
}
Expand Down Expand Up @@ -173,6 +180,42 @@ export default class LocalRuntime {
await this.log(`Set useragent to ${this.userAgentValue}`)
}

private async authenticate(
username: string,
password: string,
pattens?: RequestPattern[],
): Promise<void> {
const { Network } = this.client
await Network.setRequestInterception({
patterns: pattens ? pattens : [{ urlPattern: '*' }],
})

Network.requestIntercepted(
async ({ interceptionId, request, authChallenge }) => {
// perform a test against the intercepted request
this.log(
`${interceptionId} ${request.url} ${
authChallenge ? authChallenge.origin : 'undefined'
} ${authChallenge ? authChallenge.scheme : 'undefined'}`,
)
if (authChallenge) {
await Network.continueInterceptedRequest({
interceptionId: interceptionId,
authChallengeResponse: {
response: 'ProvideCredentials',
username: username,
password: password,
},
})
} else {
await Network.continueInterceptedRequest({
interceptionId: interceptionId,
})
}
},
)
}

private async waitTimeout(timeout: number): Promise<void> {
this.log(`Waiting for ${timeout}ms`)
await wait(timeout)
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ export type Command =
selector: string
files: string[]
}
| {
type: 'authenticate'
username: string
password: string
patterns?: RequestPattern[]
}

export type Headers = Record<string, string>

Expand Down Expand Up @@ -269,3 +275,9 @@ export interface Viewport {
height: number
scale: number
}

export interface RequestPattern {
urlPattern?: string
resourceType?: string
interceptionStage?: string
}

0 comments on commit b256d3c

Please sign in to comment.