【问题标题】:Using AVA with PhantomJS将 AVA 与 PhantomJS 一起使用
【发布时间】:2017-04-21 19:27:07
【问题描述】:

我正在尝试使用 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);
        });
    });

页面加载后是否可以运行测试?我想在同一个页面上运行多个测试,但我不想每次都加载页面。

谢谢

【问题讨论】:

    标签: phantomjs ava


    【解决方案1】:

    您需要更改您的助手以不评估任何内容。然后你可以重用页面实例:

    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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多