在http调用完成后创建元素时,使用Protractor for Angular测试元素是否存在

时间:2018-04-12 22:06:52

标签: angular protractor

我的量角器测试没有等待http调用完成,并且找不到元素并且将无法通过测试。在查找元素之前,如何使量角器测试等待我的http调用完成?

模板:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Condition
from selenium.common.exceptions import *

# Start your driver
driver = ...
# Then wherever you want to wait for an alert
timeout = 10    # Max time to wait in seconds
try:
    # This waits only as long as needed to detect the alert, then continues.
    WebDriverWait(driver, timeout).until(Condition.alert_is_present())
    print("Alert was found, now what?")
except TimeoutException:
    print("Timeout waiting for alert")

的javascript:

<div *ngIf="done" class="myelement">myelement</div>

量角器:

    this.http.get(url).subscribe(()=> {
        this.done = true;
    })

我收到错误&#34;期望false为真&#34;。

如果我删除expect(element(by.className('myelement')).isPresent()).eventually.to.be.true; ,那么我的测试将通过,因此http调用肯定是问题

1 个答案:

答案 0 :(得分:0)

量角器不提供api等待http调用完成,只能从页面等待一些元素。

在这种情况下,您需要按browser.wait(condition, timeout)

等待元素本身
var EC = protractor.ExpectedConditions;

browser

.wait(EC.presenceOf(by.css('div.myelement')), 15*1000)

.catch(function(err) {
   // if not find the element after 15 seconds (15 * 1000)
   // it will throw timeoutexception, means the element not present
   // so we need to catch and handle it.

   // if not throw timeoutexception, means the element present
   // no need to give one more code line to check `isPresent() === true`
   expect(false).to.be.true;
});