electron-userland / spectron

DEPRECATED: 🔎 Test Electron apps using ChromeDriver
http://electronjs.org/spectron
MIT License
1.68k stars 229 forks source link

app.client.getText is not a function #815

Open AndreiSoroka opened 3 years ago

AndreiSoroka commented 3 years ago

Hello!

I'm trying to get a text from the page, but i can't. I get error: app.client.getText is not a function;

A wrote all errors in comments (in the code below). Thanks!

pakage.json

"test:e2e": "mocha -r esm -b ./tests/e2e/",
...
"spectron": "^11.1.0",
"electron": "^9.4.0",
"vue-cli-plugin-electron-builder": "^2.0.0-rc.5",

P.s. https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/testingAndDebugging.html#testing

background.js

//...
  win = new BrowserWindow({
    width: 800,
    height: 600,
    minHeight: 300,
    minWidth: 500,
    webPreferences: {
      enableRemoteModule: true,
      nodeIntegration: true,
      preload: __dirname + '/../src/preload.js',
      webSecurity: false,
    },
  });
//...

tests/e2e/index.spec.js

import { assert } from 'chai';
import spectron from 'spectron';
import { testWithSpectron } from 'vue-cli-plugin-electron-builder';

describe('E2E Test', function () {
  this.timeout(500000);

  before(async function () {
    this.electronApplication = await testWithSpectron(spectron);
  });

  after(async function () {
    await this.electronApplication.stopServe();
  });

  it('Open Application', async function () {
      const { app } = this.electronApplication;

      const isRunning = await app.isRunning();
      assert.equal(isRunning, true, 'App should be running');

      const isVisible = await app.browserWindow.isVisible();
      assert.equal(isVisible, true, 'Window should be visible');

      const count = await app.client.getWindowCount();
      assert.equal(count, 1, 'Window count should be 1');

      const title = await app.client.getTitle();
      assert.equal(title, 'Recruitment Tools | FuryFerret', 'Title should be right');

      await app.webContents.openDevTools();
  });

  it('Rules page', async function () {
    const { app } = this.electronApplication;

    const agreeRulesText = await app.client.getText('[data-test-id="agreeRules"]');

    /*
    ---- Error:
    TypeError: app.client.getText is not a function
      at Context.<anonymous> (tests/e2e/index.spec.js:27:45)
      at processImmediate (internal/timers.js:456:21)
      at process.topLevelDomainCallback (domain.js:137:15)
     */

    const agreeRulesText = await app.webContents.executeJavaScript(`2+5`);
    console.log('agreeRulesText', agreeRulesText);
    /*
    ---- OK
    agreeRulesText7
     */

    const agreeRulesText = await app.webContents.executeJavaScript(`$('[data-test-id="agreeRules"]').textContent`);
    console.log('agreeRulesText', agreeRulesText);
    /*
    - Error:
     javascript error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
JavaScript stack:
Error: Script failed to execute, this normally means an error was thrown. Check the renderer console for the error.
    at WebFrame.e.startsWith.e.startsWith.WebFrame.<computed> [as _executeJavaScript] (electron/js2c/renderer_init.js:87:1542)
    at electron/js2c/renderer_init.js:139:429
    at electron/js2c/renderer_init.js:123:361
    at EventEmitter.<anonymous> (electron/js2c/renderer_init.js:127:872)
    at EventEmitter.emit (events.js:223:5)
    at Object.onMessage (electron/js2c/renderer_init.js:115:818)
  (Session info: chrome=83.0.4103.122)

  ---- Console in browser
      Uncaught ReferenceError: $ is not defined
        at <anonymous>:1:1
        at WebFrame.e.startsWith.e.startsWith.WebFrame.<computed> [as _executeJavaScript] (electron/js2c/renderer_init.js:87)
        at electron/js2c/renderer_init.js:139
        at electron/js2c/renderer_init.js:123
        at EventEmitter.<anonymous> (electron/js2c/renderer_init.js:127)
        at EventEmitter.emit (events.js:223)
        at Object.onMessage (electron/js2c/renderer_init.js:115)
    (anonymous) @ VM281:1
    e.startsWith.e.startsWith.WebFrame.<computed> @ electron/js2c/renderer_init.js:87
    (anonymous) @ electron/js2c/renderer_init.js:139
    (anonymous) @ electron/js2c/renderer_init.js:123
    (anonymous) @ electron/js2c/renderer_init.js:127
    emit @ events.js:223
    onMessage @ electron/js2c/renderer_init.js:115

    // todo but it's ok:
    $('[data-test-id="agreeRules"]').textContent
    "Agree with the rules?"
     */

    app.webContents.savePage('/Users/kevin/page.html', 'HTMLComplete')
      .then(function () {
        console.log('page saved')
      }).catch(function (error) {
      console.error('saving page failed', error.message)
    })
    /*
    ---- Result
    Page is ok, html is full
    */

    assert.equal(agreeRulesText, "Agree with the rules?");
  });
});
AndreiSoroka commented 3 years ago

Hi there. I resolved the problem.

Need downgrade spectron@11.1.0 to spectron@11.0.0 (for electron@9.4.0).