首页 > 解决方案 > 如何使用打字稿(量角器+黄瓜)执行 beforeTest 和 afterTest 方法

问题描述

使用的框架 - 量角器 BDD - 黄瓜语言 - 打字稿

我的配置文件:config.ts

export  let config: Config = {
        allScriptsTimeout: 40000,
        getPageTimeout: 40000,
        setDefaultTimeout: 60000,
        defaultTimeoutInterval: 30000,
        specs: [
            // '../../utils/beforeEach.ts',
            '../../features/*.feature',
        ],
        onPrepare: () => {
            Reporter.createDirectory(jsonReports);
            tsNode.register({
                project: './tsconfig.json'
            });
        },
        multiCapabilities: [
            androidPixel2XLCapability,
            // iPhoneXCapability
        ],
        framework: 'custom',
        frameworkPath: require.resolve('protractor-cucumber-framework'),
        cucumberOpts: {
            compiler: "ts:ts-node/register",
            glue: ["steps"],
            format: [
                "json:./reports/json/cucumber_report.json",
            ],
            require: ['supports/timeout.js', '../../stepdefinitions/*.ts'],
            tags: "@firstPurchasePopup",
        },
        seleniumAddress: serverAddress,

        onComplete: () => {
            Reporter.createHTMLReport();
        },


       // =====
       // Hooks
       // =====
       beforeTest: function () {

       },

       beforeLaunch(){
            console.log("Before");
            seleniumAddress: 'http://localhost:4723/wd/hub';
       },

       afterLaunch() {
            console.log("After");
       },
    };

我的另一个 beforeEach.ts:这不起作用,但我累了并且不起作用。

import {After, AfterAll, Before} from "cucumber";
const serverAddress = 'http://localhost:4723/wd/hub';
import {beforeEach, afterEach, describe} from "selenium-webdriver/testing";



    beforeEach(function () {
    console.log("Before");
    });
// });

afterEach(function () {
    console.log("Before");
});

// let beforeEach: () => void;
// beforeEach = () => {
//     console.log("Before Test");
//     // config.multiCapabilities;
//     seleniumAddress: serverAddress;
// };
//
// let afterEach: () => void;
// afterEach = () => {
//     console.log("After Test");
// };

这是我的功能文件:bonus.feature

this is my feature file:

Background:
    Given I launch the app
    Then I should see the popup window for the Bonus
    And I verify the UI
    Then I tap on ok button
    And The popup window should not be seen

  @firstPurchasePopup
  Scenario: firstPurchasePopup new join button
    When I tap on the 'New ' button
    And The popup window should not be seen
    Then I navigate back from join page to home page
    Then The popup window should not be seen
    Then I close the app

  @firstPurchasePopup
  Scenario: firstPurchasePopup login button
    And I tap on log in button on the initial screen
    Then I navigate back from login page to home page
    And The popup window should not be seen
    Then I close the app

我希望我所写的场景能够一个接一个地执行,例如执行场景:firstPurchasePopup new join button 。但是,当它再次启动该应用程序一秒钟Scenario: firstPurchasePopup login button时,由于驱动程序未再次启动,因此它无法正常工作,因为它已在上一个中关闭。要开始它,我需要创建 beforeTest 我很难编写代码

标签: protractorcucumberappium

解决方案


我没有在 Cucumber 中使用 Protractor,但我已经将 Cucumber 和 Typescript 一起使用。cucumber.js我通过在默认情况下在开始时加载的根目录中的文件解决了这个问题,看起来像这样:

var settings = "features/**/*.feature -r step-definitions/**/*.ts -r hooks/**/*.ts -r support/**/*.ts "

module.exports = {
  "default": settings
}

但是,我认为在您的情况下,解决方案是将挂钩文件的路径添加到config.cucumberOpts.require列表而不是config.specs一个。

你试过了吗?


推荐阅读