【问题标题】:Protractor test script is not getting object data from function量角器测试脚本没有从函数中获取对象数据
【发布时间】:2019-12-12 09:03:47
【问题描述】:

在获取填充在函数中以测试脚本的对象数据方面需要帮助。以下是详细信息:

candidate Data = function() {
    "firstName": "firstname",
    "lastName": "lastname", 
    "mobilePhone": "phone",
    "primaryEmail": "email"
};
module.exports = new candidateData();
test script is as As below:
var testData = require('candidateData');
var myFunction = require('getData');

testCandiate = {};
testCandidate = testData;

it('Get Candidate Data', function(){
    myFunction.getCandidateData(testCandidate);
    console.log(`FirstName is -: ${testCandidate.firstName}:-`);
    console.log(`LastName is -: ${testCandidate.firstName}:-`);
    console.log(`Phone is: ${testCandidate.mobilePhone}:-`);
    console.log(`Primary Email is -: ${testCandidate.primaryEmail}:-`);
});

函数在单独的 js 文件中定义为: // 因为我是从页面的编辑字段中捕获数据,所以使用 get Attribute value 来获取值

var getData = function() {
    this.getCandidateData = function(testObj){  
        page.firstname.getAttribute("value").then(function(fname){
            testObj.firstName = fname;
        }); 
        page.lastname.getAttribute("value").then(function(lname){
            testObj.lastName = lname;
        });
        page.phone.getAttribute("value").then(function(phone){
            testObj.mobilePhone = phone;
        });
        page.email.getAttribute("value").then(function(email){
            testObj.primaryEmail = email;
        });
    };
};
module.exports = new getData();

运行脚本后,我得到的控制台日志结果如下,而不是字段上的填充数据:

名字是 -: firstName :- 姓氏是-:姓氏:- 手机是 -: mobilePhone :- 主要电子邮件是 -: primaryEmail :-

请帮助。

【问题讨论】:

  • 第一件事:语法错误是偶然的还是您的代码的精确副本?

标签: protractor 2-way-object-databinding


【解决方案1】:

这不是量角器问题,而是您对承诺的基本误解,因为您正处于竞争状态。您调用 myfunction.getCandidateData() 调用 4 个返回承诺的独立函数。虽然这些函数正在获取您想要的属性 javascript 在您的代码中继续。这意味着 console.log 语句立即触发,无需等待四个 getAttribute 承诺解决

要解决此问题,您必须将 getCandidateData 转换为返回承诺的函数。作为概念证明,试试这个看看 firstName 将被填充:

var testData = require('candidateData');
var myFunction = require('getData');

testCandiate = {};
testCandidate = testData;

it('Get Candidate Data', function(){
    myFunction.getCandidateData(testCandidate).then(() => {
        console.log(`FirstName is -: ${testCandidate.firstName}:-`);
    });
    // console.log(`LastName is -: ${testCandidate.firstName}:-`);
    // console.log(`Phone is: ${testCandidate.mobilePhone}:-`);
    // console.log(`Primary Email is -: ${testCandidate.primaryEmail}:-`);
});

var getData = function() {
  this.getCandidateData = function(testObj){  
      return page.firstname.getAttribute("value").then(function(fname){
          testObj.firstName = fname;
      }); 
      /*
      page.lastname.getAttribute("value").then(function(lname){
          testObj.lastName = lname;
      });
      page.phone.getAttribute("value").then(function(phone){
          testObj.mobilePhone = phone;
      });
      page.email.getAttribute("value").then(function(email){
          testObj.primaryEmail = email;
      });
      */
  };
};
module.exports = new getData();

【讨论】:

  • 谢谢基隆!让我试试看。
猜你喜欢
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多