【发布时间】: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