Spectron:启动应用程序后无法与电子应用程序上的元素进行交互

时间:2019-07-22 05:52:19

标签: testing automation electron spectron

我可以使用Spectron启动电子应用程序,但不能对其执行任何操作。

操作系统:Windows 8.1

节点版本:10.16.0

Spectron:3.8.0

var Application = require('spectron').Application;
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
const SearchPage = require('./page-objects/search.page');
const assert= require('assert');

describe('Test Suite', function () {
    this.timeout(20000);
    beforeEach('Start Application',  function () {
        this.app = new Application({
            path: 'path of .exe file located', // Ex: D:\\Foldername\\filename.exe
            requireName:'electronRequire',
            env: {
                NODE_ENV: 'test'
            }
        });
        chai.should();
        chai.use(chaiAsPromised);
        chaiAsPromised.transferPromiseness = this.app.transferPromiseness;
        return  this.app.start()
    });

    afterEach(() => {
        if (this.app && this.app.isRunning()) {
            return this.app.stop();
        }
    });

    it('Sign In, function () {
         return this.app.client.
            .pause(20000) //waiting for login window
            .setValue(SearchPage.username, 'username').pause(1000)
            .setValue(SearchPage.password, 'password').pause(1000)
            .click(SearchPage.loginButton);
    });         

});

Package.json文件:

{
  "name": "spectron-test-framework",
  "version": "1.0.0",
  "description": "Test Framework for Electron Desktop Application",  
  "main": "index.js",
  "scripts": {
    "test": "mocha --timeout 20000",
  },
  "author": "Tester",
  "license": "ISC",
  "devDependencies": {
    "webdriverio": "^4.10.2",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "electron": "^2.0.2",
    "mocha": "^5.2.0",
    "mochawesome": "^3.0.2",
    "spectron": "^3.8.0"

  }
}

我无法交互元素,并且看到错误

1)测试套件        登入:      错误:超时超过20000ms。对于异步测试和挂钩,请确保调用了“ done()”;如果返回承诺,请确保其解决。 (D:\ spectron-example \ Spec.js)

即使我增加了超时时间,仍然会看到此超时错误。

如何解决此问题?

我的问题是

我们可以在没有开发代码库的情况下开始编写自动化脚本吗?因为在大多数示例测试中,我已经看到自动化脚本可以在开发代码的测试文件夹中找到。

启动时我有.exe,可以看到正在启动的应用程序,但无法执行操作。是否需要将.exe文件提供给QA以便在启用/禁用任何特定程序包和选项的情况下进行自动化。

理想的电子,Spectron,Nodejs,Wdio,Mocha兼容版本可以一起使用吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

您似乎将超时设置为20秒this.timeout(20000);,在您的it步骤中,您要做的第一件事是暂停20秒app.client.pause(20000),以便达到超时在尝试设置值之前。这将解释输出Error: Timeout of 20000ms exceeded

但是当我开始使用Spectron时,我遇到了类似的错误。我看到应用程序正常启动,并且可以查看登录页面,但是当我尝试与输入字段进行交互时,我得到了:Error: unable to locate element.我尝试使用app.client.pause()来消除出现计时问题的机会,但这没有帮助。

Spectron做windowByIndex(0),您作为测试作者需要管理电子应用程序中的窗口数。在我们的应用程序中,有许多开发人员插件(例如Devotion,React,MobX)创建了自己的窗口。

为诊断问题,我使用了一个等待策略,该策略将暂停直到app.client.getWindowCount()等于4。但这并不是一个很好的解决方案,因为以后添加新的dev插件时,它将开始失败。

还有另一个棘手的部分。我不能只是启动应用程序,而只是告诉它专注于第五个窗口。因为当应用程序首次初始化时,仅存在3个窗口。最后两个窗口的初始化花费了一段时间,并且页面渲染花费了更多时间。这要求我们实施更好的等待策略,以便我们不会在应用就绪之前尝试与之交互。

此解决方案可能对您不起作用,但对我们来说确实很可靠。初始化客户端后,我使用了一个名为waitUntilWindow的函数,并将与登录页面相关联的urlPart传递给该函数,然后执行app.client.windowByIndex将焦点设置在正确的窗口上。到那时,我可以与输入字段进行交互,并且.setValue的工作原理与往常一样。

如果此信息比帮助更令人困惑,我深表歉意。我一直在努力与Spectron合作,但它仍然是UI测试电子应用程序的最佳选择。恩典与和平的兄弟。

await waitUntilWindow(app, 'bundle=login', 'Login window never appeared.', 15000);

export async function waitUntilWindow(app, urlPart, msg, timeoutMs = 15000, interval = 150) {
  await app.client.waitUntil(
    async () => {
      return selectWindow(app, urlPart);
    },
    timeoutMs,
    msg || `Didn't see window with url part ${urlPart} in ${timeoutMs} ms.`,
    interval
  );
}

// Will return true if found the window with certain urlPart text in URL,
// or false if it did not.
export async function selectWindow(app, urlPart) {
  const windowCount = await app.client.getWindowCount();
  for (let i = 0; i < windowCount; i++) {
    await app.client.windowByIndex(i);
    const url = await app.client.getUrl();
    if (url.includes(urlPart)) {
      return true;
    }
  }
  return false;
}