【问题标题】:How to set error message using protractor如何使用量角器设置错误消息
【发布时间】:2020-07-26 02:58:52
【问题描述】:

所以我已经使用量角器工作了很长时间,我发现我遇到了错误消息等问题。如果我在 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);
    }

};

实用程序:

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();

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

【问题讨论】:

  • 您的代码中的utils 是什么?来自互联网的包,还是你写的?
  • @SergeyPleshakov 糟糕!马上添加
  • @SergeyPleshakov 刚刚添加 :)
  • 太棒了,那就简单了。很快就会收到回复

标签: javascript selenium protractor


【解决方案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

【讨论】:

  • 你是我的老师!你帮了我很多,可能也只是一个开始。我真的很感激它,这也正是我想要的!真的很喜欢它,谢尔盖! :) - 我有一个问题,如果我想在这种情况下使用默认超时,我应该不输入任何 10000 的值还是仍然需要添加超时?
  • 是的,将超时指定为jasmine.DEFAULT_TIMEOUT_INTERVALundefined。如果对您有帮助,请采纳并点赞
  • 只是好奇,我怎么能做到jasmine.DEFAULT_TIMEOUT_INTERVAL?当然,如果您不需要太长时间,则作为超时:)
  • 如果我正确理解了您的问题,并且如果我没记错的话,jasmine.DEFAULT_TIMEOUT_INTERVAL 在配置中指定为defaultTimeoutInterval。如果不是,则设置为 30 秒。然后它可以在项目中的任何地方作为全局变量使用(不需要它)。我更新了答案以指示如何设置默认超时
  • 正是我想要的!我认为在这种情况下我们还需要将await utils.presenceOf(frameCartIcon, 10000, "frame should be populated"); 更改为await utils.presenceOf(frameCartIcon, "frame should be populated");
猜你喜欢
  • 1970-01-01
  • 2019-02-20
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多