Protractor shows variable value as “undefined”












1















We are defining variables from the elements on one page of the website, clicking on the edit button, which is opening the next page. In this page we need to assert that the data captured on the earlier page matches the data shown on the 2nd page. Our problem is, once the test moves to the 2nd page, it fails to recall the variables that we defined on the 1st page. below is our code snippets:



     it ('Student ID Validation', function(){

// get rows
var rows = tableData_Dashboard.all(by.tagName("tr"));

// get cell values
var cells = rows.all(by.tagName("td"));

var Student_ID = cells.get(0).getText().then(function(SID){
console.log(SID);
});
Edit_Button_1.click();
browser.sleep(2000);
expect(Student_ID_on_Reg_Page.getAttribute('value')).toEqual(Student_ID);


after execution, we get the following error



 Message:
Expected '123456' to equal undefined.


We were suspecting that it may be due to asynchronization, but that is not the case. the test moves to page 2 after it stores the variable from page 1, so we are at a loss why this is happening. How can we fix this and use the variables for assertion purpose?










share|improve this question



























    1















    We are defining variables from the elements on one page of the website, clicking on the edit button, which is opening the next page. In this page we need to assert that the data captured on the earlier page matches the data shown on the 2nd page. Our problem is, once the test moves to the 2nd page, it fails to recall the variables that we defined on the 1st page. below is our code snippets:



         it ('Student ID Validation', function(){

    // get rows
    var rows = tableData_Dashboard.all(by.tagName("tr"));

    // get cell values
    var cells = rows.all(by.tagName("td"));

    var Student_ID = cells.get(0).getText().then(function(SID){
    console.log(SID);
    });
    Edit_Button_1.click();
    browser.sleep(2000);
    expect(Student_ID_on_Reg_Page.getAttribute('value')).toEqual(Student_ID);


    after execution, we get the following error



     Message:
    Expected '123456' to equal undefined.


    We were suspecting that it may be due to asynchronization, but that is not the case. the test moves to page 2 after it stores the variable from page 1, so we are at a loss why this is happening. How can we fix this and use the variables for assertion purpose?










    share|improve this question

























      1












      1








      1








      We are defining variables from the elements on one page of the website, clicking on the edit button, which is opening the next page. In this page we need to assert that the data captured on the earlier page matches the data shown on the 2nd page. Our problem is, once the test moves to the 2nd page, it fails to recall the variables that we defined on the 1st page. below is our code snippets:



           it ('Student ID Validation', function(){

      // get rows
      var rows = tableData_Dashboard.all(by.tagName("tr"));

      // get cell values
      var cells = rows.all(by.tagName("td"));

      var Student_ID = cells.get(0).getText().then(function(SID){
      console.log(SID);
      });
      Edit_Button_1.click();
      browser.sleep(2000);
      expect(Student_ID_on_Reg_Page.getAttribute('value')).toEqual(Student_ID);


      after execution, we get the following error



       Message:
      Expected '123456' to equal undefined.


      We were suspecting that it may be due to asynchronization, but that is not the case. the test moves to page 2 after it stores the variable from page 1, so we are at a loss why this is happening. How can we fix this and use the variables for assertion purpose?










      share|improve this question














      We are defining variables from the elements on one page of the website, clicking on the edit button, which is opening the next page. In this page we need to assert that the data captured on the earlier page matches the data shown on the 2nd page. Our problem is, once the test moves to the 2nd page, it fails to recall the variables that we defined on the 1st page. below is our code snippets:



           it ('Student ID Validation', function(){

      // get rows
      var rows = tableData_Dashboard.all(by.tagName("tr"));

      // get cell values
      var cells = rows.all(by.tagName("td"));

      var Student_ID = cells.get(0).getText().then(function(SID){
      console.log(SID);
      });
      Edit_Button_1.click();
      browser.sleep(2000);
      expect(Student_ID_on_Reg_Page.getAttribute('value')).toEqual(Student_ID);


      after execution, we get the following error



       Message:
      Expected '123456' to equal undefined.


      We were suspecting that it may be due to asynchronization, but that is not the case. the test moves to page 2 after it stores the variable from page 1, so we are at a loss why this is happening. How can we fix this and use the variables for assertion purpose?







      javascript node.js jasmine protractor






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 18:16









      nhrcptnhrcpt

      383423




      383423
























          2 Answers
          2






          active

          oldest

          votes


















          2














          The problem is that you've specified the then() callback where you just log the value but don't return it:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          });


          As nothing is returned, Student_ID would become a promise which would resolve into undefined.



          You either need a return:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          return SID;
          });


          Or, remove the custom callback completely:



          var Student_ID = cells.get(0).getText();





          share|improve this answer


























          • Thanks a lot. This is exactly what we were missing.

            – nhrcpt
            Jan 2 at 18:37



















          0














          actually, the following part is causing the problem. Once we removed this part, the test is working fine.



          .then(function(SID){
          console.log(SID);
          });






          share|improve this answer



















          • 1





            be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

            – Sergey Pleshakov
            Jan 3 at 15:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011231%2fprotractor-shows-variable-value-as-undefined%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          The problem is that you've specified the then() callback where you just log the value but don't return it:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          });


          As nothing is returned, Student_ID would become a promise which would resolve into undefined.



          You either need a return:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          return SID;
          });


          Or, remove the custom callback completely:



          var Student_ID = cells.get(0).getText();





          share|improve this answer


























          • Thanks a lot. This is exactly what we were missing.

            – nhrcpt
            Jan 2 at 18:37
















          2














          The problem is that you've specified the then() callback where you just log the value but don't return it:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          });


          As nothing is returned, Student_ID would become a promise which would resolve into undefined.



          You either need a return:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          return SID;
          });


          Or, remove the custom callback completely:



          var Student_ID = cells.get(0).getText();





          share|improve this answer


























          • Thanks a lot. This is exactly what we were missing.

            – nhrcpt
            Jan 2 at 18:37














          2












          2








          2







          The problem is that you've specified the then() callback where you just log the value but don't return it:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          });


          As nothing is returned, Student_ID would become a promise which would resolve into undefined.



          You either need a return:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          return SID;
          });


          Or, remove the custom callback completely:



          var Student_ID = cells.get(0).getText();





          share|improve this answer















          The problem is that you've specified the then() callback where you just log the value but don't return it:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          });


          As nothing is returned, Student_ID would become a promise which would resolve into undefined.



          You either need a return:



          var Student_ID = cells.get(0).getText().then(function(SID){
          console.log(SID);
          return SID;
          });


          Or, remove the custom callback completely:



          var Student_ID = cells.get(0).getText();






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 18:36

























          answered Jan 2 at 18:28









          alecxealecxe

          329k72654875




          329k72654875













          • Thanks a lot. This is exactly what we were missing.

            – nhrcpt
            Jan 2 at 18:37



















          • Thanks a lot. This is exactly what we were missing.

            – nhrcpt
            Jan 2 at 18:37

















          Thanks a lot. This is exactly what we were missing.

          – nhrcpt
          Jan 2 at 18:37





          Thanks a lot. This is exactly what we were missing.

          – nhrcpt
          Jan 2 at 18:37













          0














          actually, the following part is causing the problem. Once we removed this part, the test is working fine.



          .then(function(SID){
          console.log(SID);
          });






          share|improve this answer



















          • 1





            be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

            – Sergey Pleshakov
            Jan 3 at 15:21
















          0














          actually, the following part is causing the problem. Once we removed this part, the test is working fine.



          .then(function(SID){
          console.log(SID);
          });






          share|improve this answer



















          • 1





            be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

            – Sergey Pleshakov
            Jan 3 at 15:21














          0












          0








          0







          actually, the following part is causing the problem. Once we removed this part, the test is working fine.



          .then(function(SID){
          console.log(SID);
          });






          share|improve this answer













          actually, the following part is causing the problem. Once we removed this part, the test is working fine.



          .then(function(SID){
          console.log(SID);
          });







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 18:27









          nhrcptnhrcpt

          383423




          383423








          • 1





            be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

            – Sergey Pleshakov
            Jan 3 at 15:21














          • 1





            be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

            – Sergey Pleshakov
            Jan 3 at 15:21








          1




          1





          be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

          – Sergey Pleshakov
          Jan 3 at 15:21





          be careful with that. It may not work all the time. To understand what I mean research the difference of promises with asynchronous functions in JS

          – Sergey Pleshakov
          Jan 3 at 15:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011231%2fprotractor-shows-variable-value-as-undefined%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas