2017-04-09 57 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量角器(的