4

I am trying to read an input for an operation in protractor. I'm trying to send an ajax request from within protractor. I need to send a unique value (which I'm calling groupCode) to a part of a JSON object that I want to send to the server.

So first I tried setting up a hidden input that could be updated. I have tried:

<div style='hidden' >
    <input 
        id="group-sendgrid-hidden-input" 
        ng-model='groupCode' 
        value='{{groupCode}}' 
        ng-init='groupCode=dangdangdang'
    >
</div>

Also this attempt to read the model value:

<div style='hidden' >
    <input 
        id="group-sendgrid-hidden-input" 
        ng-model='groupCode' 
        value='{{groupCode}}' 
        ng-init='groupCode=dangdangdang'
    >
   {{groupCode}}
</div>

Now, I can see in the Angular console that this value updates as I except. Meaning that $scope.groupCode does resolve to 'dangdangdang'. So I don't thing this is the issue. From here, I'm hoping to read the groupCode string in a protractor test as followed:

Here is some of what I've tried:

var groupCodeModel      = element(by.model('groupCode'));
var groupCodeBinding    = element(by.binding('groupCode'));
var placeholder         = groupCodeBinding.getText();

also in an attempt to get the value in a more direct way (I think)

var groupCode = element(
    by.id('group-sendgrid-hidden-input')
).getAttribute('value');


// and then later I want to do this:
var sendgridData = {envelope: 'what', test: groupCode};

The problem is that no matter what I try the value of the groupCode data never seems to resolve to a string.

  1. I'm not able to console.log(groupCode);
  2. This fails: var sendgridDataString = JSON.stringify(sendgridData);

Every time I get something like this:

{ ptor_: 
    { controlFlow: [Function],
      schedule: [Function],
      getSession: [Function],
      getCapabilities: [Function],
      quit: [Function],
      actions: [Function],....

And I'm sure the data I need is here, I'm just too new to protractor to figure it out. It is dope tech, however. Thank you.

Edit:

I have now also tried:

var groupCode = element(by.id('group-sendgrid-hidden-input')).evaluate('groupCode').then(function(groupCode){

        console.log('test: ' + groupCode);
        return value;
});

as well as :

var groupCode = element(by.id('group-sendgrid-hidden-input')).getAttribute('value').then(function(groupCode){

        console.log('test: ' + value);

    return value;
});

And in this second one, groupCode returns:

{ then: [Function: then],
    cancel: [Function: cancel],
    isPending: [Function: isPending] }

Is still seems impossible to just get a string like groupCode = 'just some words'. Which is what I need to hand to another function.

I know this must be possible, but it is driving me to maddness. I will continue to read the docs.

usumoio
  • 3,500
  • 6
  • 31
  • 57

2 Answers2

2

getAttribute() as many other protractor methods returns a promise, you need to resolve it:

element(by.id('group-sendgrid-hidden-input')).getAttribute('value').then(function (value) {
    console.log(value);
});

Here are the relevant pages that should help to understand the concept of promises:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Okay, thank you. I reviewed these docs. I can't see why `console.log(value);` is returning null (or possibly undefined). I will keep reading. Is there a function call I am missing? – usumoio Mar 31 '15 at 03:37
  • @usumoio the key trick here is to use `then()` function to get an actual promise value once it's resolved. Hope it makes sense. – alecxe Mar 31 '15 at 04:03
0

We discuss this question further and answer the original question here at this other question I asked about implementing the solution listed here, which is almost complete and finished with an expect condition that I have to write inside the it test.

Cannot return string from value of element in protractor test

Community
  • 1
  • 1
usumoio
  • 3,500
  • 6
  • 31
  • 57