我正在尝试使用AVA和PhantomJS(实际上是phantomjs-node)在网页上运行一些测试
我需要文件,第一个是使用Phantom加载网页的模块(load-page.js)
const phantom = require('phantom');
/**
* Loads a page using a Promise and jsdom
* @param {string} url The page to be loaded
*/
export default function loadPage(url, callback, thenCallback) {
return new Promise(async (resolve, reject) => {
const instance = await phantom.create();
const page = await instance.createPage();
const status = await page.open('https://webslides.tv/');
if(status != 'success') reject('Error loading '+url);
await page
.evaluate(callback)
.then(await thenCallback);
await instance.exit();
resolve(true);
});
}
第二个是测试:
import test from 'ava';
import loadPage from '../helpers/load-page';
test('prueba', async t => {
const check = count => {
t.is(count.childNodes.length, 9);
}
await loadPage('http://webslides.tv', () => {
const ws = document.querySelector('#webslides');
return ws;
}, async (count) => {
await check(count);
});
});
页面加载后是否可以运行测试?我想用同一页面运行几个测试,但我不想每次加载页面。
由于
答案 0 :(得分:0)
您需要更改助手才能评估任何内容。然后你可以重用页面实例:
import test from 'ava';
import loadPage from '../helpers/load-page';
let page;
test.before(async t => {
page = await loadPage('http://webslides.tv');
});
test('prueba', async t => {
const count = await page.evaluate(() => {
const ws = document.querySelector('#webslides');
return ws;
});
t.is(count.childNodes.length, 9);
});