Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Effective E2E Test In An Electron Application
Search
joe_re
April 24, 2017
Technology
1
840
Effective E2E Test In An Electron Application
第36回 西日暮里.rb 「Electronではじめるアプリ開発」発売記念 LT
joe_re
April 24, 2017
Tweet
Share
More Decks by joe_re
See All by joe_re
Eyes on Claude Code
joere
0
100
Building Public API with GraphQL
joere
3
120
Traversing the GraphQL AST and Calculating Query Costs
joere
0
1.2k
Real-Time applications with GraphQL
joere
0
290
Prisma2 with Graphql
joere
3
1k
Go beyound static on Netlify
joere
1
340
Building Real-time Vue App
joere
4
4.7k
ReactNativeのAsyncStorageをNodeのReplから操作する
joere
0
340
Mock Native API in your E2E test
joere
2
1.2k
Other Decks in Technology
See All in Technology
ナレッジワークのご紹介(第88回情報処理学会 )
kworkdev
PRO
0
210
Shifting from MCP to Skills / ベストプラクティスの変遷を辿る
yamanoku
4
860
Cortex Code CLI と一緒に進めるAgentic Data Engineering
__allllllllez__
0
180
JAWS DAYS 2026 楽しく学ぼう!ストレージ 入門
yoshiki0705
2
180
It’s “Time” to use Temporal
sajikix
1
140
Keycloak を使った SSO で CockroachDB にログインする / CockroachDB SSO with Keycloak
kota2and3kan
0
120
ガバメントクラウドにおけるAWSの長期継続割引について
takeda_h
2
120
AI時代の「本当の」ハイブリッドクラウド — エージェントが実現した、あの頃の夢
ebibibi
0
120
Claude Code のコード品質がばらつくので AI に品質保証させる仕組みを作った話 / A story about building a mechanism to have AI ensure quality, because the code quality from Claude Code was inconsistent
nrslib
13
7.9k
ナレッジワーク IT情報系キャリア研究セッション資料(情報処理学会 第88回全国大会 )
kworkdev
PRO
0
190
猫でもわかるKiro CLI(AI 駆動開発への道編)
kentapapa
0
200
JAWS DAYS 2026 ExaWizards_20260307
exawizards
0
430
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
180
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
62
51k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
440
Designing for humans not robots
tammielis
254
26k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.8k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
250
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
220
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
86
Designing for Timeless Needs
cassininazir
0
160
Become a Pro
speakerdeck
PRO
31
5.8k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
160
Transcript
Effective E2E Test In An Electron Application @joere
Who am I? twitter: @joe_re github: @joere 西日暮里.rb オーガナイザ working
in freee.K.K
最近本を書きました
今日はElectron の E2E テストの書き方について 3 つのポイントをお話します
Spectron
What is Spectron? Electron アプリケーションのE2E テストを実行するための テストツール テストコードの中でElectronAPI を叩くことができる ChromeDriver
+ WebDriverIO を通じてアプリケーションの 操作、情報取得が行える
Demo CafePitch https://github.com/joere/cafepitch Electron 製のマークダウンで書けるプレゼンツール このスライドもCafePitch で作っています
Example const app = new spectron.Application({ path: 'path/to/your/app' }); describe('application
launch', function () { this.timeout(10000); beforeEach(function () { return app.start(); }); afterEach(function () { return app.stop(); }); it('shows an initial window', function () { return app.client.getWindowCount().then(function (count) { assert.equal(count, 1); }); }); });
Strong Points Webdriver.io を通じて、任意のDOM 要素にアクセスして テストが書ける Electron のAPI をテストコードの中から透過的に呼び出す ことができる
Electron 内部のChromium をそのまま使うのでセットアップ の手間がない( 基本的にパスを指定するだけ) Travis やAppVeyor などのCI サービスをサポートしている
しかし、E2E テストは 壊れやすくメンテナンス が大変...
Effective1: Using Page Object Pattern
Why maintenance of E2E tests are difficult? E2E テストは画面変更に弱い 画面を変更すると関連するすべてのテストケースに
影響がある 画面要素へのアクセスが大量に発生するので 修正すべき箇所を把握しづらい
Page Object Pattern is... テストコードをページとシナリオに分ける アプリケーションの1 つの画面をオブジェクトと捉える (Page Object) Page
Object にはアサーションは含まず、 ページ操作を振る舞いとして記述する アサーションはシナリオに記述する (Page Object はライブラリとして利用されるイメージ) PageObject にはアサーションは含まない 他にも原則があるので、詳しくはSelenium の公式ページへ (https://github.com/SeleniumHQ/selenium/wiki/PageObjects)
Strong Points シナリオから煩雑な画面操作が取り除かれるので、 見通しが良くなる 複数のシナリオから1 つのPage Object を利用するので、 画面変更時にはPage Object
を変更するだけで良い ( 画面変更に強い) Page Object の振る舞い === 画面操作となるので、 変更箇所の把握が用意になる
Example export default class SlideEditorPage { constructor(private client: Client<void>) {}
inputText(text: string): WebdriverIO.Client<void> { return this.client.waitForExist('#editor').then(() => { this.client.setValue('#editor textarea', text); }); } getSlideHtml(): WebdriverIO.Client<string> { return this.client.waitForExist('.slide-content') .then(() => this.client.getHTML('.slide-content')) .then((html) => typeof html === 'string' ? html : html.join()); } ... }
話は変わって
皆さんE2E テスト 書いてますか
テストが失敗したとき どうしてますか
E2E テストの失敗は 原因を追うのが難しい...
そこで
Effective2: Record the state at the time of test failure
Electron + Spectron のAPI を利用して 失敗時の状態を取得して記録する 画面キャプチャ BrowserWindow.capturePage() ログ client.getRenderProcessLogs()
client.getMainProcessLogs()
Demo
Example function capturePage(app: Application, testName: string) { return app.browserWindow.capturePage().then((img) =>
{ fs.writeFileSync(`${outputDir}/capture_${testName}.png`, img); }); } function reportLog(app: Application, testName: string) { return Promise.all([ app.client.getRenderProcessLogs(), app.client.getMainProcessLogs() ]).then(([ rendererLogs, mainLogs ]) => { const logs = JSON.stringify({ renderer: rendererLogs, main: mainLogs }); fs.writeFileSync(`${outputDir}/logs_${testName}.txt`, logs, "utf8"); }); }
Effective3: Mock a native API
現在のSpectron(v3.6.2) では メニューの操作やファイルダイアログの 操作はできない https://github.com/electron/spectron/issues/21 Menu のAPI がJSON にserialize されていないので、process
間通信が必要なSpectron では操作が難しい アプリケーションの操作をChrome Driver を通じて行うのが Spectron の基本なので、これらのnative なAPI を必要とする 操作に対するサポートはまだ不十分
ところでElectron は ネイティブなAPI 呼び出しも、 JavaScript から 扱えるようにするため、 JavaScript のレイヤーで ラップしている
つまりElectron のAPI を 局所的に差し替えてしまえば テストができる
Electron のrequire オプションを利用する Electron は --require オプションで、起動直前にスクリプト を読み込ませることができる これを利用して、テスト実行時だけ1 部のElectron
のAPI を 書き換えるスクリプトを読み込む
Menu は作ってライブラリにした spectronfakemenu https://github.com/joere/spectronfakemenu Spectron から任意のラベルを持つメニューの クリックができる fakeMenu.apply(app); // apply
fake menu fakeMenu.clickMenu('Config'); // 'Config' Menu click fakeMenu.clickMenu('File', 'CloseTab'); // File->CloseTab Menu click
ダイアログはこんな感じで差し替える const { dialog } = require('electron'); function mockShowSaveDialog() {
return 'sandbox/test.md'; }; function mockShowOpenDialog() { return [ 'sandbox/test.md' ]; }; dialog.showSaveDialog = mockShowSaveDialog; dialog.showOpenDialog = mockShowOpenDialog;
Demo
E2E テストでモックを使うのは 基本的にマナー違反なので、 あくまで必要最小限にしましょう
おさらい Effective1: Using Page Object Pattern Effective2: Record the state
at the time of test failure Effective3: Mock a native API
ところでこの話は全部本 に書いてあります( 宣伝)
Thanks for your attention!