Add tests for all public methods on page

This commit is contained in:
Joel Einbinder 2017-07-07 19:12:29 -07:00
parent 090ecfa6b9
commit 965b87f2ea
3 changed files with 71 additions and 2 deletions

View File

@ -285,8 +285,8 @@ class Page extends EventEmitter {
fitWindow: false
}),
this._client.send('Emulation.setVisibleSize', {
width: width / zoom,
height: height / zoom,
width: Math.floor(width / zoom),
height: Math.floor(height / zoom),
})
]);
}

View File

@ -0,0 +1,3 @@
<script>
window.result = window.injected;
</script>

View File

@ -14,6 +14,7 @@
* limitations under the License.
*/
let fs = require('fs');
let path = require('path');
let Browser = require('../lib/Browser');
let SimpleServer = require('./SimpleServer');
@ -678,6 +679,71 @@ describe('Puppeteer', function() {
]);
}));
});
describe('Page.addScriptTag', function() {
it('should work', SX(async function() {
await page.navigate(EMPTY_PAGE);
await page.addScriptTag('/injectedfile.js');
expect(await page.evaluate(() => __injected)).toBe(42);
}));
});
describe('Page.url', function() {
it('should work', SX(async function() {
expect(await page.url()).toBe('about:blank');
await page.navigate(EMPTY_PAGE);
expect(await page.url()).toBe(EMPTY_PAGE);
}));
});
describe('Page.viewportSize', function() {
it('should get the proper viewport size', SX(async function() {
expect(page.viewportSize()).toEqual({width: 400, height: 300});
await page.setViewportSize({width: 123, height: 456});
expect(page.viewportSize()).toEqual({width: 123, height: 456});
}));
});
describe('Page.evaluateOnInitialized', function() {
it('should evaluate before anything else on the page', SX(async function() {
await page.evaluateOnInitialized(function(){
window.injected = 123;
});
await page.navigate(STATIC_PREFIX + '/tamperable.html');
expect(await page.evaluate(() => window.result)).toBe(123);
}));
});
describe('Page.printToPDF', function() {
let outputFile = __dirname + '/assets/output.pdf';
afterEach(function() {
fs.unlinkSync(outputFile);
});
it('should print to pdf', SX(async function() {
await page.navigate(STATIC_PREFIX + '/grid.html');
await page.printToPDF(outputFile);
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
}));
});
describe('Page.plainText', function() {
it('should return the page text', SX(async function(){
await page.evaluate(function(){
let element = document.createElement('div');
document.body.appendChild(element);
element.textContent = 'the result text';
});
expect(await page.plainText()).toBe('the result text');
}));
});
describe('Page.title', function() {
it('should return the page title', SX(async function(){
await page.navigate(STATIC_PREFIX + '/input/button.html');
expect(await page.title()).toBe('Button test');
}));
});
});
// Since Jasmine doesn't like async functions, they should be wrapped