2

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

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

我确实确保在配置文件中将 SELENIUM_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, locatorType, locator, 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
4

1 回答 1

1

我记得不久前试图解决这个问题,但我做不到。但我实施了一堆变通办法,显然这已经足够了

首先,你能分享一下这些在做什么吗

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

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

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

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),
    this._elapsedTime,
    `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变量本身

于 2021-03-24T13:14:54.793 回答