2020-08-05 01:09:24 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-05-06 17:08:22 +03:00
|
|
|
import { test as it, expect } from './pageTest';
|
2020-08-12 01:50:53 +03:00
|
|
|
import path from 'path';
|
2020-08-05 01:09:24 +03:00
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should throw an error if no options are provided', async ({ page, server }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
let error = null;
|
|
|
|
try {
|
2020-08-28 14:20:29 +03:00
|
|
|
// @ts-ignore
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.addStyleTag('/injectedstyle.css');
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error.message).toContain('Provide an object with a `url`, `path` or `content` property');
|
|
|
|
});
|
|
|
|
|
2022-03-10 21:42:52 +03:00
|
|
|
it('should work with a url @smoke', async ({ page, server }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const styleHandle = await page.addStyleTag({ url: '/injectedstyle.css' });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should throw an error if loading from url fail', async ({ page, server }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
let error = null;
|
|
|
|
try {
|
|
|
|
await page.addStyleTag({ url: '/nonexistfile.js' });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
expect(error).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should work with a path', async ({ page, server, asset }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2021-04-06 01:51:45 +03:00
|
|
|
const styleHandle = await page.addStyleTag({ path: asset('injectedstyle.css') });
|
2020-08-05 01:09:24 +03:00
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(255, 0, 0)');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should include sourceURL when path is provided', async ({ page, server, asset }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2021-04-06 01:51:45 +03:00
|
|
|
await page.addStyleTag({ path: asset('injectedstyle.css') });
|
2020-08-05 01:09:24 +03:00
|
|
|
const styleHandle = await page.$('style');
|
|
|
|
const styleContent = await page.evaluate(style => style.innerHTML, styleHandle);
|
|
|
|
expect(styleContent).toContain(path.join('assets', 'injectedstyle.css'));
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should work with content', async ({ page, server }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const styleHandle = await page.addStyleTag({ content: 'body { background-color: green; }' });
|
|
|
|
expect(styleHandle.asElement()).not.toBeNull();
|
|
|
|
expect(await page.evaluate(`window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')`)).toBe('rgb(0, 128, 0)');
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should throw when added with content to the CSP page', async ({ page, server }) => {
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page.addStyleTag({ content: 'body { background-color: green; }' }).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2021-09-27 19:58:08 +03:00
|
|
|
it('should throw when added with URL to the CSP page', async ({ page, server, isAndroid }) => {
|
2021-04-09 17:59:09 +03:00
|
|
|
it.skip(isAndroid, 'No cross-process on Android');
|
2021-04-03 00:23:42 +03:00
|
|
|
|
2020-08-05 01:09:24 +03:00
|
|
|
await page.goto(server.PREFIX + '/csp.html');
|
|
|
|
let error = null;
|
|
|
|
await page.addStyleTag({ url: server.CROSS_PROCESS_PREFIX + '/injectedstyle.css' }).catch(e => error = e);
|
|
|
|
expect(error).toBeTruthy();
|
|
|
|
});
|