Add tests for all public methods on Page (#62)

This commit is contained in:
JoelEinbinder 2017-07-10 10:09:59 -07:00 committed by Andrey Lushnikov
parent ff4a389274
commit 739c1566a9
3 changed files with 67 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,67 @@ 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.setContent('<div>the result text</div>');
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