問題描述
[
上圖我正在運行 protractor Conf.js,具有特定的環境名稱,存儲在 JSON 文件中
The above pic i am run protractor Conf.js with particular environmental name which is stored in JSON file
如何僅在量角器測試用例中測試特定的環境 URL?
how to test particular environmental URL only in the protractor test case?
推薦答案
FIRST METHOD - 您必須在命令行中使用 params
變量傳遞參數.更新您的 conf.js
文件以包含名為 baseUrl
的參數和其他 url 變量,如下所示 -
FIRST METHOD - You have to pass the parameters using params
variable in command line. Update your conf.js
file to include a parameter called baseUrl
and other url variables as shown below -
params: {
baseUrl: "http://default_url" //provide your default url to be used
}
稍后在命令提示符中傳遞該值.方法如下-
later pass the value in the command prompt. Here's how -
protractor conf.js --params.baseUrl 'http://www.google.com'
無論您在哪里有代碼來獲取規范中的 url,請使用以下代碼 -
Wherever you have code to get the url in your spec's, use the following code -
browser.get(browser.params.baseUrl);
第二種方法 - 如果您不想每次都將 url 傳遞給 params
對象,那么您可以將它們存儲在 conf.js 中,甚至您的規格文件并調用它們.這是一個例子-
SECOND METHOD - If at all you don't want to pass the url to the params
object everytime, then you can store them in your conf.js or even your specs file and call them. Here's an example -
你的 conf.js
文件 -
params: {
baseUrl: ""
},
onPrepare: function(){
switch(browser.params.baseUrl){
case 'firsturl':
browser.get("http://firsturl.com"); //replace firsturl with your actual url
break;
case 'secondurl':
browser.get("http://www.secondurl.com");
break;
default:
browser.get("http://www.defaulturl.com");
}
}
現在通過命令行傳遞您要使用的 url -
Now pass the url's that you want to use through command line -
protractor conf.js --params.baseUrl 'firsturl' //to get first url
protractor conf.js //to open default url
第三種方法 - 如果您在運行具有許多規范的測試套件時遇到問題,在這種情況下,上述第二種方法將不起作用.您需要在每個測試規范文件中使用 browser.get()
,在這種情況下使用以下方法 -
THIRD METHOD - If at all you have a problem of running a test suite with many spec's, in that case above second method wouldn't work. You need to use browser.get()
in each of your test spec files, in such cases use following method -
更新您的 conf.js 文件 -
Update your conf.js file -
params: {
baseUrl: "",
url: ""
},
onPrepare: function(){
switch(browser.params.baseUrl){
case 'firsturl':
browser.params.url = "http://firsturl.com"; //replace firsturl with your actual url
break;
case 'secondurl':
browser.params.url = "http://www.secondurl.com";
break;
default:
browser.params.url = "http://www.defaulturl.com";
}
}
你的命令行命令 -
protractor conf.js --params.baseUrl 'firsturl' //to get first url
protractor conf.js //to open default url
您的測試規范文件需要包含 browser.get()
命令.方法如下-
Your test spec files need to include the browser.get()
command. Here's how -
browser.get(browser.params.url);
希望對你有幫助.
這篇關于如何使用量角器 conf js 測試單獨的環境名稱?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!