以可读的形式从黄瓜测试中捕获 shell 输出

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

我正在尝试编写一个 cucuber 测试来运行 shell 命令,但我似乎无法正确显示输出...

我正在尝试使用 appium 获取 apk 文件,并且我想确保我可以在测试开始之前启动后端服务......当我运行 exec 命令来启动服务器时,我得到一个 feakish 输出...... .

输出

̷̢̢̪̰͔͔̻̻͖̲̬̖̬̯̙͔̙̩͕̻͈͕̙͎̱̟͓͑̕͢Ì�̱͎̹̭͚Í�̞̭͈̤̼͓̟̯͉̩͈̗̤͚̞̺̖̗̦̰̼̤̙͙Í�Ì© ̇͊̂ͣͦ̑̒ͥ̽Ì�̨̌̄̓̚Ì�Ì�ÌœÍ™Í™Ì¹Ì«Í…Ì£Ì—Í•Í•Ì²Í”Í“Í…ÌŸÌ±ÍŽÌ®Ì£Ì­ÍŽÌ«ÌºÌ˜Ì—ÍˆÌ¹Ì™ÌžÍ–Ì™Ì¯Í‰Ì–Ì£Ì ÌªÍ•Ì¬Ì¬Ì©Ì�͈̦̩̞̣͖̼̙͖Í�̖̼͙͔̳͚̬Í�̤̳̘̘̪̺tÌ‚Ì�ͤ̓Ì�ÍªÍ›ÍŒÌ½ÍŒÍ­Ì¶Í¡Ì¸Í Ì�Ì¼ÌªÌ¥Ì Í–Ì¬ÌžÌ¹Ì¦ÍŽÌ˜Ì¦ÍšÍ…Ì¤Ìªe̾͌̌ͨ͗̀̒̾ͭͭ̎̀̋ ҉̡̢̗̭͙͖̙̗̯̩̹͙̭̗͜͢Í�̼̥͙̬Í�̪̼̲Í�͇̜Í�̱͉̪Ì�̗͇̦̳͇̘͔sÍ‘Ì�̈ͩ̿Ì�̒̇ͤÌ�ÍœÍ Ì›Ì°ÌœÌ Ì˜ÌžÌ�ÌžÍ…Ì«ÍŽÌ®Ì«Ì©ÌºÌ¹Ì Í“Ì»Ì±Ì³Í–Ì³Ì©Ì—Ì¥ÍšÍ…Ì¤Ì¤t̾̿̎ͣ͋͊̅Ì�̋̽͆̀Í�̼̲̻̳̯͈͔͕̲̟̥̥̞̤̲iÌ�̶̑̈ͩͩ͆ ҉͕Ì�̯̩̜̫̞͓̮̫̬͎Í�ÌŸÌ™Ì®ÍšÍ”Ì³Ì™Í“Ì ÌºÍŽÌ˜ÍšÌ˜ÌŸÌ±Í•Í…Í‡Í‰Ì°n̎̌̈͌̿̇͋͋̓͆̊̈Í�̵̮̰͖͉̟͖͕̳̫̗̲͎̳̙͕͕̲g͓̯͔̫̺̮͈̟̩̳̭̜͚͔ͤ̎͋̋̀͞͞ͅÌ�̩̦̱͈͈͎͚̦̪̗͈̗͖̫ͅÌ�̲Í�̲̰͖̟͚̦̟̬͓̟̯̹̣̜ͅ ͋̉ͨÌ�̋̒ͨ̽Í�Ì€Í Í˜Ì—ÌªÍ…Í–Ì¼ÍŽÍŽÌªÌ±Ì¥Í…Ì¤Ì©Í–Ì�̩̲̹̺̣̫̻̜̯̰̯̻̯̞̞͙̫tÌ�ͪ̄͂̈Ì�̴͗̿͢͞ Ò‰Í�̹̱̱̞͈̭̣̯̦̜̖͉͈̦̬ͅͅeÌ�̑̈̓̀̉̄ͨÌ�Ì�̸̶̯͙̥̻͓͋ͫͦ͋̈̈̕Í�̰̖͓͕͉̱̖̳͉̯͕̜̼̮͎̯̮̯̤̻s̻̗̺̲̗̪̬̙͉͖̺̟̦͇̘͎͓͎̬̣͙̜͌͋͌̑̆ͫ͊͒͛͆͊̾͆ͥ̀͜ͅtÌ�ͧ̇̊Í�Í�ͥ̃Í�̶͋̕͞Í�̘̤̙͎͙͡Í�̥͖̞̱͇͈͕̳͉͇Í�Ì®Í�Ì�̰̱̭͔̮̭̜̦̭͓̥̳̖͓̦͈̰͉͔̖̣̣͖̯̣̥͎̣̥͎iͯ̄ͦ͛̀Ì�Í—Ì�Í Í�̣̥̱̱̪Í�͔̖̼̪n͊ͨÌ�Ì¿ÌŒÌ¡Í ÌµÍŸÌ·ÍˆÌ®Í…ÌœÌ»Ì¦Ì¤Ì©ÌžÌ¹Ì Ì©Ì¦Ì±Ì±Ì²ÍˆÌ²Í™Ì�̣̥͉̤Ì�̯Ì�Í�Ì®ÌªÌ¤Í…Ì¹Í“ÌŸÌªÌ˜ÌŸÌŸÍ…Ì Í–Ì¼Í…Ì¬Ì–ÌœÌŸÍ�Í–

而不是一个漂亮的可读输出......

有什么想法为什么我无法获得正确的输出吗?

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "scripts": {
    "test": "wdio run wdio.conf.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/preset-env": "^7.1.6",
    "@wdio/allure-reporter": "8.39.0",
    "@wdio/appium-service": "^8.1.3",
    "@wdio/browserstack-service": "^8.1.3",
    "@wdio/cli": "^8.1.3",
    "@wdio/cucumber-framework": "8.1.2",
    "@wdio/globals": "^8.1.3",
    "@wdio/junit-reporter": "^8.1.2",
    "@wdio/local-runner": "^8.1.3",
    "@wdio/sauce-service": "^8.1.3",
    "@wdio/selenium-standalone-service": "^8.1.2",
    "@wdio/spec-reporter": "^8.1.2",
    "@wdio/sync": "7.27.0",
    "allure-commandline": "2.29.0",
    "appium": "^2.4.0",
    "appium-chromedriver": "5.6.41",
    "appium-uiautomator2-driver": "^2.29.10",
    "chai": "^4.2.0",
    "cucumber": "6.0.7",
    "cucumber-pretty": "^1.5.0",
    "cucumber-tsflow": "^3.2.0",
    "eslint": "^8.31.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-n": "^15.0.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^6.1.1",
    "fs-extra": "11.2.0",
    "multiple-cucumber-html-reporter": "3.6.1",
    "request": "^2.34",
    "run": "1.5.0",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4",
    "wdio-chromedriver-service": "^8.0.1",
    "wdio-cucumberjs-json-reporter": "5.1.8",
    "wdio-wait-for": "^3.0.1",
    "webdriverio": "8.1.3"
  },
  "dependencies": {
    "@wdio/codemod": "^0.12.0",
    "axios": "1.6.7",
    "force": "0.0.3",
    "jscodeshift": "^0.14.0",
    "request-promise-native": "1.0.9"
  }
}

我有这样的配置..

wdio.conf.js

const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');

exports.config = {
    exclude: [],
    logLevel: 'info',
    bail: 0,
    waitforTimeout: 20000,
    services: ['appium'],
    path: '/wd/hub/',
    runner: 'local',
    capabilities: [
        {
            platformName: 'Android',
            port: 4723,
            'appium:sessionOverride': false,
            'appium:debugLogSpacing': true,
            'appium:connectionRetryTimeout': 30000,
            'appium:maxInstances': 1,
            'appium:maxInstancesPerCapability': 1,
            'appium:connectionRetryCount': 5,
            'appium:automationName': 'UiAutomator2',
            'appium:nativeInstrumentsLib': true,
            'appium:isolateSimDevice': true,
            'appium:noReset': false,
            'appium:relaxedSecurity': true,
            "appium:autoGrantPermissions": true,
            "appium:autoLaunch": true,
            'appium:ignoreHiddenApiPolicyError': true,
        },
    ],

    framework: 'cucumber',
    specs: [
        './tests/features/*.feature',
    ],
    maxInstances: 1,

    cucumberOpts: {
        require: ['./tests/features/step-definitions/*.js'],
        timeout: 100000,
        reportFilePerRetry: false
    },
};

还有一些简单的执行测试...

特点

Feature: Launch Application

  Scenario: Launching a application
    Given a application
    When I launch the application
    Then the application should run successfully

和一个步骤文件,如

const { Given, When, Then } = require('cucumber');
const { exec } = require('child_process');

Given('a application', function () {
    console.log('application is ready to be launched.');
});

When('I launch the application', function (callback) {
    exec('bash -c "ping remoteHost"', (error, stdout, stderr) => {
        if (error) {
            console.error(`exec error: ${error}`);
            return callback(error);
        }
        console.log(`stdout: ${stdout}`);
        console.error(`stderr: ${stderr}`);
        callback();
    });
});

Then('the application should run successfully', function () {
    console.log('Application ran successfully.');
});

所以启动它是一个简单的安装和运行测试

npm install
npm run test

nodejs 安装了所有内容,appium 启动,但是当 exec 测试启动时,乱码输出永远不会停止,我期待一个像这样的简单 ping 输出...

ping localhost

Pinging remoteHost [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-se

有谁知道为什么这件事发生在可怜的小我身上?

提前感谢您指出我的方法中的错误...

他,

X

node.js testing cucumber exec wdio
1个回答
0
投票

所以,看来我过度设计了这个垃圾,并且运行 cmd 步骤不需要 wdio 和 cucumber 测试。 我刚刚重新组织了执行...

  1. NPM 安装
  2. 在 bash 中运行 init 命令
  3. 运行测试...

在测试脚本中运行 init 命令是一个坏主意,它实际上是通过 wido 驱动程序运行的。因此输出是乱码。

© www.soinside.com 2019 - 2024. All rights reserved.