test.browser.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* TODO: make it work in Travis CI
  2. ------------------------------------------------------------------------ */
  3. const selenium = require ('selenium-webdriver/testing')
  4. /* ------------------------------------------------------------------------ */
  5. selenium.describe ('Chrome test', (done) => {
  6. const webdriver = require ('selenium-webdriver')
  7. , path = require ('path')
  8. , fs = require ('fs')
  9. , memFS = new (require ('memory-fs')) ()
  10. , it = selenium.it
  11. , webpack = require ('webpack')
  12. , logging = require ('selenium-webdriver/lib/logging')
  13. let driver
  14. /* Prepare ChromeDriver (with CORS disabled and log interception enabled) */
  15. selenium.before (() => driver =
  16. new webdriver
  17. .Builder ()
  18. .withCapabilities (
  19. webdriver.Capabilities
  20. .chrome ()
  21. .setLoggingPrefs (new logging.Preferences ().setLevel (logging.Type.BROWSER, logging.Level.ALL))
  22. .set ('chromeOptions', {
  23. 'args': ['--disable-web-security'] }))
  24. .build ())
  25. selenium.after (() => driver.quit ())
  26. it ('works', async () => {
  27. /* Compile get-source */
  28. const compiledScript = await (new Promise (resolve => { Object.assign (webpack ({
  29. entry: './test/files/get-source.webpack.entry.js',
  30. output: { path: '/', filename: 'get-source.webpack.compiled.js' },
  31. plugins: [ new webpack.IgnorePlugin(/^fs$/) ]
  32. }), { outputFileSystem: memFS }).run ((err, stats) => {
  33. if (err) throw err
  34. resolve (memFS.readFileSync ('/get-source.webpack.compiled.js').toString ('utf-8'))
  35. })
  36. }))
  37. /* Inject it into Chrome */
  38. driver.get ('file://' + path.resolve ('./test/files/test.html'))
  39. driver.executeScript (compiledScript)
  40. /* Execute test */
  41. const exec = fn => driver.executeScript (`(${fn.toString ()})()`)
  42. try {
  43. await exec (function () {
  44. path.relativeToFile ('http://foo.com/scripts/bar.js', '../bar.js.map')
  45. .should.equal ('http://foo.com/bar.js.map')
  46. path.relativeToFile ('http://foo.com/scripts/bar.js', 'http://bar.js.map')
  47. .should.equal ('http://bar.js.map')
  48. path.relativeToFile ('http://foo.com/scripts/bar.js', '/bar.js.map')
  49. .should.equal ('file:///bar.js.map')
  50. path.relativeToFile ('http://foo.com/scripts/bar.js', '//bar.com/bar.js.map')
  51. .should.equal ('http://bar.com/bar.js.map')
  52. var loc = getSource ('../original.uglified.beautified.js').resolve ({ line: 2, column: 4 })
  53. loc.line.should.equal (4)
  54. loc.column.should.equal (2)
  55. loc.sourceFile.path.should.contain ('test/files/original.js')
  56. loc.sourceLine.should.equal ('\treturn \'hello world\' }')
  57. })
  58. } catch (e) { throw e } finally {
  59. driver.manage ().logs ().get (logging.Type.BROWSER).then (entries => {
  60. entries.forEach (entry => {
  61. console.log('[BROWSER] [%s] %s', entry.level.name, entry.message);
  62. })
  63. })
  64. }
  65. })
  66. })
  67. /* ------------------------------------------------------------------------ */