Вопрос

So I'm trying to figure out a way to get a value from the browser when a test is running with DalekJS. As far as I can tell, it's not currently possible. I even had a look at hacking the Dalek code, but since I know very, very little about CommonJS and NodeJS other than installing and running things, I figured I might actually ask around first.

What I'm trying to do is something like:

// Non-working example
module.exports = {
  'My Test': function(test) {
    var foo;

    test
    .open('http://localhost/mysite')
    .execute(function(){
      foo = document.getElementById('bar');
    })
    .type('#myField', foo)
    .done();
  }
}

Now I know why this code doesn't work: because the execute() function is executed in the context of the browser window (as though I was typing it into the console).

The question I'm asking is: is there a way I can retrieve a value to use in my test?


Context information: My use case is that I'm testing an E2E scenario where a form is submitted and a code is provided to the user. This code then needs to be entered on another page. I could potentially set up mock values for the purpose of testing, but then it's not a true scenario.

Thanks in advance!

Это было полезно?

Решение

Yes,

that is a usecase we haven't implemented yet, but until we haven't found a proper solution, you can use a workaround:

module.exports = {
  'My Test': function(test) {
    test
      .open('http://localhost/mysite')
      .execute(function(){
        var foo = document.getElementById('bar').innerText;
        document.getElementById('myField').setAttribute('value', foo);
      })
      .done();
  }
};

This method has some problems, none of the eventhandlers like keyup, keydown etc. will be fired when adding the value to the field in this way. If your code doesn't rely on them, you are good to go. Else you would have to wait some weeks until the new version is out, which will provide a better solution for such scenarios.

Hope that helps.

Cheers Sebastian

Другие советы

Based on the documented example of the .log.message function, you can do something like the following:

module.exports = {
  'My Test': function(test) {
    test
      .open('http://localhost/mysite')
      .execute(function(){
        var foo = document.getElementById('bar').value;
        this.data('foo', foo);
      })
      .type('#myField', test.data('foo'))
      .done();
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top