簡體   English   中英

使用非角度頁面上的量角器更新全局變量

[英]Update global variable with protractor on non-Angular page

對它。 我有一個要導出的全局變量,因此可以在以下規格中使用該值。 但是由於量角器無法同步工作,因此導出會在變量更新為正確的值之前進行。 該操作是單擊按鈕,在此處創建了玩家,我需要導出用戶名。 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;

因為您使用的是瀏覽器忽略同步,所以您的測試可能會嵌套了then語句,因為您需要等待promise解決。 您可以將它們放在一個then語句中,而不是創建多個then語句。 同樣,您可以使用global.username = username將變量綁定到全局名稱空間,或使用browser.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');

所以這解決了我的問題,我導出一個字符串而不是變量。...用錯誤的標記搜索了google。 這放置在底部的第一個描述外部。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM