量角器 - 打开浏览器,等待,运行测试

时间:2016-08-22 12:41:29

标签: protractor

我正在学习量角器,看起来很棒。但我想在单页开放时运行几个规格。我怎样才能做到这一点?我的问题是,我页面上的很多东西是通过ajax加载的,有些是通过级联加载的。

目前,我使用waitbeforeEach运行规范。这是代码:

const br = browser;
const url = 'http://localhost:3000';

br.ignoreSynchronization = true;


describe('Component', () => {
    beforeEach(() => { br.get(url); });

    it ('should be present', () => {
        expect(element(by.className('news-list__wrapper')).isPresent()).toBe(true);
    });

    it ('should render children', () => {
        br.wait(() => {
            return element(by.className('news-block')).isPresent();
        }, 2500).then((r) => { if (r) { console.log('\n ✓ Component renders children') } });
    });

    it ('should render images', () => {
        br.wait(() => {
            return element.all(by.className('news-block__img')).first().isPresent();
        }, 2500).then((r) => { if (r) { console.log('\n ✓ Component renders images') } });
    });

    it ('should load images', () => {
        br.wait(() => {
            return element(by.className('news-block__image--loaded')).isPresent();
        }, 2500).then((r) => { if (r) { console.log('\n ✓ Component loads images') } });
    })
})

我该怎么办?

2 个答案:

答案 0 :(得分:1)

ExpectedConditions中使用browser.wait()进行此类ajax调用 - 有关详细信息,请参阅API文档 - http://www.protractortest.org/#/api?view=ProtractorExpectedConditions

 const br = browser;
 const url = 'http://localhost:3000';
 const EC = protractor.ExpectedConditions;

 br.ignoreSynchronization = true;


describe('Component', () => {
beforeEach(() => { br.get(url); });

it ('should be present', () => {
    expect(element(by.className('news-list__wrapper')).isPresent()).toBe(true);
});

it ('should render children', () => {
    br.wait(EC.presenceOf(element(by.className('news-block'))), 2500).then((r) => { if (r) { console.log('\n ✓ Component renders children') } });
});

it ('should render images', () => {
    br.wait(EC.presenceOf(element.all(by.className('news-block__img')).first()), 2500).then((r) => { if (r) { console.log('\n ✓ Component renders images') } });
});

it ('should load images', () => {
    br.wait(EC.presenceOf(element(by.className('news-block__image--loaded'))), 2500).then((r) => { if (r) { console.log('\n ✓ Component loads images') } });
})
})

答案 1 :(得分:1)

设置browser.ignoreSynchronization = false并使用browser.waitForAngular()让您的脚本等待所有ajax调用完成。量角器的强大之处在于它将等待页面所做的所有异步调用完成,然后才会执行下一行代码。