2017-04-09 76 views
0

我做終端使用protractor.When我啓動,在我看來這個消息的測試量角器:不要等待元素出現在大教堂

混亂應用端到端測試菜單0項應該顯示結束測試第一個評論作者爲 消息: 失敗:不使用定位器找到的元素:by.model(「FiltText」)

我怎樣才能讓量角器等到元素出現在DOM?

相應量角器配置代碼是:由於使用量角器控制流

var EC = protractor.ExpectedConditions; 
var timeout = 5000; // in miliseconds 
// Waits for the element to be present on the dom. 
browser.wait(EC.presenceOf(element(by.model("FiltText"))), timeout); 
// Here you can safely use your element, because here it is guaranted to be present. 

exports.config = { 
    allScriptsTimeout:11000, 

    specs: [ 
     'e2e/*.js' 
     ], 
    capabilities: { 
    'browserName': 'chrome' 
    }, 

baseUrl: 'http://localhost:3001/', 

framework: 'jasmine', 
directConnect: true, 

jasmineNodeOpts: { 
    showColors: true, 
    defaultTimeoutInterval: 30000 
} 
}; 

scenarios.js包含E2E測試代碼

describe('menu 0 item', function() { 
beforeEach(function() { 

    browser.get('index.html#/menu/0'); 


}); 

it('should have a name', function() { 
     var name = element(by.binding('dish.name')); 
     expect(name.getText()). 
     toEqual('Uthapizza Hot $4.99'); 
}); 

it('should show the number of comments as', function() { 

    expect(element.all(by.repeater('comment in dish.comments')) 
     .count()).toEqual(5); 


}); 

it('should show the first comment author as', function() { 
     element(by.model('FiltText')).sendKeys('author'); 

     expect(element.all(by.repeater('comment in dish.comments')) 
     .count()).toEqual(5); 

     var author = element.all(by.repeater('comment in dish.comments')) 
        .first().element(by.binding('comment.author')); 

     expect(author.getText()).toContain('25 Cent'); 

}); 
    }); 
+1

可能的複製http://stackoverflow.com/questions/30205235 /量角器等待元素爲dom) –

+0

錯誤中測試的名稱不在您提供的規範中的任何位置。代碼在哪裏? – Gunderson

+0

我添加了測試代碼。 PLZ檢查它。 – munirah

回答

2

可以使用ExpectedConditions ,這個命令直到纔會完成210可見或時間大於超時。

點擊此處瞭解詳情: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions

+0

我做了你的建議,但沒有奏效。這條消息在我看來:失敗:在5001ms後等待超時 – munirah

+0

我實際在此代碼之前添加代碼:element(by.model('FiltText'))。sendKeys('author');因爲當我把它放在第一個問題後,再次出現 – munirah

+0

你得到超時,因爲你的元素花費超過5秒產卵或者你的元素根本不會產生。您必須測量「FiltText」在屏幕上顯示需要多長時間。如果需要10秒鐘,那麼你會得到超時錯誤 – FCin

0

有很多方法可以實現這一點,例如:

  1. 使用protractor.ExpectedConditions API

  2. 等待幾秒鐘,以確保一切加載,在這裏是示例代碼:

utils.js

'use strict'; 

/** 
* Navigate to an url and wait some seconds 
* @param {string} path The path 
* @param {seconds} [seconds] The number of seconds to wait for 
* @returns {Promise} 
* @see waitSomeSeconds 
*/ 
function navigateAndWait(path, seconds) { 
    return browser.get(path) 
    .then(function() { 
     // Wait some seconds until page is fully loaded 
     return waitSomeSeconds(seconds); 
    }); 
} 

/** 
* Wait some seconds (default is 3) 
* @param {int} [seconds] 
* @returns {Promise} 
*/ 
function waitSomeSeconds(seconds) { 
    return browser.sleep((seconds || 3) * 1000); 
} 


module.exports = { 
    navigateAndWait: navigateAndWait, 
    waitSomeSeconds: waitSomeSeconds 
} 

然後你就可以在測試中使用它:

**sampleSpec.js** 

'use strict'; 

var util = require('./utils'); 

describe('Products', function() { 

it('should be redirected to products page after login', function (done) { 
    util.waitSomeSeconds().then(function() { 
     browser.getCurrentUrl().then(function (value) { 
     var relativePath = value.substring(value.lastIndexOf('/')); 
     expect(relativePath).toEqual('/products'); 
     done(); 
     }); 
    }); 

it('should navigate to products list', function() { 
    return util.navigateAndWait('/products'); 
    }); 

    it('should display title with correct data', function() { 
    expect(element(by.css('h1')).getText()).toBe('Newest products'); 
    }); 

}); 
[等待元素在DOM量角器(的