I'm writing a test with Protrator/Jasmine that is supposed to iterate over a few hundred records. The test has to click several links and there are a few browser.sleep
methods sprinkled throughout. All together it takes about 10 seconds for one iteration of the test.
When setting up the test I had a simple array to loop over:
verifyRecord = (record) ->
...
element(`by`.id('generalSearch').sendKeys(record)
element(`by`.id('submit').click()
browser.sleep 3000
...
expect(result).toBe(true)
records = [
'RCD-001'
'RCD-002'
'RCD-003'
]
describe 'The crucial records', ->
beforeEach ->
browser.ignoreSynchronization = true
it 'should be accessible to the user', ->
browser.get '/path/to/form'
for record in records
verifyRecord(record)
This works just fine. Each verifyRecord
call occurs synchronously, completing before the next is fired. But the list of records is long and changes, so I need to pull it in from a text file rather than hard-coding it in my script.
If I try to use readline
to pull record IDs from a file and iterate over them, they all get sent to the veryifyRecord
function immediately.
fs = require 'fs'
readline = require 'readline'
verifyRecord = ...
describe 'The crucial records', ->
beforeEach ->
browser.ignoreSynchronization = true
it 'should be accessible to the user', ->
browser.get '/path/to/form'
lineReader = readline.createInterface({
input: fs.createReadStream 'records.txt'
})
lineReader.on 'line', (line) ->
verifyRecord(line)
I guess that using readline results in verifyRecord
being called asynchronously. If I add a console.log(record)
inside the verifyRecord
method, all of the record IDs are dumped to the terminal nearly instantly.
I've also tried loading the results up into a local array and then iterating over that:
records = []
lineReader.on 'line', (line) ->
records.push line
for record in records
verifyRecord(record)
This doesn't work either.
How can I make the lineReader
wait for each verifyRecord
call to complete before calling the method with the next record in the list?