本文介绍了想要删除 Browser.sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用量角器来测试非角度网站.最初在测试时我有 browser.sleep() 以便页面完全加载.我知道添加 browser.sleep 不是一种好的测试方式,因此想从代码中删除它.

Hi all I am working on a protractor to test a nonangular website. Initially while testing I had browser.sleep() so that page gets load completely. I know that adding browser.sleep is not a good way of testing and hence want to remove it from the code.

我尝试了 browser.wait,但出现错误,当我添加 broswer.manage.timeouts.implicitwait() 时,等待不会发生.我被这个问题困扰了很长时间,请帮帮我:(

I tried browser.wait but I get an error and when I add broswer.manage.timeouts.implicitwait() the wait doesn't happen. I'm stuck on this issue for a long time please help me out :(

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

    describe("Portal: Partner Admin ", function () {
        beforeEach(function () {
            browser.ignoreSynchronization = true;

        });

        it("test", co.wrap(function* () {

            yield browser.get(browser.params.baseUrl);

            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("Userid");
                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);
            var elmOK = browser.driver.findElement(by.xpath('//*[@id="lnav"]/li[3]/a'));
            yield elmOK.click();
            browser.sleep(1500);
            browser.driver.findElement(by.xpath('//*[@id="administration"]/div/div[1]/select/option[2]')).click();
            browser.sleep(5000);

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

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

推荐答案

与其等待页面本身,不如等待页面上的元素.

Rather than waiting for the page itself, wait for an element on the page.

诀窍是先等待元素出现,然后等待它被显示.如果您不先等待isPresent",则仅调用isDisplayed"会导致错误.这是一个很好用的功能.

The trick is to wait first for the element to be present, then wait for it to be displayed. Just calling "isDisplayed" causes errors if you do not wait for "isPresent" first. Here is a good function to use.

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

代替

browser.sleep(5000);
var elmOK = browser.driver.findElement(by.xpath('//*[@id="lnav"]/li[3]/a'));
yield elmOK.click();

var elmOk = element(by.xpath('//*[@id="lnav"]/li[3]/a'));
waitForElement(elmOk, 5000);
elmOk.click();

这篇关于想要删除 Browser.sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:52