javascript - 在指定的超时时间内未调用 jasmine 异步回调

标签 javascript node.js angularjs jasmine protractor

我是 Protractor 和 javascript 的新手。我知道有很多关于此错误的问题和解决方案。但是没有一个解决方案让我清楚地了解异步回调,这个错误对我来说仍然存在。

我正在使用页面对象模型。请引用以下我自己构造的代码:

elements.js:

var pageElements = function(){};

pageElements.prototype = Object.create({},{
  usernameField:{get: function(){return element(by.id('username'));}},
  passwordField:{get: function(){return element(by.id('password'));}},
  signinButton:{get: function(){return element(by.xpath("xpath"));}},
  formField1:{get: function(){return element(by.xpath("xpath1"));}},
  formField2:{get: function(){return element(by.xpath("xpath2"));}},
});

module.exports = pageElements;

controls.js:

var EC = protractor.ExpectedConditions;
var controls = require('../Controls/controls.js');

exports.waitForElementById = async function(id){
  debugger;
  var isVisible = EC.visibilityOf(id);
  await browser.wait(isVisible,50000);
  browser.sleep(3000);
};

exports.waitForElementByXpath = async function(xpaths){
  var isVisible = EC.visibilityOf(xpaths);
  await browser.wait(isVisible,50000);
  browser.sleep(3000);
};

exports.sendKeysById = async function(ids, value){
  controls.waitForElementById(ids);
  var isVisible;
  for(i = 1; i<5; i++){
    isVisible = EC.visibilityOf(ids);
    if(isVisible){
        return await ids.sendKeys(value).then(function(){
            browser.sleep(500);
        }) .catch(error => {
            console.log('---------Error: Element not sent by id-----------');

            throw error;
        });
    }
  }
}

exports.clickElementById = async function(id){
  controls.waitForElementById(id);
  var isClickable;
  var processingBar;
  var check;
  for(i = 1; i<5; i++){
    isClickable = EC.elementToBeClickable(id);
    processingBar = EC.invisibilityOf(element(by.xpath("//*[contains(@id,'exterroLoader')]")));
    check = EC.and(isClickable,processingBar);
    if(check){
        return await id.click().then(function(){
            browser.sleep(500);
        }) .catch(error => {
            console.log('---------Error: Element not clicked by id-----------');
            throw error;
        });
    }
  }
};

formOperations.js

browser.ignoreSynchronization = true;
var controls = require('./controls.js');
var pageElements = require('./elements.js');
var e;

exports.form1 = async function(){
    e = new pageElements();
    browser.get("https://form.htm");
    await controls.sendKeysById(e.usernameField);
    await controls.sendKeysById(e.passwordField);
    await controls.clickByXpath(e.signinButton);
    await controls.sendKeysById(e.formField1);
};

exports.form2 = async function(){
    e = new pageElements();
    browser.get("https://form.htm");
    await controls.sendKeysById(e.usernameField);
    await controls.sendKeysById(e.passwordField);
    await controls.clickByXpath(e.signinButton);
    await controls.sendKeysById(e.formField2);
};

TestCases.js

var ignore = function(exp){return{it:((typeof exp==='function')?exp():exp)?function(){}:it}};

describe('Automation', function() {
var controls = require('./controls.js');
browser.ignoreSynchronization = true;
browser.get('https://prre2eauto.exterrocloud.info');

it('Form 1', async function(){
    var a = require('./formOperations.js');
    await a.form1();
});

it('Form 2', async function(){
    var a = require('./formOperations.js');
    await a.form2();
});

});

上面给出了示例代码,但我使用的模型完全相同。

我正面临的是

  1. form1 成功执行,没有任何错误。
  2. form2 执行成功但出现以下错误:

    Jasmine spec timed out. Resetting the WebDriver Control Flow.
    Message:
    [31m    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.[0m
    Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
    

请帮助解决这个问题,因为我需要更长的时间才能理解。

最佳答案

试试下面的

var controls = require('./controls.js');
describe('Automation', function() {

beforeEach(async() =>{ //This block will be executed before every `it` block
await browser.waitForAngualrEnabled(true);
await browser.get('https://prre2eauto.exterrocloud.info');
});

it('Form 1', async function(){
    var a = require('./formOperations.js');
    await a.form1();
});

it('Form 2', async function(){
    var b = require('./formOperations.js');
    await b.form2();
});

});

希望对你有帮助

关于javascript - 在指定的超时时间内未调用 jasmine 异步回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489974/

相关文章:

javascript - 使用 ngStorage 进行 Protractor 测试

javascript - 在不使用 Id 的情况下通过 javascript 应用样式

javascript - 如何使这个 Javascript 不仅仅适用于页面上第一个元素?

node.js - 让 express.js 从异步回调发送响应

node.js - coinbase- Node : Scopes don't match current API version

javascript - CSS/Angular : Accordion as footer - styling issues

javascript - 如何获取svg map 起点的纬度和经度坐标(x=0,y=0)?

javascript - 在 laravel 中发送多个属性?

c++ - 使用node-ffi时如何防止Node崩溃

javascript - forEach 函数比等效的 for 循环快得多