electron-userland / spectron

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

How to use protractor with Spectron #164

Open sn0opr opened 7 years ago

sn0opr commented 7 years ago

I can't use Protractor with Spectron, are any tutorial to how to use Protractor for my e2e tests ?

sn0opr commented 7 years ago

bump

MarshallOfSound commented 7 years ago

@sn0opr Please don't "bump" things on GitHub, all it does is send more notifications to people who have already read the original question. If someone knows the answer you're looking for I'm sure they will respond.

jguze commented 7 years ago

@sn0opr I had a small version of this working. The main goal should be to launch the app using Spectron, and attach Protractor to the ChromeDriver session created by Spectron. Here's the advice I can give you to get you going:

1. Launch your app with Spectron

Follow the instructions in the README, and launch your app using Spectron. This will start the ChromeDriver instance and a session. If you didn't mess with the default settings, you will see that a chromedriver instance is launched at the endpoint: http://localhost:9515/wd/hub. Also take not that Spectron provides WebDriverIO through the app.client module, we have a nice set of options here.

2. Get the session id

Since our goal is to attach to the session created by Spectron, we need to get the session id. Looking at the WebdriverIO API docs, you can grab it through the /sessions API

3. Attach Protractor to the existing session

Luckily for us, Protractor added this functionality about a year ago in 3f3805f

You can create a really simple conf.js file with the following options:

seleniumAddress: http://localhost:9515/wd/hub,
seleniumSessionId: <session id from Step 2>

After all that, you should have a Protractor instance attached to the session created by Spectron.

sn0opr commented 7 years ago

@jguze Thank you very much for the help! I will try it and give you feedback.

luismanuel001 commented 7 years ago

@sn0opr did you ever give this a try?, do you mind sharing?

scuy commented 7 years ago

I just wanted to let you know that I got this running with:

const Application = require('spectron').Application;
const spawn = require('child_process').spawn;
const http = require('http');

let app = new Application({
    path: './node_modules/.bin/electron',
    args: ['.']
});

app.start().then(() => app.client.sessions()).then(sessions => {

    const sessionId = sessions.value[0].id;

    return new Promise(resolve => {
        const protractor = spawn('protractor', [
            'test/e2e/config/protractor-spectron.conf.js',
            '--seleniumSessionId=' + sessionId
        ]);
        protractor.stdout.setEncoding('utf8');
        protractor.stdout.on('data', data => console.log(data));
        protractor.stderr.setEncoding('utf8');
        protractor.stderr.on('data', data => console.error(data));
        protractor.on('close', code => resolve(code));
    });

}).then(code => {
    app.stop().then(() => process.exit(code));
});

And in test/e2e/config/protractor-spectron.conf.js:

seleniumAddress: 'http://localhost:9515/wd/hub',
baseUrl: 'file://' + path.resolve(__dirname, '../../..') + '/index.html',
troppoli commented 4 years ago

I started with the above snippet and was able to avoid the spawn by patching into protractor's startup hooks to launch spectron and get the session id.

in package.json scripts: "dbg_protract": "node --nolazy --inspect-brk=5858 ./node_modules/protractor/bin/protractor PATH_TO_YOUR_config.js",

here's the typescript for config.ts

import {Config} from 'protractor';
import { Application } from 'spectron';

let app:Application;

export let config: Config = {
    framework: 'jasmine',
    specs:['something_spec.js'],
    seleniumAddress:'http://localhost:9515/wd/hub',
    beforeLaunch: async()=>{
        app = new Application({
            path: 'PATH_TO_YOUR_ELECTRON_APP.exe',
            webdriverLogPath:'c:\\temp\\webdriverlog.log'
          })
          await app.start();
          let s:any = await app.client.session();
          config.seleniumSessionId = s.sessionId;
    },
    afterLaunch: async (exitCode: number) =>{
        return app.stop();
    }
};

vscode launch config:

          {
            "name": "dbg pro",
            "type": "node",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "run-script", "dbg_protract"
            ],
            "port": 5858
        }