简体   繁体   English

如何使用量角器等待网站完全加载

[英]How to wait for a website to load completely using protractor

I am working on creating a test scripts for a non angular js website using protractor. 我正在使用量角器为非angular js网站创建测试脚本。 My code is as follows : 我的代码如下:

var co = require('co');
var path = require('path');

describe("TEST", function () {

    it("test", co.wrap(function* () {
        browser.ignoreSynchronization = true;

        yield browser.get(URL, 60000);
        browser.sleep(5000);// I want to remove the sleep statements
        var elmOK = browser.driver.findElement(by.css('a[href="#login"]'));
        yield elmOK.click();
        expect(browser.getCurrentUrl()).toContain("login");
        yield browser.switchTo().frame('here-account-sdk').then(function () {
            browser.driver.findElement(by.id('sign-in-email')).sendKeys("uid");
            browser.driver.findElement(by.id('sign-in-password-encrypted')).sendKeys("password");
            browser.driver.findElement(by.xpath(' //*[@id="sign-in-form"]/div[2]/div[6]/form/fieldset[3]/button')).click();

        });
        browser.sleep(5000);// I want to remove the sleep statements
        var elmOK = browser.driver.findElement(by.xpath('//*[@id="lnav"]/li[3]/a'));
        yield elmOK.click();
        browser.sleep(1500);// I want to remove the sleep statements
        browser.driver.findElement(by.xpath('//*[@id="administration"]/div/div[1]/select/option[2]')).click();
        browser.sleep(5000);// I want to remove the sleep statements

        browser.driver.findElement(by.xpath('//*[@id="administration"]/div/div[2]/table/tbody/tr[1]/td[10]/span')).click();
        browser.sleep(5000);// I want to remove the sleep statements

        browser.driver.findElement(by.xpath('//*[@id="content"]/div/div[2]/div/div/div/div[3]/button[1]')).click();//Delete the file
        browser.sleep(5000);// I want to remove the sleep statements
    }));




});

If I add sleep in my code the test runs as expected. 如果我在代码中添加睡眠,则测试将按预期运行。 But I want to remove sleep statements from my code completely. 但是我想从代码中完全删除sleep语句。 I referred to many other stack overflow questions but it didn't help. 我提到了许多其他堆栈溢出问题,但没有帮助。 :( :(

I first used browser.manage().timeouts().implicitlyWait(5000); 我首先使用browser.manage().timeouts().implicitlyWait(5000); but I think there was no delay in loading my url. 但我认为加载网址没有延迟。

I used expected condition but it was also not working. 我使用了预期的条件,但也无法正常工作。

I added browser.wait(1000) but my script fails. 我添加了browser.wait(1000)但脚本失败。 Please advice me as I feel bit lost in this problem. 请给我建议,因为我对这个问题有些困惑。

I think the problem is with this line: 我认为问题在于此行:

browser.ignoreSynchronization = true;

From this SO it 这样

makes protractor not wait for Angular promises, such as those from $http or $timeout to resolve 使量角器不必等待Angular承诺,例如来自$ http或$ timeout的承诺

Protractor element functions all return promises so this might be the cause of your problem? 量角器元素函数全部返回promise,所以这可能是造成问题的原因吗?

Also this structure looks a bit weird: 而且这个结构看起来有点怪:

var elmOK = browser.driver.findElement(by.css('a[href="#login"]'));
yield elmOK.click();

I would simplify with just: 我将简化为:

element(by.css('a[href="#login"]')).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM