2015-07-03 160 views
16

我想在量角器上設置全局變量以在所有描述塊中使用。量角器設置全局變量

var glob = 'test'; 

describe('glob test', function() { 
    it('should set glob', function() { 
     browser.get('http://example.com/test'); 
     browser.executeScript(function() { 
      window.glob = glob; 
     }); 
    });  
}); 

但這返回我以下錯誤:

Message: 
[firefox #2]  UnknownError: glob is not defined 

我也看了一下這個問題:protractor angularJS global variables

所以我試圖設置變量水珠在這樣conf.js:

exports.config = { 
    ..., 
    onPrepare: function() { 
     global.glob = 'test'; 
    } 
}; 

仍然有同樣的錯誤。

如何在量角器測試中正確添加全局變量?

回答

33

它可以設置從量角器配置文件全局與params財產的幫助:

exports.config = { 
    // ... 

    params: { 
     glob: 'test' 
    } 

    // ... 
}; 

而且你可以在使用browser.params.glob規格訪問它。

查看reference config file

The params object will be passed directly to the Protractor instance, and can be accessed from your test as browser.params. It is an arbitrary object and can contain anything you may need in your test. This can be changed via the command line as:

protractor conf.js --params.glob 'other test'

更新:

docs for browser.executeScript

If the script is provided as a function object, that function will be converted to a string for injection into the target window. Any arguments provided in addition to the script will be included as script arguments and may be referenced using the arguments object.

因此,在這種情況下,JavaScript的範圍不工作,你的功能傳遞給browser.executeScript不會有像browser這樣的規範有任何關閉變量。但是你可以明確地傳遞這些變量:

browser.executeScript(function (glob) { 

    // use passed variables on the page 
    console.log(glob); 

}, browser.params.glob); 
+0

我試圖使用,唯一的問題是,如果我嘗試在browser.executeScript的回調中獲取瀏覽器對象,我得到UnknownError:未知錯誤:未定義瀏覽器 – pietrovismara

+0

我已將'browser.executeScript'的工作解決方案添加到答案中。 –

+0

現在它工作完美!感謝您的時間。 – pietrovismara

16

您也可以使用globalonPrepare()在設置全局變量:

onPrepare: function() { 
    global.myVariable = "test"; 
}, 

然後,您只需使用myVariable整個測試過程中爲是。

這實際上是如何protractorbrowser和其他內置的全局變量were made available globally

Runner.prototype.setupGlobals_ = function(browser_) { 
    // Export protractor to the global namespace to be used in tests. 
    global.protractor = protractor; 
    global.browser = browser_; 
    global.$ = browser_.$; 
    global.$$ = browser_.$$; 
    global.element = browser_.element; 
    global.by = global.By = protractor.By; 

    // ... 
} 

注意,這種方法你污染你的全局範圍/命名空間,要小心。

+0

我一直在努力這一段時間 - 使用這種方法的作品除了當我做一些像'global.foo = require('./ foo.js');''在哪裏'foo.js'是一些簡單的東西'module.exports ='asdf';' –

-4

我知道我有點晚了答案,但這裏的設置全局變量的另一種方式,可以在整個文件

describe("Some Global most outer describe", function(){ 
    var glob = "some global variable"; 
    var blob = "some other global variable"; 

    it('should test glob', function(){ 
     expecte(glob).toEqual("some global variable"); 
    }); 

    it('should test blob', function(){ 
     expecte(glob).toEqual("some other global variable"); 
    }); 

    describe('Some inner describe', function(){ 
     //Some other inner tests can also see glob and blob 
    }); 
}); 
+2

** glob **和** blob **不能在描述範圍之外使用(「某些全局最外層描述」,函數()) 取而代之的是在外面聲明它們,啓動並使用它們,無論通過該文件。 – Subhash

+0

這和我的例子一樣,只是你在describe函數中移動了變量聲明。它不起作用。 – pietrovismara

+0

它的作品,如果你有其他描述裏面最外層描述。這就是我前一段時間使用它的原因......現在我同意它不是最好的解決方案。 – Haseeb