AssertionError:预期未定义为等于'取消'

时间:2017-08-28 03:45:21

标签: angular typescript protractor cucumber chai

我正在自动化Angular 4应用程序。用量角器,打字稿,黄瓜和柴为断言。

获得以下错误:

AssertionError: expected undefined to equal 'Cancel'
     at getBasePromise.then.then.newArgs (D:\Vinni\ProtractorWorkspace\protractor-cucumber-final\node_modules\chai-as-promised\lib\chai-as-promised.js:302:22)
     at process._tickCallback (internal/process/next_tick.js:109:7)

无法在其他问题中找到解决方案similar to this

下面是我的代码详细信息: 节点版本:7.10.1 量角器版本5.1.2

config.ts

import {browser} from "protractor";
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

exports.config = {
 seleniumAddress: "http://localhost:4444/wd/hub",
 SELENIUM_PROMISE_MANAGER: false,
 baseUrl: "http://localhost:4200/",
 framework: "custom",
 frameworkPath: require.resolve("protractor-cucumber-framework"),
 specs: ["../features/*.feature"],
 exclude: "../features/database.feature",
 resultJsonOutputFile: "./reports/json/protractor_report.json",
 onPrepare: () => {
   browser.ignoreSynchronization = true;
   browser.manage().window().maximize();
   global.expect = chai.expect;
 },
 cucumberOpts: {
   strict: true,
   format: ["pretty"],
   require: ["../stepDefinitions/*.js", "../support/*.js"],
   tags: "@smoke"
 }
};

ManageRecipeStep.ts

import {defineSupportCode} from 'cucumber';
import {ManageRecipePage} from "../pages/ManageRecipePage";
import {ActionUtil} from "../utils/ActionUtil";
import {browser} from "protractor";
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
let expect = chai.expect;

defineSupportCode(({Given, When, Then}) => {
  let manageRecipePage = new ManageRecipePage();
  let actionUtil = new ActionUtil();

 Given(/^Navigate to the Recipes page$/, async () => {
    browser.ignoreSynchronization = true;
    await manageRecipePage.clickFirstRecipe();
 });

 When(/^click manage recipe$/, async () => {
    await manageRecipePage.manageRecipe();
 });

 When(/^click edit$/, async () => {
    await manageRecipePage.clickEdit();
 });

 Then(/^Cancel button should be displayed$/, async () => {
    await expect(manageRecipePage.getCancelButton()).to.eventually.equal('Cancel');
 }); // This assertion should pass but getting error here

 Then(/^Cancel button should be enabled/, async () => {
    await expect(manageRecipePage.isCancelEnabled()).to.eventually.be.true;
 });

});

ManageRecipePage.ts

import {BasePage, IdentificationType} from "../utils/BasePage";
import {ActionUtil} from "../utils/ActionUtil";

const Locators = {
    firstRecipe: {
        type:IdentificationType[IdentificationType.Xpath],
        value: "//A[@ng-reflect-router-link='0']"
    },
    manageRecipeByText: {
        type:IdentificationType[IdentificationType.PartialButtonText],
        value: "Manage Recipe"
    },
    edit: {
        type: IdentificationType[IdentificationType.PartialButtonText],
        value: "edit"
    },
    cancelByText: {
        type:IdentificationType[IdentificationType.PartialButtonText],
        value: "Cancel"
    }
};

let actionUtil = new ActionUtil();

export class ManageRecipePage extends BasePage {

    async clickFirstRecipe() {
        await actionUtil.clickElement(Locators.firstRecipe);
    }

    async manageRecipe() {
        await actionUtil.clickElement(Locators.manageRecipeByText);
    }

    async clickEdit() {
        await actionUtil.clickElement(Locators.edit);
    }

    async getCancelButton() {
        await actionUtil.getElementText(Locators.cancelByText);
    }

    async isCancelEnabled() {
        await actionUtil.isElementEnabled(Locators.cancelByText);
    }
}

ActionUtils.ts

async getElementText(obj) {
        let attempts = 0;

        while(attempts < 2) {
            try {
                await this.basePage.ElementLocator(obj).getText();
            } catch(StaleElementException) {
                console.log("EXCEPTION while getting Text" + StaleElementException);
            }
            attempts++;
        }
    }

的package.json

"dependencies": {
    "chai": "^4.0.2",
    "cucumber": "^2.3.0",
    "mkdirp": "^0.5.1",
    "protractor": "^5.1.1",
    "protractor-cucumber-framework": "^3.1.0",
    "ts-node": "^3.1.0",
    "typescript": "^2.2.1"
  },
  "devDependencies": {
    "@types/cucumber": "^2.0.3",
    "@types/node": "^8.0.25",
    "@types/selenium-webdriver": "^3.0.0",
    "chai": "^4.0.2",
    "chai-as-promised": "^7.0.0",
    "cucumber": "^2.3.0",
    "cucumber-html-report": "^0.6.0",
    "cucumber-html-reporter": "^0.5.2",
    "cucumberjs-allure-reporter": "^1.0.3",
    "mkdirp": "^0.5.1",
    "pg": "^6.0.3"
  }

提前致谢。 @Ram Pasala你有什么建议!

1 个答案:

答案 0 :(得分:0)

您的测试方法会在解析承诺之前评估结果。 您需要在测试方法中return the promise