Skip to content

Commit a95fbda

Browse files
committed
refactor variables store to not be a singleton
1 parent a73e7ae commit a95fbda

File tree

11 files changed

+113
-114
lines changed

11 files changed

+113
-114
lines changed

packages/selenium-ide/src/neo/IO/SideeX/ext-command.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import browser from 'webextension-polyfill'
1919
import Debugger, { convertLocator } from '../debugger'
2020
import PlaybackState from '../../stores/view/PlaybackState'
21-
import variables from '../../stores/view/Variables'
22-
import { Logger, Channels } from '../../stores/view/Logs'
21+
import { Logger, Channels, output } from '../../stores/view/Logs'
2322
import FrameNotFoundError from '../../../errors/frame-not-found'
2423
import { absolutifyUrl } from '../playback/utils'
2524
import { userAgent as parsedUA } from '../../../common/utils'
@@ -77,12 +76,31 @@ export default class ExtCommand {
7776
this.setNewTab(details.tabId)
7877
}
7978
}
79+
80+
this.commandVariablesHandler = (message, _sender, sendResponse) => {
81+
if (message.getVar) {
82+
return sendResponse(this.variables.get(message.variable))
83+
} else if (message.storeVar) {
84+
this.variables.set(message.storeVar, message.storeStr)
85+
return sendResponse(true)
86+
} else if (
87+
message.log &&
88+
output.logs[output.logs.length - 1].message.indexOf(
89+
message.log.message
90+
) === -1
91+
) {
92+
// this check may be dangerous, especially if something else is bombarding the logs
93+
this.logger[message.log.type || 'log'](message.log.message)
94+
return sendResponse(true)
95+
}
96+
}
8097
}
8198

82-
async init(baseUrl, testCaseId, options = {}) {
99+
async init(baseUrl, testCaseId, options = {}, variables) {
83100
this.baseUrl = baseUrl
84101
this.testCaseId = testCaseId
85102
this.options = options
103+
this.variables = variables
86104
this.waitForNewWindow = false
87105
this.windowName = ''
88106
this.windowTimeout = 2000
@@ -116,6 +134,7 @@ export default class ExtCommand {
116134
browser.webNavigation.onCreatedNavigationTarget.addListener(
117135
this.newTabHandler
118136
)
137+
browser.runtime.onMessage.addListener(this.commandVariablesHandler)
119138
}
120139

121140
detach() {
@@ -128,6 +147,7 @@ export default class ExtCommand {
128147
browser.webNavigation.onCreatedNavigationTarget.removeListener(
129148
this.newTabHandler
130149
)
150+
browser.runtime.onMessage.removeListener(this.commandVariablesHandler)
131151
}
132152

133153
getCurrentPlayingWindowSessionIdentifier() {
@@ -360,7 +380,7 @@ export default class ExtCommand {
360380
async setNewTab(tabId) {
361381
if (this.waitForNewWindow) {
362382
this.waitForNewWindow = false
363-
variables.set(this.windowName, tabId)
383+
this.variables.set(this.windowName, tabId)
364384
}
365385
this.windowSession.openedTabIds[
366386
this.getCurrentPlayingWindowSessionIdentifier()
@@ -631,12 +651,12 @@ export default class ExtCommand {
631651
}
632652

633653
doStore(string, varName) {
634-
variables.set(varName, string)
654+
this.variables.set(varName, string)
635655
return Promise.resolve()
636656
}
637657

638658
doStoreWindowHandle(varName) {
639-
variables.set(varName, this.getCurrentPlayingTabId())
659+
this.variables.set(varName, this.getCurrentPlayingTabId())
640660
return Promise.resolve()
641661
}
642662

packages/selenium-ide/src/neo/IO/SideeX/find-select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function getActiveTabForTest() {
4040

4141
export async function find(target) {
4242
try {
43-
const xlatedTarget = xlateArgument(target)
43+
const xlatedTarget = xlateArgument(target, PlaybackState.variables)
4444
const tab = await getActiveTabForTest()
4545
const region = new Region(xlatedTarget)
4646
await browser.windows.update(tab.windowId, {

packages/selenium-ide/src/neo/IO/SideeX/formatCommand.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import browser from 'webextension-polyfill'
19-
import { Logger, Channels, output } from '../../stores/view/Logs'
20-
import variables from '../../stores/view/Variables'
21-
22-
const logger = new Logger(Channels.PLAYBACK)
2318
const nbsp = String.fromCharCode(160)
2419

25-
export function xlateArgument(value) {
20+
export function xlateArgument(value, variables) {
2621
value = value.replace(/^\s+/, '')
2722
value = value.replace(/\s+$/, '')
2823
let r2
@@ -56,7 +51,7 @@ export function xlateArgument(value) {
5651
}
5752
}
5853

59-
export function interpolateScript(script) {
54+
export function interpolateScript(script, variables) {
6055
let value = script.replace(/^\s+/, '').replace(/\s+$/, '')
6156
let r2
6257
let parts = []
@@ -106,24 +101,3 @@ function string(value) {
106101
return ''
107102
}
108103
}
109-
110-
function handleFormatCommand(message, _sender, sendResponse) {
111-
if (message.getVar) {
112-
return sendResponse(variables.get(message.variable))
113-
} else if (message.storeVar) {
114-
variables.set(message.storeVar, message.storeStr)
115-
return sendResponse(true)
116-
} else if (
117-
message.log &&
118-
output.logs[output.logs.length - 1].message.indexOf(message.log.message) ===
119-
-1
120-
) {
121-
// this check may be dangerous, especially if something else is bombarding the logs
122-
logger[message.log.type || 'log'](message.log.message)
123-
return sendResponse(true)
124-
}
125-
}
126-
127-
try {
128-
browser.runtime.onMessage.addListener(handleFormatCommand)
129-
} catch (e) {} // eslint-disable-line

packages/selenium-ide/src/neo/IO/SideeX/playback.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ let ignoreBreakpoint = false
2929
let breakOnNextCommand = false
3030
let executor = undefined
3131

32-
export function play(currUrl, exec) {
32+
export function play(currUrl, exec, variables) {
3333
baseUrl = currUrl
3434
ignoreBreakpoint = false
3535
breakOnNextCommand = false
3636
executor = exec
3737
initPlaybackTree()
38-
return prepareToPlay()
38+
return prepareToPlay(variables)
3939
.then(executionLoop)
4040
.then(finishPlaying)
4141
.catch(catchPlayingError)
@@ -180,12 +180,17 @@ function runNextCommand() {
180180
}
181181
}
182182

183-
function prepareToPlay() {
184-
return executor.init(baseUrl, PlaybackState.currentRunningTest.id, {
185-
// softInit will try to reconnect to the last session for the sake of running the command if possible
186-
softInit:
187-
PlaybackState.isSingleCommandRunning || PlaybackState.isPlayFromHere,
188-
})
183+
function prepareToPlay(variables) {
184+
return executor.init(
185+
baseUrl,
186+
PlaybackState.currentRunningTest.id,
187+
{
188+
// softInit will try to reconnect to the last session for the sake of running the command if possible
189+
softInit:
190+
PlaybackState.isSingleCommandRunning || PlaybackState.isPlayFromHere,
191+
},
192+
variables
193+
)
189194
}
190195

191196
function prepareToPlayAfterConnectionFailed() {

packages/selenium-ide/src/neo/IO/playback/webdriver.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import webdriver from 'browser-webdriver'
1919
import { absolutifyUrl } from './utils'
20-
import variables from '../../stores/view/Variables'
2120
import { Logger, Channels } from '../../stores/view/Logs'
2221
import PlaybackState from '../../stores/view/PlaybackState'
2322

@@ -38,8 +37,9 @@ export default class WebDriverExecutor {
3837
this.logger = new Logger(Channels.PLAYBACK)
3938
}
4039

41-
async init(baseUrl) {
40+
async init({ baseUrl, variables }) {
4241
this.baseUrl = baseUrl
42+
this.variables = variables
4343
this.driver = await new webdriver.Builder()
4444
.withCapabilities(this.capabilities)
4545
.usingServer(this.server)
@@ -219,7 +219,7 @@ export default class WebDriverExecutor {
219219
...script.argv
220220
)
221221
if (optionalVariable) {
222-
variables.set(optionalVariable, result)
222+
this.variables.set(optionalVariable, result)
223223
}
224224
}
225225

@@ -231,33 +231,33 @@ export default class WebDriverExecutor {
231231
...script.argv
232232
)
233233
if (optionalVariable) {
234-
variables.set(optionalVariable, result)
234+
this.variables.set(optionalVariable, result)
235235
}
236236
}
237237

238238
// store commands
239239

240240
async doStore(string, variable) {
241-
variables.set(variable, string)
241+
this.variables.set(variable, string)
242242
return Promise.resolve()
243243
}
244244

245245
async doStoreText(locator, variable) {
246246
const element = await waitForElement(locator, this.driver)
247247
const text = await element.getText()
248-
variables.set(variable, text)
248+
this.variables.set(variable, text)
249249
}
250250

251251
async doStoreValue(locator, variable) {
252252
const element = await waitForElement(locator, this.driver)
253253
const value = await element.getAttribute('value')
254-
variables.set(variable, value)
254+
this.variables.set(variable, value)
255255
}
256256

257257
// assertions
258258

259259
async doAssert(variableName, value) {
260-
const variable = `${variables.get(variableName)}`
260+
const variable = `${this.variables.get(variableName)}`
261261
if (variable != value) {
262262
throw new Error(
263263
"Actual value '" + variable + "' did not match '" + value + "'"

packages/selenium-ide/src/neo/__test__/IO/playback/formatCommand.spec.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,45 @@ import {
1919
interpolateScript,
2020
xlateArgument,
2121
} from '../../../IO/SideeX/formatCommand'
22-
import variables from '../../../stores/view/Variables'
22+
import Variables from '../../../stores/view/Variables'
2323

24-
afterEach(() => {
25-
variables.clear()
24+
let variables
25+
beforeEach(() => {
26+
variables = new Variables()
2627
})
2728

2829
describe('interpolate string', () => {
2930
it('should interpolate false values', () => {
3031
variables.set('a', undefined)
31-
expect(xlateArgument('${a}')).toBe('undefined')
32+
expect(xlateArgument('${a}', variables)).toBe('undefined')
3233
variables.set('a', null)
33-
expect(xlateArgument('${a}')).toBe('null')
34+
expect(xlateArgument('${a}', variables)).toBe('null')
3435
variables.set('a', false)
35-
expect(xlateArgument('${a}')).toBe('false')
36+
expect(xlateArgument('${a}', variables)).toBe('false')
3637
variables.set('a', 0)
37-
expect(xlateArgument('${a}')).toBe('0')
38+
expect(xlateArgument('${a}', variables)).toBe('0')
3839
variables.set('a', '')
39-
expect(xlateArgument('${a}')).toBe('')
40+
expect(xlateArgument('${a}', variables)).toBe('')
4041
})
4142
})
4243

4344
describe('interpolate script', () => {
4445
it('should not interpolate a script without variables', () => {
4546
const script = 'return 1'
46-
expect(interpolateScript(script).script).toEqual(script)
47+
expect(interpolateScript(script, variables).script).toEqual(script)
4748
})
4849
it('should interpolate a script with a single argument', () => {
4950
variables.set('a', 1)
5051
const script = 'return ${a}'
51-
const r = interpolateScript(script)
52+
const r = interpolateScript(script, variables)
5253
expect(r.script).toEqual('return arguments[0]')
5354
expect(r.argv[0]).toBe(1)
5455
})
5556
it('should interpolate a script with multiple arguments', () => {
5657
variables.set('a', 1)
5758
variables.set('b', false)
5859
const script = 'return ${a} + ${a} || ${b}'
59-
const r = interpolateScript(script)
60+
const r = interpolateScript(script, variables)
6061
expect(r.script).toEqual(
6162
'return arguments[0] + arguments[0] || arguments[1]'
6263
)

packages/selenium-ide/src/neo/__test__/playback/playback-tree/command-node.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import Command, { ControlFlowCommandNames } from '../../../models/Command'
1919
import { CommandNode } from '../../../playback/playback-tree/command-node'
20+
import Variables from '../../../stores/view/Variables'
2021

2122
describe('Command Node', () => {
2223
it('control flow check returns correct result', () => {
@@ -69,7 +70,7 @@ describe('Command Node', () => {
6970
''
7071
)
7172
const node = new CommandNode(command)
72-
node._evaluate().then(result => {
73+
node._evaluate({ variables: new Variables() }).then(result => {
7374
expect(result.result).toEqual('Invalid number provided as a target.')
7475
})
7576
})

packages/selenium-ide/src/neo/__test__/stores/view/Variables.spec.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,52 @@
1616
// under the License.
1717

1818
import { useStrict } from 'mobx'
19-
import Variables from '../../../stores/view/Variables'
19+
import Variables from '../../../stores/view/variables'
2020

2121
useStrict(true)
2222

23-
afterEach(() => {
24-
Variables.clear()
25-
})
26-
27-
describe('Variables', () => {
23+
describe('variables', () => {
24+
let variables
25+
beforeEach(() => {
26+
variables = new Variables()
27+
})
2828
it('should clear the map', () => {
2929
const key = 'key1'
3030
const value = 'value1'
31-
Variables.set(key, value)
32-
expect(Variables.storedVars.size).toBe(1)
33-
Variables.clear()
34-
expect(Variables.storedVars.size).toBe(0)
31+
variables.set(key, value)
32+
expect(variables.storedVars.size).toBe(1)
33+
variables.clear()
34+
expect(variables.storedVars.size).toBe(0)
3535
})
3636
it('should add new key and value to the map', () => {
3737
const key = 'key1'
3838
const value = 'value1'
39-
Variables.set(key, value)
40-
expect(Variables.storedVars.size).toBe(1)
39+
variables.set(key, value)
40+
expect(variables.storedVars.size).toBe(1)
4141
})
4242
it('should not add duplicates to the map', () => {
4343
const key = 'key1'
4444
const value = 'value1'
4545
const value2 = 'value2'
46-
Variables.set(key, value)
47-
Variables.set(key, value2)
48-
expect(Variables.storedVars.size).toBe(1)
46+
variables.set(key, value)
47+
variables.set(key, value2)
48+
expect(variables.storedVars.size).toBe(1)
4949
})
5050
it('should get the value correctly from the map', () => {
5151
const key1 = 'key1'
5252
const value1 = 'value1'
5353
const key2 = 'key2'
5454
const value2 = 'value2'
55-
Variables.set(key1, value1)
56-
Variables.set(key2, value2)
57-
expect(Variables.get(key1)).toBe(value1)
55+
variables.set(key1, value1)
56+
variables.set(key2, value2)
57+
expect(variables.get(key1)).toBe(value1)
5858
})
5959
it('should delete the key and the value from the map', () => {
6060
const key = 'key1'
6161
const value = 'value1'
62-
Variables.set(key, value)
63-
expect(Variables.storedVars.size).toBe(1)
64-
Variables.delete(key)
65-
expect(Variables.storedVars.size).toBe(0)
62+
variables.set(key, value)
63+
expect(variables.storedVars.size).toBe(1)
64+
variables.delete(key)
65+
expect(variables.storedVars.size).toBe(0)
6666
})
6767
})

0 commit comments

Comments
 (0)