如何使用量角器设置错误消息

问题描述 投票:0回答:1

因此,我与量角器一起工作了相当长的一段时间,我发现我遇到了错误消息等问题。如果在60秒钟内找不到元素,那么超时将引发错误。这不是真正了解问题实质的好方法,我在这里问你们我如何能够发出自己的错误消息,例如找不到该特定元素或类似的东西。

我已经编写了类似的代码。

测试用例类:

const userData = require("../globalContent.json");
const Page = require("../objects/ikeaProductPage.obj");


describe("Product page", function () {

    ikeaPage = new Page();

    for (let [link, amount] of Object.entries(userData[browser.baseUrl])) {
        // The Ikea page is accessible by the specified URL
        it(`Is defined by the URL: ${link}`,
            async function() {
                await Page.navigateDesktop(`${link}`);
            });

        // page has a quantity label and it can be filled out with user data
        it("Has a label quantity that can receive user data",
            async function() {
                await Page.fillFormWithUserData(`${amount}`);
            });

        // Details page allows the user to add to cart
        it("Enables resolution of added to cart",
            async function() {
                await Page.hasAddToShoppingCart();
            });

        // Details page allows the user to proceed to the next stage when page has been resolved
        it("Allows the user to proceed to the next stage of add to cart",
            async function() {
                await Page.hasAddedToBag();
                await browser.sleep(1000);
            });
    }
});

对象类:

const utils = require("../utils/utils");
const Specs = require("../specs/ProductPage.specs");

module.exports = class Page {

    constructor() {
        const _fields = {
            amountInput: Specs.productAmount
        };

        const _formButtons = {
            addToCart: ikeaSpecs.addToCart
        };

        const _productFrame = {
            cartIcon: ikeaSpecs.cartIcon,
            addedToCartIcon: Specs.addedToCart,
        };

        this.getFields = function() {
            return _fields;
        };
        this.getFormButtons = function() {
            return _formButtons;
        };
        this.getFrame = function() {
            return _productFrame;
        };
    }

    getForm() {

        return {
            fields: this.getFields(),
            buttons: this.getFormButtons(),
        };
    }

    getPageFrame() {
        return {
            buttons: {
                iconFrames: this.getFrame()
            }
        };
    }


    //Navigate for Desktop
    async navigateDesktop(URL) {
        await browser.waitForAngularEnabled(false);
        await browser.manage().window().maximize();
        await browser.get(URL);
    }

    //Fill qty from globalContent.json
    async fillFormWithUserData(amountQuantity) {
        const formFields = this.getForm().fields.amountInput;
        await formFields.clear();
        await utils.sendKeys(formFields, amountQuantity);
    }

    //Check if we can add to shopping cart
    async hasAddToShoppingCart() {
        const formButton = this.getForm().buttons.addToCart;
        await utils.elementToBeClickable(formButton);
        await utils.click(formButton);
    }

    //Check if the product has been added
    async hasAddedToBag() {
        const frameCartIcon = this.getPageFrame().buttons.iconFrames.cartIcon;
        const frameAddedToCart = this.getPageFrame().buttons.iconFrames.addedToCartIcon;
        await utils.presenceOf(frameCartIcon);
        await utils.elementToBeClickable(frameAddedToCart);
    }

};

utils:

const utils = function () {
    var EC = protractor.ExpectedConditions;

    this.presenceOf = function (params) {
        return browser.wait(EC.presenceOf(params));
    };

    this.elementToBeClickable = function (params) {
        return browser.wait(EC.elementToBeClickable(params));
    };

    this.sendKeys = function (params, userData) {
        return params.sendKeys(userData);
    };

    this.click = function (params) {
        return browser.executeScript("arguments[0].click();", params.getWebElement());
    };

    this.switch = function (params) {
        return browser.switchTo().frame(params.getWebElement());
    };

    this.switchDefault = function () {
        return browser.switchTo().defaultContent();
    };
};

module.exports = new utils();

而且我想知道如何能设置更多正确的错误,而不仅仅是超时?

javascript selenium protractor
1个回答
1
投票

由于您在引擎盖下使用browser.wait,因此您要考虑使用其中之一parameters。如页面所示,它需要3个参数,并且所有参数都很有用:

browser.wait(
  () => true, // this is your condition, to wait for (until the function returns true)
  timeout, // default value is jasmineNodeOpts.defaultTimeoutInterval, but can be any timeout
  optionalMessage // this is what you're looking for
)

更新

因此,如果我全部使用这三个,它将看起来像这样

this.presenceOf = function (params, message) {
  return browser.wait(
    EC.presenceOf(params),
    jasmine.DEFAULT_TIMEOUT_INTERVAL,
    `Element ${params.locator().toString()} is not present. Message: ${message}`
  )
};

当您这样称呼它时

await utils.presenceOf(frameCartIcon, 10000, "frame should be populated");

并且失败,您将获得此堆栈

      - Failed: Element By(css selector, "some css") is not present. Message: frame should be populated
      Wait timed out after 1002ms
      Wait timed out after 1002ms
          at /Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2201:17
          at ManagedPromise.invokeCallback_ (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:1376:14)
          at TaskQueue.execute_ (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:3084:14)
          at TaskQueue.executeNext_ (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:3067:27)
          at asyncRun (/Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:2927:27)
          at /Users/spleshakov/Documents/ui-automation/node_modules/selenium-webdriver/lib/promise.js:668:7
          at processTicksAndRejections (internal/process/next_tick.js:81:5)
      From: Task: Element By(css selector, "some css") is not present. Message: frame should be populated
© www.soinside.com 2019 - 2024. All rights reserved.