browser_collection.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict'
  2. const EXECUTING = require('./browser').STATE_EXECUTING
  3. const Result = require('./browser_result')
  4. class BrowserCollection {
  5. constructor (emitter, browsers) {
  6. this.browsers = browsers || []
  7. this.emitter = emitter
  8. }
  9. add (browser) {
  10. this.browsers.push(browser)
  11. this.emitter.emit('browsers_change', this)
  12. }
  13. remove (browser) {
  14. const index = this.browsers.indexOf(browser)
  15. if (index === -1) {
  16. return false
  17. }
  18. this.browsers.splice(index, 1)
  19. this.emitter.emit('browsers_change', this)
  20. return true
  21. }
  22. getById (browserId) {
  23. return this.browsers.find((browser) => browser.id === browserId) || null
  24. }
  25. setAllToExecuting () {
  26. this.browsers.forEach((browser) => {
  27. browser.state = EXECUTING
  28. })
  29. this.emitter.emit('browsers_change', this)
  30. }
  31. areAllReady (nonReadyList) {
  32. nonReadyList = nonReadyList || []
  33. this.browsers.forEach((browser) => {
  34. if (!browser.isReady()) {
  35. nonReadyList.push(browser)
  36. }
  37. })
  38. return nonReadyList.length === 0
  39. }
  40. serialize () {
  41. return this.browsers.map((browser) => browser.serialize())
  42. }
  43. getResults () {
  44. const results = this.browsers.reduce((previous, current) => {
  45. previous.success += current.lastResult.success
  46. previous.failed += current.lastResult.failed
  47. previous.error = previous.error || current.lastResult.error
  48. previous.disconnected = previous.disconnected || current.lastResult.disconnected
  49. return previous
  50. }, {success: 0, failed: 0, error: false, disconnected: false, exitCode: 0})
  51. // compute exit status code
  52. results.exitCode = results.failed || results.error || results.disconnected ? 1 : 0
  53. return results
  54. }
  55. // TODO(vojta): can we remove this? (we clear the results per browser in onBrowserStart)
  56. clearResults () {
  57. this.browsers.forEach((browser) => {
  58. browser.lastResult = new Result()
  59. })
  60. }
  61. clone () {
  62. return new BrowserCollection(this.emitter, this.browsers.slice())
  63. }
  64. // Array APIs
  65. map (callback, context) {
  66. return this.browsers.map(callback, context)
  67. }
  68. forEach (callback, context) {
  69. return this.browsers.forEach(callback, context)
  70. }
  71. get length () {
  72. return this.browsers.length
  73. }
  74. }
  75. BrowserCollection.factory = function (emitter) {
  76. return new BrowserCollection(emitter)
  77. }
  78. module.exports = BrowserCollection