【问题标题】:Update global variable with protractor on non-Angular page在非 Angular 页面上使用量角器更新全局变量
【发布时间】:2016-11-30 14:41:55
【问题描述】:

权利。我有一个要导出的全局变量,因此我可以在以下规范中使用该值。但是由于量角器没有同步工作,因此在变量更新为正确值之前发生了导出。 该操作是单击创建播放器的按钮,我需要导出用户名。 Console.log 包含正确的值,并且如果我设置 globalUsername = ""; 任何人都可以帮助了解如何使此导出同步,因此它将等待所有描述完成或变量更新。

describe ("Quick add player", function() {

    it ("New player is created and credentials are displayed", function() {
        browser.ignoreSynchronization = true;

        var playerData1 = playerData.getText().then(function(text) {
            console.log("1: ", text);
            return text.split("\n")[0];
            //console.log(text.split(" ")[1]);
        });


        var globalUsername = "1234";

        playerData1.then(function(text) {
            globalUsername = text.split(" ")[1];
            //expect(globalUsername).not.toEqual("");
            console.log("*****************\n" + globalUsername);    
        });
    });
});
module.exports = globalUsername;

【问题讨论】:

    标签: angularjs protractor global-variables specs


    【解决方案1】:

    因为您使用的是浏览器忽略同步,所以您的测试可能会嵌套 then 语句,因为您需要等待 Promise 解决。您可以将它们放入单个 then 语句中,而不是创建多个 then 语句。此外,您可以使用 global.username = username 将变量绑定到全局命名空间,或者使用 browser.username = username 将其添加到全局浏览器对象

    describe ("Quick add player", function() {
    
      it ("New player is created and credentials are displayed", function() {
        browser.ignoreSynchronization = true;
        global.username = "1234";
        playerData.getText().then(function(text) {
          console.log("1: ", text);
          global.username = text.split("\n")[0].split(" ")[1];
          console.log("*****************\n" + global.username);
        });
      });
    });
    
    // access using global.username tied to the global namespace
    // instead of var username = require('thisfile');
    

    【讨论】:

      【解决方案2】:

      所以这解决了我的问题,我导出一个字符串而不是变量....用错误的标签搜索谷歌。 它位于底部的第一个描述之外。

      module.exports = {
       exportUsername: function () {
        return globalUsername;
       }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多