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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
joe_re
April 24, 2017
Technology
840
1
Share
Effective E2E Test In An Electron Application
第36回 西日暮里.rb 「Electronではじめるアプリ開発」発売記念 LT
joe_re
April 24, 2017
More Decks by joe_re
See All by joe_re
Eyes on Claude Code
joere
0
110
Building Public API with GraphQL
joere
3
130
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
350
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
主催・運営として"場をつくる”というアウトプットのススメ
_mossann_t
0
110
Network Firewall Proxyで 自前プロキシを消し去ることができるのか
gusandayo
0
190
ログ基盤・プラグイン・ダッシュボード、全部整えた。でも最後は人だった。
makikub
1
210
出版記念イベントin大阪「書籍紹介&私がよく使うMCPサーバー3選と社内で安全に活用する方法」
kintotechdev
0
150
サイボウズフロントエンドの活動から考える探究と発信
mugi_uno
0
110
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
10
77k
「できない」のアウトプット 同人誌『精神を壊してからの』シリーズ出版を 通して得られたこと
comi190327
3
570
Data Intelligence Engineering Unit 部門と各ポジション紹介
sansantech
PRO
0
120
GitHub Advanced Security × Defender for Cloudで開発とSecOpsのサイロを超える: コードとクラウドをつなぐ、開発プラットフォームのセキュリティ
yuriemori
1
130
Tour of Agent Protocols: MCP, A2A, AG-UI, A2UI with ADK
meteatamel
0
200
最大のアウトプット術は問題を作ること
ryoaccount
0
300
ADOTで始めるサーバレスアーキテクチャのオブザーバビリティ
alchemy1115
2
130
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
72
12k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
160
Marketing to machines
jonoalderson
1
5.1k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.6k
How to build a perfect <img>
jonoalderson
1
5.3k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
150
Into the Great Unknown - MozCon
thekraken
40
2.3k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.4k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
Designing for Performance
lara
611
70k
How to Ace a Technical Interview
jacobian
281
24k
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!