2017-04-21 41 views
2

我試圖運行使用AVA和PhantomJS(實際上phantomjs節點)使用AVA與PhantomJS

我必須在文件的網頁一些測試,第一個是加載使用幻影網頁模塊(負載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

你需要改變你的幫手不評價任何東西。然後,您可以重新使用頁面實例:

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