Skip to content

Commit

Permalink
update typing in puppeteer-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyajones committed Apr 13, 2021
1 parent 6d9ec07 commit 842459f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
1 change: 0 additions & 1 deletion build-system/tasks/e2e/functional-test-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-nocheck

/**
* A wrapper class that allows client code to own references to
Expand Down
77 changes: 37 additions & 40 deletions build-system/tasks/e2e/puppeteer-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

const fs = require('fs');
const puppeteer = require('puppeteer');
const puppeteer = require('puppeteer'); // eslint-disable-line no-unused-vars
const {
DOMRectDef,
ElementHandle,
Expand All @@ -24,13 +24,7 @@ const {
} = require('./functional-test-controller');
const {ControllerPromise} = require('./controller-promise');
const {dirname, join} = require('path');

const {
Browser, // eslint-disable-line no-unused-vars
JSHandle, // eslint-disable-line no-unused-vars
Page, // eslint-disable-line no-unused-vars
ElementHandle: PuppeteerHandle, // eslint-disable-line no-unused-vars
} = puppeteer;
const {promisify} = require('util');

/**
* For a list of all possible key strings, see
Expand All @@ -52,7 +46,7 @@ const DEFAULT_WAIT_TIMEOUT = 10000;
/**
* Make the test runner wait until the value returned by the valueFn matches
* the given condition.
* @param {!Page} page
* @param {!puppeteer.Page} page
* @param {function(T1): !Promise<?T2>|?T2} valueFn
* @param {!*[]} args
* @param {function(T2): boolean} condition
Expand Down Expand Up @@ -83,12 +77,13 @@ async function waitFor(page, valueFn, args, condition, opt_mutate) {
}

/**
* Remove the jsonValue wrapper from a PuppeteerHandle and
* Remove the jsonValue wrapper from a ElementHandle and
* remove an outer object wrapper if present.
* @param {!puppeteer.PuppeteerHandle} handle
* @param {!puppeteer.ElementHandle} handle
* @return {!Promise<*>}
*/
async function unboxHandle(handle) {
/** @type {*} */
const prop = await handle.jsonValue();
return 'value' in prop ? prop.value : prop;
}
Expand All @@ -97,7 +92,7 @@ async function unboxHandle(handle) {
* Evaluate the given function and its arguments in the context of the document.
* @param {!puppeteer.Frame} frame
* @param {function(...*):*} fn
* @return {!Promise<!JSHandle>}
* @return {!Promise<!puppeteer.JSHandle>}
*/
function evaluate(frame, fn) {
const args = Array.prototype.slice.call(arguments, 2);
Expand All @@ -107,7 +102,7 @@ function evaluate(frame, fn) {
/** @implements {FunctionalTestController} */
class PuppeteerController {
/**
* @param {!Browser} browser
* @param {!puppeteer.Browser} browser
*/
constructor(browser) {
/** @private @const */
Expand All @@ -128,7 +123,7 @@ class PuppeteerController {

/**
* Get the current page object. Create the object if it does not exist.
* @return {!Promise<!Page>}
* @return {!Promise<!puppeteer.Page>}
*/
async getPage_() {
if (!this.page_) {
Expand Down Expand Up @@ -171,7 +166,7 @@ class PuppeteerController {
* Evaluate a function in the context of the document.
* @param {function(...*):T} fn
* @param {...*} args
* @return {!Promise<!JSHandle>}
* @return {!Promise<!puppeteer.JSHandle>}
* @template T
*/
async evaluate(fn, ...args) {
Expand All @@ -186,14 +181,15 @@ class PuppeteerController {
*/
async evaluateValue_(fn, ...args) {
const value = await this.evaluate(fn, ...args);
/** @type {*} */
const json = await value.jsonValue();
return json.value;
}

/**
* @param {string} selector
* @param {number=} timeout
* @return {!Promise<!ElementHandle<!PuppeteerHandle>>}
* @return {!Promise<!ElementHandle<!puppeteer.ElementHandle>>}
* @override
*/
async findElement(selector, timeout = DEFAULT_WAIT_TIMEOUT) {
Expand All @@ -213,7 +209,7 @@ class PuppeteerController {

/**
* @param {string} selector
* @return {!Promise<!Array<!ElementHandle<!PuppeteerHandle>>>}
* @return {!Promise<!Array<!ElementHandle<!puppeteer.ElementHandle>>>}
* @override
*/
async findElements(selector) {
Expand Down Expand Up @@ -243,7 +239,7 @@ class PuppeteerController {

/**
* @param {string} xpath
* @return {!Promise<!ElementHandle<!PuppeteerHandle>>}
* @return {!Promise<!ElementHandle<!puppeteer.ElementHandle>>}
* @override
*/
async findElementXPath(xpath) {
Expand All @@ -265,7 +261,7 @@ class PuppeteerController {

/**
* @param {string} xpath
* @return {!Promise<!Array<!ElementHandle<!PuppeteerHandle>>>}
* @return {!Promise<!Array<!ElementHandle<!puppeteer.ElementHandle>>>}
* @override
*/
async findElementsXPath(xpath) {
Expand Down Expand Up @@ -316,7 +312,7 @@ class PuppeteerController {
}

/**
* @return {!Promise<!ElementHandle<!PuppeteerHandle>>}
* @return {!Promise<!ElementHandle<!puppeteer.ElementHandle>>}
* @override
*/
async getDocumentElement() {
Expand All @@ -337,7 +333,7 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {string|Key} keys
* @return {!Promise}
* @override
Expand All @@ -357,7 +353,7 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @return {!Promise<string>}
* @override
*/
Expand All @@ -371,7 +367,7 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!ElementHandle>} handle
* @param {string} styleProperty
* @return {!Promise<string>} styleProperty
* @override
Expand All @@ -389,7 +385,7 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {string} attribute
* @return {!Promise<string>}
* @override
Expand All @@ -406,7 +402,7 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {string} property
* @return {!Promise<string>}
* @override
Expand All @@ -428,7 +424,7 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @return {!Promise<!DOMRectDef>}
* @override
*/
Expand Down Expand Up @@ -565,7 +561,7 @@ class PuppeteerController {
}

/**
* @return {!Promise<!PuppeteerHandle>}
* @return {!Promise<!puppeteer.ElementHandle>}
* @override
*/
async getActiveElement() {
Expand All @@ -578,7 +574,7 @@ class PuppeteerController {

/**
* TODO(cvializ): decide if we need to waitForNavigation on click and keypress
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @return {!Promise}
* @override
*/
Expand All @@ -587,8 +583,8 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!puppeteer.ScrollToOptionsDef=} opt_scrollToOptions
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {!*=} opt_scrollToOptions
* @return {!Promise}
* @override
*/
Expand All @@ -604,8 +600,8 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!puppeteer.ScrollToOptionsDef=} opt_scrollToOptions
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {!*=} opt_scrollToOptions
* @return {!Promise}
* @override
*/
Expand All @@ -623,7 +619,7 @@ class PuppeteerController {
/**
* For some reason page.screenshot fails, so we use the html element.
* @param {string} path
* @return {!Promise<string>} An encoded string representing the image data
* @return {!Promise<string|Buffer|void>} An encoded string representing the image data
* @override
*/
async takeScreenshot(path) {
Expand All @@ -632,15 +628,16 @@ class PuppeteerController {
}

/**
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {string} path The path relative to the build-system/tasks/e2e root.
* @return {!Promise<string>} An encoded string representing the image data
* @return {!Promise<string|void|Buffer>} An encoded string representing the image data
* @override
*/
async takeElementScreenshot(handle, path) {
const relative = join(__dirname, path);
try {
// TODO(rileyajones) ask questions about this line...
// probably something like `await promisify(fs.mkdir)(dirname(relative), {recursive: true});`
await fs.mkdir(dirname(relative), {recursive: true});
} catch (err) {
if (err.code != 'EEXIST') {
Expand All @@ -657,7 +654,7 @@ class PuppeteerController {
}

/**
* @param {ElementHandle<!PuppeteerHandle>} handle
* @param {ElementHandle<!puppeteer.ElementHandle>} handle
* @return {!Promise}
*/
async switchToFrame(handle) {
Expand All @@ -679,7 +676,7 @@ class PuppeteerController {

/**
* Switch controller to shadowRoot body hosted by given element.
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @return {!Promise}
*/
switchToShadow(handle) {
Expand All @@ -689,7 +686,7 @@ class PuppeteerController {

/**
* Switch controller to shadowRoot hosted by given element.
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @return {!Promise}
*/
switchToShadowRoot(handle) {
Expand All @@ -698,7 +695,7 @@ class PuppeteerController {
}

/**.
* @param {!ElementHandle<!PuppeteerHandle>} handle
* @param {!ElementHandle<!puppeteer.ElementHandle>} handle
* @param {!Parameters<evaluate>[1]} getter
*/
async switchToShadowInternal_(handle, getter) {
Expand All @@ -713,7 +710,7 @@ class PuppeteerController {

/**
* Get the current root
* @return {!Promise<!PuppeteerHandle>}
* @return {!Promise<!puppeteer.JSHandle>}
*/
async getRoot_() {
if (this.shadowRoot_) {
Expand Down

0 comments on commit 842459f

Please sign in to comment.