JavaScript 的 Bitbar Cloud Appium 服务器端测试包内容

Bitbar Cloud Appium Server Side test package content for JavaScript

有人知道在使用 Javascript 作为语言时用于服务器端测试 运行 的 zip 文件的内容应该是什么吗?我查看了 github 页面上的 python 和 ruby 示例(GitHub bitbar-samples repository), but there is nothing for Javascript, and the same is true in the documentation - 只是 java 和 python。但是什么都没有Javascript.

来自文档:

All needed files for testing need to be uploaded to cloud as zip package. This package must contain test files, data files and needs to have a Shell script to launch test execution at the root level of the package.

并且:

For the cloud side execution to work properly, it is important to have the test suite zip outfitted with the right files. Here we will go through the files and also the correct makeup of the test zip. The most important is the run-tests.sh file, it is responsible for starting the test execution in the cloud and also installing requirements.

因此,理论上,ZIP 包中只能包含 run-tests.sh 文件。因为这是一个简单的 bash 脚本,所以您可以自由选择该脚本的功能。查看 Python 和 Ruby 示例,这是我的 ZIP 结构的样子:

.
├── package.json
├── package-lock.json
├── run-tests.sh
├── test
│   └── specs
│       └── main.js
└── wdio.conf.js

我用过 Webdriver.io Testrunner - 这就是我用 wdio.conf.jstest/specs/main.js 的原因。

这是我的 run-tests.sh 的样子:

#!/usr/bin/env bash

echo "Preparing..."

# Make sure there's no pre-existing `screenshots` file blocking symbolic link creation
rm -rf screenshots

# Recreate screenshots dir
mkdir screenshots

echo "Extracting tests.zip..."
unzip tests.zip

echo "Installing dependencies..."
npm install

echo "Running tests..."
./node_modules/.bin/wdio wdio.conf.js

我的main.js(我用过bitbar-sample-app.apk,测试方法从上到下):

describe('Bitbar Sample App', () => {
    it('Should radio button should be visible ', async () => {
        let el = await $('//android.widget.RadioButton[@text="Buy 101 devices"]');
        let visible = await el.isDisplayed();
        visible.should.be.true;
    });

    it('Should show failure page', async () => {
        let el;

        console.log("view1: Clicking button - 'Buy 101 devices'");
        el = await $('//android.widget.RadioButton[@text="Buy 101 devices"]');
        el.click();

        console.log("view1: Typing in textfield[0]: Bitbar user");
        el = await $('//android.widget.EditText[@resource-id="com.bitbar.testdroid:id/editText1"]');
        el.setValue('Bitbar user');

        driver.hideKeyboard();
        console.log("view1: Taking screenshot screenshot1.png");
        await takeScreenshot('screenshot1');

        console.log("view1: Clicking button Answer");
        el = await $('//android.widget.Button[@text="Answer"]');
        el.click();

        console.log("view2: Taking screenshot screenshot2.png");
        await takeScreenshot('screenshot2');

        el = await $('//android.widget.TextView[@text="Wrong Answer!"]');
        let txt = await el.getText();
        txt.should.be.equal('Wrong Answer!');
    });

});

还有 wdio.conf.js(看看 before 钩子):

const path = require('path');

exports.config = {
    runner: 'local',

    framework: 'mocha',
    mochaOpts: {
        ui: 'bdd',
        timeout: 60000
    },

    logLevel: 'silent',
    deprecationWarnings: true,

    bail: 0,
    waitforTimeout: 10000,
    connectionRetryTimeout: 90000,
    connectionRetryCount: 3,

    reporters: [
        'spec',
        [
            'junit', {
                outputDir: './',
                outputFileFormat: () => {
                    return 'TEST-all.xml';
                }
            }
        ]
    ],

    host: '127.0.0.1',
    port: 4723,
    path:  '/wd/hub',

    services: ['appium'],
    appium: {
        command: 'appium',
        logPath : './',
    },

    specs: [
        './test/specs/**/*.js'
    ],

    capabilities: [{
        platformName: 'Android',
        maxInstances: 1,

        'appium:deviceName': 'Android device',
        'appium:automationName': 'UiAutomator2',
        'appium:app': path.resolve('application.apk'),
        'appium:appActivity': '.BitbarSampleApplicationActivity',
        'appium:appPackage': 'com.bitbar.testdroid',
        'appium:newCommandTimeout': 240
    }],

    before: function() {
        const chai = require('chai');
        global.expect = chai.expect;
        chai.should();

        const fs = require('fs');

        global.takeScreenshot = async (fileName) => {
            let screenshot = await driver.takeScreenshot();
            screenshot = screenshot.replace(/^data:image\/png;base64,/, "")
            let filePath = path.resolve(`./screenshots/${fileName}.png`);
            fs.writeFileSync(filePath, screenshot, 'base64');
        };

    }
}

最后但并非最不重要的 package.json:

{
  "name": "appium-server-side-example",
  "version": "1.0.0",
  "description": "Bitbar Cloud Appium Server Side Test Example",
  "author": "Marek Sierociński <marek.sierocinski@smartbear.com>",
  "license": "ISC",
  "dependencies": {
    "@wdio/appium-service": "^5.16.5",
    "@wdio/cli": "^5.16.7",
    "@wdio/junit-reporter": "^5.15.5",
    "@wdio/local-runner": "^5.16.7",
    "@wdio/mocha-framework": "^5.16.5",
    "@wdio/spec-reporter": "^5.16.5",
    "@wdio/sync": "^5.16.5",
    "chai": "^4.2.0"
  }
}

如您所见,我使用了 chai(因为我想使用 BDD 方法)和 junit-reporter(因为 Bitbar 开发人员是 Java 怪胎,您可以从示例中猜出Cloud 正在读取 JUnit 文件以读取测试方法)。

它对我有用: