2017-04-21 6 views
2

最初のものはファントムを使用してWebページをロードするモジュールである(荷重 - 、私は、ファイルに持っている(実際には、ノードがphantomjs)私はAVAとPhantomJSを使用してWebページ上のいくつかのテストを実行しようとしていますPhantomJS

のAVAを使用して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); 
}); 
関連する問題