|
| 1 | +import Promise from 'pinkie'; |
| 2 | +import jimp from 'jimp'; |
| 3 | +import BaseBackend from './base'; |
| 4 | +import requestApiBase from '../utils/request-api'; |
| 5 | +import createBrowserstackStatus from '../utils/create-browserstack-status'; |
| 6 | + |
| 7 | + |
| 8 | +const API_POLLING_INTERVAL = 80000; |
| 9 | + |
| 10 | +const BROWSERSTACK_API_PATHS = { |
| 11 | + browserList: { |
| 12 | + url: 'https://api.browserstack.com/automate/browsers.json' |
| 13 | + }, |
| 14 | + |
| 15 | + newSession: { |
| 16 | + url: 'http://hub-cloud.browserstack.com/wd/hub/session', |
| 17 | + method: 'POST' |
| 18 | + }, |
| 19 | + |
| 20 | + openUrl: id => ({ |
| 21 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}/url`, |
| 22 | + method: 'POST' |
| 23 | + }), |
| 24 | + |
| 25 | + getWindowSize: id => ({ |
| 26 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}/window/current/size` |
| 27 | + }), |
| 28 | + |
| 29 | + setWindowSize: id => ({ |
| 30 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}/window/current/size`, |
| 31 | + method: 'POST' |
| 32 | + }), |
| 33 | + |
| 34 | + maximizeWindow: id => ({ |
| 35 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}/window/current/maximize`, |
| 36 | + method: 'POST' |
| 37 | + }), |
| 38 | + |
| 39 | + getUrl: id => ({ |
| 40 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}/url` |
| 41 | + }), |
| 42 | + |
| 43 | + deleteSession: id => ({ |
| 44 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}`, |
| 45 | + method: 'DELETE' |
| 46 | + }), |
| 47 | + |
| 48 | + screenshot: id => ({ |
| 49 | + url: `http://hub-cloud.browserstack.com/wd/hub/session/${id}/screenshot` |
| 50 | + }), |
| 51 | + |
| 52 | + getStatus: id => ({ |
| 53 | + url: `https://api.browserstack.com/automate/sessions/${id}.json` |
| 54 | + }), |
| 55 | + |
| 56 | + setStatus: id => ({ |
| 57 | + url: `https://api.browserstack.com/automate/sessions/${id}.json`, |
| 58 | + method: 'PUT' |
| 59 | + }) |
| 60 | +}; |
| 61 | + |
| 62 | + |
| 63 | +function requestApi (path, params) { |
| 64 | + return requestApiBase(path, params) |
| 65 | + .then(response => { |
| 66 | + if (response.status !== 0) |
| 67 | + throw new Error(`API error ${response.status}: ${response.value.message}`); |
| 68 | + |
| 69 | + return response; |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function getCorrectedSize (currentClientAreaSize, currentWindowSize, requestedSize) { |
| 74 | + var horizontalChrome = currentWindowSize.width - currentClientAreaSize.width; |
| 75 | + var verticalChrome = currentWindowSize.height - currentClientAreaSize.height; |
| 76 | + |
| 77 | + return { |
| 78 | + width: requestedSize.width + horizontalChrome, |
| 79 | + height: requestedSize.height + verticalChrome |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +export default class AutomateBackend extends BaseBackend { |
| 84 | + constructor (...args) { |
| 85 | + super(...args); |
| 86 | + |
| 87 | + this.sessions = {}; |
| 88 | + } |
| 89 | + |
| 90 | + async _requestSessionUrl (id) { |
| 91 | + var sessionInfo = await requestApiBase(BROWSERSTACK_API_PATHS.getStatus(this.sessions[id].sessionId)); |
| 92 | + |
| 93 | + return sessionInfo['automation_session']['browser_url']; |
| 94 | + } |
| 95 | + |
| 96 | + async _requestCurrentWindowSize (id) { |
| 97 | + var currentWindowSizeData = await requestApi(BROWSERSTACK_API_PATHS.getWindowSize(this.sessions[id].sessionId)); |
| 98 | + |
| 99 | + return { |
| 100 | + width: currentWindowSizeData.value.width, |
| 101 | + height: currentWindowSizeData.value.height |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + async getBrowsersList () { |
| 106 | + var platformsInfo = await requestApiBase(BROWSERSTACK_API_PATHS.browserList); |
| 107 | + |
| 108 | + return platformsInfo.reverse(); |
| 109 | + } |
| 110 | + |
| 111 | + getSessionUrl (id) { |
| 112 | + return this.sessions[id] ? this.sessions[id].sessionUrl : ''; |
| 113 | + } |
| 114 | + |
| 115 | + async openBrowser (id, pageUrl, capabilities) { |
| 116 | + var { localIdentifier, local, ...restCapabilities } = capabilities; |
| 117 | + |
| 118 | + capabilities = { |
| 119 | + 'browserstack.localIdentifier': localIdentifier, |
| 120 | + 'browserstack.local': local, |
| 121 | + ...restCapabilities |
| 122 | + }; |
| 123 | + |
| 124 | + this.sessions[id] = await requestApi(BROWSERSTACK_API_PATHS.newSession, { |
| 125 | + body: { desiredCapabilities: capabilities }, |
| 126 | + |
| 127 | + executeImmediately: true |
| 128 | + }); |
| 129 | + |
| 130 | + this.sessions[id].sessionUrl = await this._requestSessionUrl(id); |
| 131 | + |
| 132 | + var sessionId = this.sessions[id].sessionId; |
| 133 | + |
| 134 | + this.sessions[id].interval = setInterval(() => requestApi(BROWSERSTACK_API_PATHS.getUrl(sessionId), { executeImmediately: true }), API_POLLING_INTERVAL); |
| 135 | + |
| 136 | + await requestApi(BROWSERSTACK_API_PATHS.openUrl(sessionId), { body: { url: pageUrl } }); |
| 137 | + } |
| 138 | + |
| 139 | + async closeBrowser (id) { |
| 140 | + clearInterval(this.sessions[id].interval); |
| 141 | + |
| 142 | + await requestApi(BROWSERSTACK_API_PATHS.deleteSession(this.sessions[id].sessionId)); |
| 143 | + } |
| 144 | + |
| 145 | + async takeScreenshot (id, screenshotPath) { |
| 146 | + return new Promise(async (resolve, reject) => { |
| 147 | + var base64Data = await requestApi(BROWSERSTACK_API_PATHS.screenshot(this.sessions[id].sessionId)); |
| 148 | + var buffer = Buffer.from(base64Data.value, 'base64'); |
| 149 | + |
| 150 | + jimp |
| 151 | + .read(buffer) |
| 152 | + .then(image => image.write(screenshotPath, resolve)) |
| 153 | + .catch(reject); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + async resizeWindow (id, width, height, currentWidth, currentHeight) { |
| 158 | + var sessionId = this.sessions[id].sessionId; |
| 159 | + var currentWindowSize = await this._requestCurrentWindowSize(id); |
| 160 | + var currentClientAreaSize = { width: currentWidth, height: currentHeight }; |
| 161 | + var requestedSize = { width, height }; |
| 162 | + var correctedSize = getCorrectedSize(currentClientAreaSize, currentWindowSize, requestedSize); |
| 163 | + |
| 164 | + await requestApi(BROWSERSTACK_API_PATHS.setWindowSize(sessionId), { body: correctedSize }); |
| 165 | + } |
| 166 | + |
| 167 | + async maximizeWindow (id) { |
| 168 | + await requestApi(BROWSERSTACK_API_PATHS.maximizeWindow(this.sessions[id].sessionId)); |
| 169 | + } |
| 170 | + |
| 171 | + async reportJobResult (id, jobResult, jobData, possibleResults) { |
| 172 | + var sessionId = this.sessions[id].sessionId; |
| 173 | + var jobStatus = createBrowserstackStatus(jobResult, jobData, possibleResults); |
| 174 | + |
| 175 | + await requestApiBase(BROWSERSTACK_API_PATHS.setStatus(sessionId), { body: jobStatus }); |
| 176 | + } |
| 177 | +} |
0 commit comments