量角器 7

问题描述

我正在尝试使用量角器和 jasmine 自动执行一些测试,我正在使用 async/await 来解决承诺。

问题是,当发生错误时,堆栈跟踪太短,因此,我似乎无法找到问题的根源。

我确实确保在配置文件中将 SEELENIUM_PROMISE_MANAGER 设置为 FALSE。

我正在使用量角器 7 和节点 14.16.0

有谁知道如何解决这个问题?没有足够的细节

这是一个代码片段

const invoicesButton: Button = new Button("Invoices",LocatorType.Href,"#/Invoices");


describe("Kouka test",function () {
    beforeEach(function () {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000000000;
    });

    it("Does a random test",async function () {
        await browser.get("https://appdev.expensya.com/Portal/#/Login?lang=en");
        await loginEmailInput.typeText("amr.refacto@yopmail.com")
        await loginPasswordInput.typeText("a")
        await loginButton.click(true);
        await dashboardPage.invoicesButton.click().catch((e) => {
            e.stackTraceLimit = Infinity;
            throw e;
        });
        await userInvoicesPage.createManualInvoice(invoice).catch((e) => {
            e.stackTraceLimit = Infinity;
            console.error("TEST ERROR ",e);
            throw e;
        });
        await browser.sleep(10000);

    });

});

这是“按钮”类的定义:

import { browser } from "protractor";
import { WebComponent } from "./webComponent";


export class Button extends WebComponent {

/**
 * @param webElementText Text that the web element contains.
 * @param locatorType Locator type of the web element to search for.
 * @param locator Locator of the web element to search for.
 * @param parentElement Parent Web Component if it exists.
 */
constructor(webElementText,locatorType,locator,parentElement: WebComponent = null) {
    super(webElementText,parentElement);
}


async click(usingJavaScript = false) {
    if (usingJavaScript) {
        await this.isPresent();
        await browser.executeScript("arguments[0].click();",await this.webElement)
    }
    else {
        await this.isVisible();
        await this.isClickable();
        await this.webElement.click();
    }
}

}

最后,这是堆栈跟踪

Started
Jasmine started
undefined
F
  Kouka test
    × Does a random test
      - Failed: Wait timed out after 10012ms
          at C:\Users\Amrou Bellalouna\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2201:17
          at runMicrotasks (<anonymous>)
          at processticksAndRejections (internal/process/task_queues.js:93:5)
      From asynchronous test:
      Error
          at Suite.<anonymous> (C:\Users\Amrou Bellalouna\source\repos\NewE2EArchitecture\NewArchitecture\koukouTest.spec.ts:20:5)
          at Object.<anonymous> (C:\Users\Amrou Bellalouna\source\repos\NewE2EArchitecture\NewArchitecture\koukouTest.spec.ts:15:1)
          at Module._compile (internal/modules/cjs/loader.js:1063:30)
          at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
          at Module.load (internal/modules/cjs/loader.js:928:32)
          at Function.Module._load (internal/modules/cjs/loader.js:769:14)



Failures:
1) Kouka test Does a random test
  Message:
    Failed: Wait timed out after 10012ms
  Stack:
    TimeoutError: Wait timed out after 10012ms
        at C:\Users\Amrou Bellalouna\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2201:17
        at runMicrotasks (<anonymous>)
        at processticksAndRejections (internal/process/task_queues.js:93:5)
    From asynchronous test:
    Error
        at Suite.<anonymous> (C:\Users\Amrou Bellalouna\source\repos\NewE2EArchitecture\NewArchitecture\koukouTest.spec.ts:20:5)
        at Object.<anonymous> (C:\Users\Amrou Bellalouna\source\repos\NewE2EArchitecture\NewArchitecture\koukouTest.spec.ts:15:1)
        at Module._compile (internal/modules/cjs/loader.js:1063:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
        at Module.load (internal/modules/cjs/loader.js:928:32)
        at Function.Module._load (internal/modules/cjs/loader.js:769:14)

1 spec,1 failure
Finished in 19.461 seconds

解决方法

我记得前一段时间试图解决这个问题,但我不能。但我实施了一堆解决方法,显然这已经足够

首先,你能分享一下他们在做什么吗

await this.isPresent();
await this.isVisible();
await this.isClickable();

  1. 有这个功能
async isVisible(){
  await browser.wait(
    ExpectedConditions.visibilityOf(this.webElement),this._elapsedTime
  )
}

您可以使用 browser.wait 的第三个参数的优势,如 here 所述,并在失败时包含一个可选的消息

async isVisible(){
  await browser.wait(
    ExpectedConditions.visibilityOf(this.webElement),this._elapsedTime,`Element ${this.webElement.locator().toString()} is not present after ${this._elapsedTime}ms`);
  )
}
  1. (我把我所有的秘制酱料都给了你哈哈)如果你把它添加到配置中的 onPrepare
    /**
     *  Set global environment configuration
     */
    Object.defineProperty(global,'__stack',{
        get: function() {
            let orig = Error.prepareStackTrace;
            Error.prepareStackTrace = function(_,stack) {
                return stack;
            };
            let err = new Error();
            Error.captureStackTrace(err,arguments.callee);
            let stack = err.stack;
            Error.prepareStackTrace = orig;
            return stack;
        },});

    // returns name of the file from which is called
    Object.defineProperty(global,'__file',{
        get: function() {
            let path = __stack[1].getFileName();
            try {
                //*nix OSes
                return path.match(/[^\/]+\/[^\/]+$/)[0];
            } catch (error) {
                //Windows based OSes
                return path.match(/[^\\]+\\[^\\]+$/)[0];
            }
        },});
    // returns function name from which is called
    Object.defineProperty(global,'__function',{
        get: function() {
            return __stack[1].getFunctionName();
        },});
    // returns line number of the position in code when called
    Object.defineProperty(global,'__line',{
        get: function() {
            return __stack[1].getLineNumber();
        },});

然后你可以用它来记录文件名、函数名和调用它的行

例如

async isVisible(){
  await browser.wait(
    ExpectedConditions.visibilityOf(this.webElement),`Failed at ${__file} -> ${__function}() -> line ${__line}`
  )
}

会导致这个错误

 - Failed: Failed at common/index.js -> isVisible() -> line 82
      Wait timed out after 1032ms
      Wait timed out after 1032ms

因此您可以根据自己的需要进行调整

我也刚刚意识到您可能想使用 __stack 变量本身