0

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?

Ben Harold
  • 6,242
  • 6
  • 47
  • 71
  • Possible duplicate of [How to wait for function to finish before continuning in Node.js](http://stackoverflow.com/questions/28849900/how-to-wait-for-function-to-finish-before-continuning-in-node-js) – gotnull May 19 '16 at 01:59

1 Answers1

0

Have you looked at using Mocha for this?

Alternately, instead of using the done() callback, you may return a Promise. This is useful if the APIs you are testing return promises instead of taking callbacks:

beforeEach(function() {
  return db.clear()
    .then(function() {
      return db.save([tobi, loki, jane]);
    });
});

describe('#find()', function() {
  it('respond with matching records', function() {
    return db.find({ type: 'User' }).should.eventually.have.length(3);
  });
});
gotnull
  • 26,454
  • 22
  • 137
  • 203