Protractor 如何使用typescript(量角器)执行测试前和测试后方法

Protractor 如何使用typescript(量角器)执行测试前和测试后方法,protractor,cucumber,appium,Protractor,Cucumber,Appium,使用的框架-量角器 黄瓜 语言打字稿 现在,我已经实现了这个框架,并且一个测试场景也可以使用Gradurator正常运行 但我面临的问题是,当我编写另一个cucumber场景时,我的测试失败了,说“会话要么终止,要么未启动” 上面的失败是因为当我的第一个cucumber场景启动时,appium服务器在我的配置中以开始,最后我关闭了服务器/驱动程序 现在我编写了另一个测试场景,因为cucumber独立于每个场景,当sec启动时,它不会再次进行配置。现在我需要一个beforeTest方法来调用

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

  • 现在,我已经实现了这个框架,并且一个测试场景也可以使用Gradurator正常运行
  • 但我面临的问题是,当我编写另一个cucumber场景时,我的测试失败了,说“会话要么终止,要么未启动”

    • 上面的失败是因为当我的第一个cucumber场景启动时,appium服务器在我的配置中以开始,最后我关闭了服务器/驱动程序
    • 现在我编写了另一个测试场景,因为cucumber独立于每个场景,当sec启动时,它不会再次进行配置。现在我需要一个beforeTest方法来调用
    • 所以我不知道如何在typescript中实现它,因为我对它还不熟悉

    • 尝试了java方式的相同概念,但没有成功。这里有javascript示例,但仍然没有帮助我

    • 尝试创建一个新的util文件夹并将my 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");
       },
    };
我的另一半在彼此之前。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按钮一样。但是,当它在sec
场景中再次启动应用程序时:firstPurchasePopup登录按钮不起作用,因为驱动程序没有再次启动,因为它在prev one中关闭。

要开始它,我需要创建beforeTest,我很难编写代码

我没有使用Cucumber的量角器,但是我一起使用了Cucumber和Typescript。我通过将文件
cucumber.js
放在根目录中解决了这个问题,默认情况下,根目录一开始就被加载,如下所示:

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

module.exports = {
  "default": settings
}
但是,我认为在您的情况下,解决方案是将hooks文件的路径添加到
config.cucumberOpts.require
list,而不是添加到
config.specs
one

你试过了吗?

@All 感谢您的投入@mhyphenated 我发现,我没有在配置中使用,而是尝试在hooks.ts中使用before和after,也没有调用服务器,实际上我没有调用android驱动程序,如下所示,这很有效 测试前:函数(){

钩子

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