All files / wdio-devtools-service/src driver.js

100% Statements 21/21
100% Branches 6/6
100% Functions 6/6
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66        2x             6x                   3x 3x       3x       4x 2x     2x     2x       3x 1x     2x 2x 2x       4x 2x     2x 2x 2x       1x 1x      
import puppeteer from 'puppeteer-core'
 
import logger from '@wdio/logger'
 
const log = logger('@wdio/devtools-service:DevToolsDriver')
 
/**
 * connects a puppeteer instance to the browser
 */
export default class DevToolsDriver {
    constructor (browser) {
        this.browser = browser
 
        /**
         * ToDo (Christian): handle target changes
         * this should usually never happens as we only connect within one
         * target within the VM so maybe we can discard this
         */
    }
 
    static async attach (url) {
        log.info(`Connect to ${url}`)
        const browser = await puppeteer.connect({
            browserURL: url,
            defaultViewport: null
        })
        return new DevToolsDriver(browser)
    }
 
    async getActiveTarget () {
        if (this.target) {
            return this.target
        }
 
        const target = this.target = await this.browser.waitForTarget(
            /* istanbul ignore next */
            (t) => t.type() === 'page')
        return target
    }
 
    async getActivePage () {
        if (this.page) {
            return this.page
        }
 
        const target = await this.getActiveTarget()
        this.page = await target.page()
        return this.page
    }
 
    async getCDPSession () {
        if (this.cdpSession) {
            return this.cdpSession
        }
 
        const target = await this.getActiveTarget()
        this.cdpSession = await target.createCDPSession()
        return this.cdpSession
    }
 
    async send (method, params) {
        const cdpSession = await this.getCDPSession()
        return cdpSession.send(method, params)
    }
}