当使用Protractor进行非Angular页面时,如何等待页面加载或元素存在

时间:2014-08-11 17:38:07

标签: javascript protractor

我是Protractor的新手。我认为在处理Angular页面时我已经失去了这个功能,但是无法解决非Angular页面问题。任何帮助,将不胜感激。

describe('Search', function() {
   it('should click Search button and wait for results', function() {
      browser.driver.findElement(by.id('search')).click();
   });
});

4 个答案:

答案 0 :(得分:15)

使用Protractor测试非角度页面在等待内容时可能会非常棘手。

我建议您将Protractor升级到latest(目前为1.5.0),使用custom function waitReady() browser.wait准备好元素并重写您的测试,如下所示。请注意,如果您愿意,可以将所有内容都放在1个规格内。

// TODO: use page objects
var searchBtnElm = $('#search'); // use element(by.id('search')) if you prefer

it('waits for the elements present and visible (non-angular)', function() {
    expect(searchBtnElm.waitReady()).toBeTruthy();
});

it('should click Search button', function() {
    searchBtnElm.click();
});

it('wait for more results', function() {
    // keep using waitReady() before interacting with the elements
    // and before performing expectations on them
});

waitReady here

的详细信息

注意:请记住设置忽略同步以测试非角度页面:

    browser.ignoreSynchronization = true;

您可以在browser.get非角度页面之前设置它。

我建议过去设置一个高隐式等待,例如

browser.manage().timeouts().implicitlyWait(5000);

hack 允许您避免waitReady并继续使用标准

expect(searchBtnElm.isPresent()).toBeTruthy();

但是在测试元素 NOT 存在时有一个丑陋的缺点,即当测试不存在或不可见的元素时,在这种情况下它将在叶片中等待5秒(5000ms),例如做什么时

expect(someNonExistingElm.isPresent()).toBeFalsy();

答案 1 :(得分:3)

想出来了。我只是在点击方法之后添加了下面的代码:

describe('Search', function() {
   it('should click Search button and wait for results', function() {
        browser.driver.findElement(by.id('search')).click();
        dvr.wait(function() {
            return dvr.isElementPresent(by.xpath(
                '/html/body/div/div[4]/div/div[2]/div/div/div/span'));
        }, 20000);
    });
});

答案 2 :(得分:1)

另一个简洁的方法是在browser.wait中使用“预期条件” - 类似这样的内容:

var EC = protractor.ExpectedConditions;
var search = element(by.id('search'))
browser.wait(EC.visibilityOf(search), 2000).then(function(){
 search.click()
})

您可以在此处获取更多详细信息:https://angular.github.io/protractor/#/api?view=ExpectedConditions

答案 3 :(得分:1)

在量角器中,页面上有两种类型的术语。 if (guess > randomNumber && guess <= 100){ System.out.println("Your guess, "+guess+", is greater than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now the value of i is 0. The value of i in the next iteration will be 1 } else if (guess < randomNumber && guess > 0){ System.out.println("Your guess, "+guess+", is less than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now the value of i is 0. The value of i in the next iteration will be 1 } 询问页面上是否存在该元素。 isPresent询问元素是否可见。如果您正在等待加载页面,则需要等待isDisplayed,但如果不存在则会出错,因此请先等待isDisplayed。我使用一个函数来等待一个元素。

isPresent

然后在测试中调用该函数。

function waitForElement(el, waitTimeoutMilliseconds){
    return browser.wait(function() { return el.isPresent(); }, waitTimeoutMilliseconds)
        .then(function(){
           return browser.wait(function() { return el.isDisplayed(); }, waitTimeoutMilliseconds);
        });
}