如何使用打字稿(量角器+黄瓜)执行beforeTest和afterTest方法

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

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

  • 现在我已经实现了框架,并且使用量角器也可以很好地运行测试场景。
  • 但是我面临的问题是当我编写另一个黄瓜方案时,我的测试失败,提示'会话已终止或未开始'

    • 以上失败是因为当我的第一个黄瓜方案启动时,appium服务器以我的配置启动,最后我关闭了服务器/驱动程序
    • 现在,我已经编写了另一个测试方案,因为Cucumber独立于每个方案,所以当秒启动时,它将不再进行配置。现在,我需要一个beforeTest方法来调用。
    • 所以我不确定如何在打字稿中实现该功能,因为我是新手。

    • 尝试过与Java方式相同的概念,但无法解决。那里有一些关于javascript的示例,但仍然没有帮助我。

    • 尝试创建一个新的util文件夹,并将我的beforeTest放在其中,但该函数未在其中调用
    • 试图在我的配置文件中使用beforeLaunch(),但仍然无法解决

我的配置文件: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

我希望我编写的脚本能够依次执行这两个脚本,就像execute Scenario:firstPurchasePopup new join button一样。但是当它再次启动该应用程序几秒钟时Scenario: firstPurchasePopup login button不起作用,因为该驱动程序没有再次启动,因为它在上一个版本中已关闭。要启动它,我需要创建beforeTest,我很难编写代码]

protractor cucumber appium
2个回答
0
投票

我没有将Protractor与Cucumber一起使用,但是我一起使用了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一个。

您尝试过吗?


0
投票

@全部感谢您的输入@mhyphenated我发现不是在配置内部使用,而是尝试在hooks.ts中使用before和after,除了调用服务器之外,我实际上没有真正调用android驱动程序,如下所示beforeTest:function(){

beforeTest: function () {

       },

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

hooks.ts

import { AndroidDriver } from "appium/node_modules/appium-android-driver";

let driver:AndroidDriver, defaultCaps;
driver =  new AndroidDriver();
Before(function () {
    // This hook will be executed before all scenarios
    browser.ignoreSynchronization = false;
    browser.manage().timeouts().implicitlyWait(500);
    let defaultCaps = config.multiCapabilities[0];
    console.log("defaultCaps = ", defaultCaps );
    driver.createSession(defaultCaps);
    driver.defaultWebviewName(); 
  });
© www.soinside.com 2019 - 2024. All rights reserved.