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
770
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
Building Public API with GraphQL
joere
3
56
Traversing the GraphQL AST and Calculating Query Costs
joere
0
960
Real-Time applications with GraphQL
joere
0
200
Prisma2 with Graphql
joere
3
910
Go beyound static on Netlify
joere
1
260
Building Real-time Vue App
joere
4
4.6k
ReactNativeのAsyncStorageをNodeのReplから操作する
joere
0
300
Mock Native API in your E2E test
joere
2
1.1k
Data feching and caching on Apollo Client
joere
2
2.7k
Other Decks in Technology
See All in Technology
(機械学習システムでも) SLO から始める信頼性構築 - ゆる SRE#9 2025/02/21
daigo0927
0
250
OpenID BizDay#17 KYC WG活動報告(法人) / 20250219-BizDay17-KYC-legalidentity
oidfj
0
450
Perlの生きのこり - エンジニアがこの先生きのこるためのカンファレンス2025
kfly8
1
250
RayでPHPのデバッグをちょっと快適にする
muno92
PRO
0
150
システム・ML活用を広げるdbtのデータモデリング / Expanding System & ML Use with dbt Modeling
i125
1
310
AIエージェント時代のエンジニアになろう #jawsug #jawsdays2025 / 20250301 Agentic AI Engineering
yoshidashingo
4
380
クラウドサービス事業者におけるOSS
tagomoris
3
980
遷移の高速化 ヤフートップの試行錯誤
narirou
6
980
NFV基盤のOpenStack更新 ~9世代バージョンアップへの挑戦~
vtj
0
330
実は強い 非ViTな画像認識モデル
tattaka
1
1.1k
ソフトウェアエンジニアと仕事するときに知っておいたほうが良いこと / Key points for working with software engineers
pinkumohikan
1
140
あなたが人生で成功するための5つの普遍的法則 #jawsug #jawsdays2025 / 20250301 HEROZ
yoshidashingo
1
110
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
Practical Orchestrator
shlominoach
186
10k
How GitHub (no longer) Works
holman
314
140k
Fireside Chat
paigeccino
34
3.2k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
640
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
250
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
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!