puppeteer
Advanced tools
Comparing version 1.0.0-next.1516351540753 to 1.0.0-next.1516663116955
@@ -569,4 +569,10 @@ /** | ||
waitFor(selectorOrFunctionOrTimeout, options = {}, ...args) { | ||
if (helper.isString(selectorOrFunctionOrTimeout)) | ||
return this.waitForSelector(/** @type {string} */(selectorOrFunctionOrTimeout), options); | ||
const xPathPattern = '//'; | ||
if (helper.isString(selectorOrFunctionOrTimeout)) { | ||
const string = /** @type {string} */ (selectorOrFunctionOrTimeout); | ||
if (string.startsWith(xPathPattern)) | ||
return this.waitForXPath(string, options); | ||
return this.waitForSelector(string, options); | ||
} | ||
if (helper.isNumber(selectorOrFunctionOrTimeout)) | ||
@@ -585,10 +591,48 @@ return new Promise(fulfill => setTimeout(fulfill, selectorOrFunctionOrTimeout)); | ||
waitForSelector(selector, options = {}) { | ||
return this._waitForSelectorOrXPath(selector, false, options); | ||
} | ||
/** | ||
* @param {string} xpath | ||
* @param {!Object=} options | ||
* @return {!Promise} | ||
*/ | ||
waitForXPath(xpath, options = {}) { | ||
return this._waitForSelectorOrXPath(xpath, true, options); | ||
} | ||
/** | ||
* @param {Function|string} pageFunction | ||
* @param {!Object=} options | ||
* @return {!Promise} | ||
*/ | ||
waitForFunction(pageFunction, options = {}, ...args) { | ||
const timeout = options.timeout || 30000; | ||
const polling = options.polling || 'raf'; | ||
return new WaitTask(this, pageFunction, polling, timeout, ...args).promise; | ||
} | ||
/** | ||
* @return {!Promise<string>} | ||
*/ | ||
async title() { | ||
return this.evaluate(() => document.title); | ||
} | ||
/** | ||
* @param {string} selectorOrXPath | ||
* @param {boolean} isXPath | ||
* @param {!Object=} options | ||
* @return {!Promise} | ||
*/ | ||
_waitForSelectorOrXPath(selectorOrXPath, isXPath, options = {}) { | ||
const timeout = options.timeout || 30000; | ||
const waitForVisible = !!options.visible; | ||
const waitForHidden = !!options.hidden; | ||
const polling = waitForVisible || waitForHidden ? 'raf' : 'mutation'; | ||
return this.waitForFunction(predicate, {timeout, polling}, selector, waitForVisible, waitForHidden); | ||
return this.waitForFunction(predicate, {timeout, polling}, selectorOrXPath, isXPath, waitForVisible, waitForHidden); | ||
/** | ||
* @param {string} selector | ||
* @param {string} selectorOrXPath | ||
* @param {boolean} isXPath | ||
* @param {boolean} waitForVisible | ||
@@ -598,4 +642,6 @@ * @param {boolean} waitForHidden | ||
*/ | ||
function predicate(selector, waitForVisible, waitForHidden) { | ||
const node = document.querySelector(selector); | ||
function predicate(selectorOrXPath, isXPath, waitForVisible, waitForHidden) { | ||
const node = isXPath | ||
? document.evaluate(selectorOrXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue | ||
: document.querySelector(selectorOrXPath); | ||
if (!node) | ||
@@ -605,3 +651,5 @@ return waitForHidden; | ||
return node; | ||
const style = window.getComputedStyle(node); | ||
const element = /** @type {Element} */ (node.nodeType === Node.TEXT_NODE ? node.parentElement : node); | ||
const style = window.getComputedStyle(element); | ||
const isVisible = style && style.visibility !== 'hidden' && hasVisibleBoundingBox(); | ||
@@ -615,3 +663,3 @@ const success = (waitForVisible === isVisible || waitForHidden === !isVisible); | ||
function hasVisibleBoundingBox() { | ||
const rect = node.getBoundingClientRect(); | ||
const rect = element.getBoundingClientRect(); | ||
return !!(rect.top || rect.bottom || rect.width || rect.height); | ||
@@ -623,20 +671,2 @@ } | ||
/** | ||
* @param {Function|string} pageFunction | ||
* @param {!Object=} options | ||
* @return {!Promise} | ||
*/ | ||
waitForFunction(pageFunction, options = {}, ...args) { | ||
const timeout = options.timeout || 30000; | ||
const polling = options.polling || 'raf'; | ||
return new WaitTask(this, pageFunction, polling, timeout, ...args).promise; | ||
} | ||
/** | ||
* @return {!Promise<string>} | ||
*/ | ||
async title() { | ||
return this.evaluate(() => document.title); | ||
} | ||
/** | ||
* @param {!Object} framePayload | ||
@@ -663,3 +693,3 @@ */ | ||
for (const waitTask of this._waitTasks) | ||
waitTask.terminate(new Error('waitForSelector failed: frame got detached.')); | ||
waitTask.terminate(new Error('waitForFunction failed: frame got detached.')); | ||
this._detached = true; | ||
@@ -666,0 +696,0 @@ if (this._parentFrame) |
@@ -885,2 +885,11 @@ /** | ||
/** | ||
* @param {string} xpath | ||
* @param {!Object=} options | ||
* @return {!Promise} | ||
*/ | ||
waitForXPath(xpath, options = {}) { | ||
return this.mainFrame().waitForXPath(xpath, options); | ||
} | ||
/** | ||
* @param {function()} pageFunction | ||
@@ -887,0 +896,0 @@ * @param {!Object=} options |
{ | ||
"name": "puppeteer", | ||
"version": "1.0.0-next.1516351540753", | ||
"version": "1.0.0-next.1516663116955", | ||
"description": "A high-level API to control headless Chrome over the DevTools Protocol", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
255834
6433