如何解释在量角器中使用 browser.sleep
How to explain using browser.sleep in protractor
我是量角器的新手,我正在尝试测试弹出事件,
这是我的代码:
describe('popover directive', function() {
var buttons= element.all(by.tagName('button'));
it('should show the popover title by clicking', function() {
var popTitle="testTitle";
var popContent="testContent";
element(by.model('title')).clear().sendKeys(popTitle);
element(by.model('content')).clear().sendKeys(popContent).then(function(){
buttons.get(0).click().then(function(){
browser.sleep(1000);
expect(element(by.className('popover-title')).getText()).toMatch(popTitle);
expect(element(by.className('popover-content')).getText()).toMatch(popContent);
});
});
buttons.get(0).click();
browser.sleep(1000);
expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);
});
});
1.If 我不加browser.sleep()
这样的延迟时间代码,测试会失败并显示信息:
NoSuchElementError: No element found using locator: By.className('popover-title')
有没有可能不加延迟时间码...像browser.sleep()
?如果那是不可能的,那么如何合理地设置睡眠时间呢?这与 CSS 动画有什么关系吗?
2.Using browser.waitForAngular()
或 click().then(function(){...})
而不是 browser.sleep()
似乎不起作用,会收到相同的错误消息。
如果有人能回答这些问题就太好了,非常感谢。
不使用浏览器,而是声明一个量角器实例:
ptor = protractor.getInstance();
ptor.ignoreSynchronization = true;
然后使用:
ptor.get
而不是浏览器。
您不得不添加睡眠的原因可能是因为您的动画。许多动画使用 setTimeout
,Angular(因此量角器)不知道也不等待。 angular相当于setTimeout
是它的$timeout
服务。
但通常您无法更改动画库以停止使用setTimeout
。要在量角器中使用它,请使用 browser.wait
超时(不是睡眠)。
buttons.get(0).click();
browser.wait(function() {
return element(by.css('[class="popover fade top in"]')).isPresent().then(function(present) {
return !present;
});
// or by checking any other element to know that the animation completed
}, 1000);
expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);
使用 Protractor 1.7,您将能够使用 expectedConditions 库,因此您可以执行以下操作:
var EC = protractor.ExpectedConditions
buttons.get(0).click();
var e = element(by.css('[class="popover fade top in"]'));
browser.wait(EC.not(EC.presenceOf(e)), 1000);
expect(e.isPresent()).toBe(false);
我是量角器的新手,我正在尝试测试弹出事件, 这是我的代码:
describe('popover directive', function() {
var buttons= element.all(by.tagName('button'));
it('should show the popover title by clicking', function() {
var popTitle="testTitle";
var popContent="testContent";
element(by.model('title')).clear().sendKeys(popTitle);
element(by.model('content')).clear().sendKeys(popContent).then(function(){
buttons.get(0).click().then(function(){
browser.sleep(1000);
expect(element(by.className('popover-title')).getText()).toMatch(popTitle);
expect(element(by.className('popover-content')).getText()).toMatch(popContent);
});
});
buttons.get(0).click();
browser.sleep(1000);
expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);
});
});
1.If 我不加browser.sleep()
这样的延迟时间代码,测试会失败并显示信息:
NoSuchElementError: No element found using locator: By.className('popover-title')
有没有可能不加延迟时间码...像browser.sleep()
?如果那是不可能的,那么如何合理地设置睡眠时间呢?这与 CSS 动画有什么关系吗?
2.Using browser.waitForAngular()
或 click().then(function(){...})
而不是 browser.sleep()
似乎不起作用,会收到相同的错误消息。
如果有人能回答这些问题就太好了,非常感谢。
不使用浏览器,而是声明一个量角器实例:
ptor = protractor.getInstance();
ptor.ignoreSynchronization = true;
然后使用:
ptor.get
而不是浏览器。
您不得不添加睡眠的原因可能是因为您的动画。许多动画使用 setTimeout
,Angular(因此量角器)不知道也不等待。 angular相当于setTimeout
是它的$timeout
服务。
但通常您无法更改动画库以停止使用setTimeout
。要在量角器中使用它,请使用 browser.wait
超时(不是睡眠)。
buttons.get(0).click();
browser.wait(function() {
return element(by.css('[class="popover fade top in"]')).isPresent().then(function(present) {
return !present;
});
// or by checking any other element to know that the animation completed
}, 1000);
expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);
使用 Protractor 1.7,您将能够使用 expectedConditions 库,因此您可以执行以下操作:
var EC = protractor.ExpectedConditions
buttons.get(0).click();
var e = element(by.css('[class="popover fade top in"]'));
browser.wait(EC.not(EC.presenceOf(e)), 1000);
expect(e.isPresent()).toBe(false);