diff --git a/package.json b/package.json index 0c06d33..33d10f1 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,6 @@ "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.9.0", "chai": "^3.5.0", - "chromedriver": "^2.21.2", "co-mocha": "^1.1.2", "concurrently": "^2.1.0", "cross-env": "^1.0.8", @@ -91,8 +90,8 @@ "node-libs-browser": "^1.0.0", "react-addons-test-utils": "^15.1.0", "redux-logger": "^2.6.1", - "selenium-webdriver": "^2.53.2", "sinon": "^1.17.4", + "spectron": "^3.2.4", "style-loader": "^0.13.1", "webpack": "^1.13.1", "webpack-dev-middleware": "^1.6.1", diff --git a/test/.eslintrc b/test/.eslintrc index 7eeefc3..19e6180 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -1,5 +1,8 @@ { "env": { "mocha": true + }, + "rules": { + "no-unused-expressions": 0 } } diff --git a/test/e2e.js b/test/e2e.js index 2b53232..0acf0a4 100644 --- a/test/e2e.js +++ b/test/e2e.js @@ -1,107 +1,102 @@ -import path from 'path'; -import chromedriver from 'chromedriver'; -import webdriver from 'selenium-webdriver'; +import { Application } from 'spectron'; import { expect } from 'chai'; import electronPath from 'electron-prebuilt'; import homeStyles from '../app/components/Home.css'; import counterStyles from '../app/components/Counter.css'; -chromedriver.start(); // on port 9515 -process.on('exit', chromedriver.stop); - const delay = time => new Promise(resolve => setTimeout(resolve, time)); describe('main window', function spec() { this.timeout(5000); before(async () => { - await delay(1000); // wait chromedriver start time - this.driver = new webdriver.Builder() - .usingServer('http://localhost:9515') - .withCapabilities({ - chromeOptions: { - binary: electronPath, - args: [`app=${path.resolve()}`] - } - }) - .forBrowser('electron') - .build(); + this.app = new Application({ + path: electronPath, + args: ['.'], + }); + return this.app.start(); }); - after(async () => { - await this.driver.quit(); + after(() => { + if (this.app && this.app.isRunning()) { + return this.app.stop(); + } }); - const findCounter = () => this.driver.findElement(webdriver.By.className(counterStyles.counter)); + const findCounter = () => this.app.client.element(`.${counterStyles.counter}`); - const findButtons = () => this.driver.findElements(webdriver.By.className(counterStyles.btn)); + const findButtons = async () => { + const { value } = await this.app.client.elements(`.${counterStyles.btn}`); + return value.map(btn => btn.ELEMENT); + }; it('should open window', async () => { - const title = await this.driver.getTitle(); + const { client, browserWindow } = this.app; + + await client.waitUntilWindowLoaded(); + await delay(500); + const title = await browserWindow.getTitle(); expect(title).to.equal('Hello Electron React!'); }); it('should to Counter with click "to Counter" link', async () => { - const link = await this.driver.findElement(webdriver.By.css(`.${homeStyles.container} > a`)); - link.click(); + const { client } = this.app; - const counter = await findCounter(); - expect(await counter.getText()).to.equal('0'); + await client.click(`.${homeStyles.container} > a`); + expect(await findCounter().getText()).to.equal('0'); }); it('should display updated count after increment button click', async () => { - const buttons = await findButtons(); - buttons[0].click(); + const { client } = this.app; - const counter = await findCounter(); - expect(await counter.getText()).to.equal('1'); + const buttons = await findButtons(); + await client.elementIdClick(buttons[0]); // + + expect(await findCounter().getText()).to.equal('1'); }); it('should display updated count after descrement button click', async () => { - const buttons = await findButtons(); - const counter = await findCounter(); - - buttons[1].click(); // - + const { client } = this.app; - expect(await counter.getText()).to.equal('0'); + const buttons = await findButtons(); + await client.elementIdClick(buttons[1]); // - + expect(await findCounter().getText()).to.equal('0'); }); it('shouldnt change if even and if odd button clicked', async () => { - const buttons = await findButtons(); - const counter = await findCounter(); - buttons[2].click(); // odd + const { client } = this.app; - expect(await counter.getText()).to.equal('0'); + const buttons = await findButtons(); + await client.elementIdClick(buttons[2]); // odd + expect(await findCounter().getText()).to.equal('0'); }); it('should change if odd and if odd button clicked', async () => { - const buttons = await findButtons(); - const counter = await findCounter(); + const { client } = this.app; - buttons[0].click(); // + - buttons[2].click(); // odd - - expect(await counter.getText()).to.equal('2'); + const buttons = await findButtons(); + await client.elementIdClick(buttons[0]); // + + await client.elementIdClick(buttons[2]); // odd + expect(await findCounter().getText()).to.equal('2'); }); it('should change if async button clicked and a second later', async () => { - const buttons = await findButtons(); - const counter = await findCounter(); - buttons[3].click(); // async - - expect(await counter.getText()).to.equal('2'); + const { client } = this.app; - await this.driver.wait(() => - counter.getText().then(text => text === '3') - , 1000, 'count not as expected'); + const buttons = await findButtons(); + await client.elementIdClick(buttons[3]); // async + expect(await findCounter().getText()).to.equal('2'); + await delay(1000); + expect(await findCounter().getText()).to.equal('3'); }); it('should back to home if back button clicked', async () => { - const link = await this.driver.findElement( - webdriver.By.css(`.${counterStyles.backButton} > a`) - ); - link.click(); - - await this.driver.findElement(webdriver.By.className(homeStyles.container)); + const { client } = this.app; + await client.element( + `.${counterStyles.backButton} > a` + ).click(); + + expect( + await client.isExisting(`.${homeStyles.container}`) + ).to.be.true; }); });