feat(Frame): reject error for addScriptTag/addStyleTag (#1287)
fix #1221
This commit is contained in:
parent
bdd5718630
commit
8e445734c6
@ -326,8 +326,14 @@ class Frame {
|
||||
* @return {!Promise<!ElementHandle>}
|
||||
*/
|
||||
async addScriptTag(options) {
|
||||
if (typeof options.url === 'string')
|
||||
return this._context.evaluateHandle(addScriptUrl, options.url);
|
||||
if (typeof options.url === 'string') {
|
||||
const url = options.url;
|
||||
try {
|
||||
return await this._context.evaluateHandle(addScriptUrl, url);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading script from ${url} failed`);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof options.path === 'string') {
|
||||
let contents = await readFileAsync(options.path, 'utf8');
|
||||
@ -349,7 +355,10 @@ class Frame {
|
||||
const script = document.createElement('script');
|
||||
script.src = url;
|
||||
document.head.appendChild(script);
|
||||
await new Promise(x => script.onload = x);
|
||||
await new Promise((res, rej) => {
|
||||
script.onload = res;
|
||||
script.onerror = rej;
|
||||
});
|
||||
return script;
|
||||
}
|
||||
|
||||
@ -371,8 +380,14 @@ class Frame {
|
||||
* @return {!Promise<!ElementHandle>}
|
||||
*/
|
||||
async addStyleTag(options) {
|
||||
if (typeof options.url === 'string')
|
||||
return await this._context.evaluateHandle(addStyleUrl, options.url);
|
||||
if (typeof options.url === 'string') {
|
||||
const url = options.url;
|
||||
try {
|
||||
return await this._context.evaluateHandle(addStyleUrl, url);
|
||||
} catch (error) {
|
||||
throw new Error(`Loading style from ${url} failed`);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof options.path === 'string') {
|
||||
let contents = await readFileAsync(options.path, 'utf8');
|
||||
@ -395,7 +410,10 @@ class Frame {
|
||||
link.rel = 'stylesheet';
|
||||
link.href = url;
|
||||
document.head.appendChild(link);
|
||||
await new Promise(x => link.onload = x);
|
||||
await new Promise((res, rej) => {
|
||||
link.onload = res;
|
||||
link.onerror = rej;
|
||||
});
|
||||
return link;
|
||||
}
|
||||
|
||||
|
22
test/test.js
22
test/test.js
@ -2406,6 +2406,17 @@ describe('Page', function() {
|
||||
expect(await page.evaluate(() => __injected)).toBe(42);
|
||||
}));
|
||||
|
||||
it('should throw an error if loading from url fail', SX(async function() {
|
||||
await page.goto(EMPTY_PAGE);
|
||||
let error = null;
|
||||
try {
|
||||
await page.addScriptTag({ url: '/nonexistfile.js' });
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
expect(error.message).toBe('Loading script from /nonexistfile.js failed');
|
||||
}));
|
||||
|
||||
it('should work with a path', SX(async function() {
|
||||
await page.goto(EMPTY_PAGE);
|
||||
const scriptHandle = await page.addScriptTag({ path: path.join(__dirname, 'assets/injectedfile.js') });
|
||||
@ -2446,6 +2457,17 @@ describe('Page', function() {
|
||||
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
||||
}));
|
||||
|
||||
it('should throw an error if loading from url fail', SX(async function() {
|
||||
await page.goto(EMPTY_PAGE);
|
||||
let error = null;
|
||||
try {
|
||||
await page.addStyleTag({ url: '/nonexistfile.js' });
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
expect(error.message).toBe('Loading style from /nonexistfile.js failed');
|
||||
}));
|
||||
|
||||
it('should work with a path', SX(async function() {
|
||||
await page.goto(EMPTY_PAGE);
|
||||
const styleHandle = await page.addStyleTag({ path: path.join(__dirname, 'assets/injectedstyle.css') });
|
||||
|
Loading…
Reference in New Issue
Block a user