กระบวนการทดสอบตั้งแต่ต้นจนจบ
นำกลยุทธ์การทดสอบตั้งแต่ต้นจนจบไปใช้เพื่อตรวจสอบลำดับการใช้งานและฟังก์ชันการทำงานทั้งหมดของแอปพลิเคชันเดสก์ท็อป Electron
กระบวนการทดสอบตั้งแต่ต้นจนจบ เป็นบทเรียน Electron Desktop App Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Electron Desktop App Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is End-to-End Testing?
End-to-End (E2E) testing simulates real user scenarios to verify that an application works as expected from start to finish. Think of it as putting yourself in the user's shoes!
For Electron apps, E2E tests check if your UI (HTML/CSS/JS) correctly interacts with your backend logic (Node.js) and native OS features.
Why E2E for Electron Apps?
Electron applications are complex, combining web technologies with native desktop capabilities. Unit tests check small parts, but E2E ensures everything works together.
- Real User Flow: Mimics how a user navigates and interacts.
- Integration Checks: Verifies communication between main and renderer processes.
- Catch Regressions: Detects if new changes break existing features.
- Confidence: Gives you peace of mind that your app is stable.
Choosing an E2E Tool
Several tools can perform E2E testing for web applications, and many are adaptable for Electron. Popular choices include:
- Playwright: A modern framework from Microsoft, known for speed and reliability. It has built-in support for Electron.
- Cypress: A developer-friendly tool that runs tests directly in the browser (or Electron's renderer).
- WebDriver (Selenium): A classic choice, though often more complex to set up for Electron specifically.
We'll focus on Playwright due to its strong Electron support.
Setting Up Playwright for Electron
First, you need to install Playwright and its Electron driver in your project. Open your terminal in your Electron app's root directory and run:
npm install --save-dev @playwright/test playwright-electron
This command adds the necessary packages to your development dependencies. Remember, Playwright tests are typically run in a separate test folder.
Our Sample Electron App
To test, let's use a simple Electron app. Create these files in your project root:
package.json:
{ "name": "my-electron-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^28.0.0"
}
}Sample App: Main Process & UI
Next, create the main process file and the user interface:
main.js:
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
mainWindow.loadFile('index.html');
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});Sample App: HTML & Preload
Finally, our simple UI and an empty preload script:
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Electron App</title>
<style>
body { font-family: sans-serif; text-align: center; padding-top: 50px; }
h1 { color: #333; }
button { padding: 10px 20px; font-size: 16px; cursor: pointer; }
</style>
</head>
<body>
<h1>Hello, CoddyKit!</h1>
<p id="message">Click the button below.</p>
<button id="myButton">Change Message</button>
<script>
document.getElementById('myButton').addEventListener('click', () => {
document.getElementById('message').textContent = 'Message Changed!';
});
</script>
</body>
</html>Writing Your First E2E Test
Create a file named e2e.spec.js in a tests folder. This test launches your Electron app and verifies its title.
tests/e2e.spec.js:
const { test, expect, _electron: electron } = require('@playwright/test');
test('app launches and shows title', async () => {
// Launch the Electron app from the current directory
const electronApp = await electron.launch({ args: ['.'] });
// Get the first window that opens
const window = await electronApp.firstWindow();
// Assert the window title
expect(await window.title()).toBe('My Electron App');
// Close the app after the test
await electronApp.close();
});Interacting & Asserting
Let's expand our test to click the button and verify the message changes. This demonstrates user interaction and making assertions.
Add to tests/e2e.spec.js:
const { test, expect, _electron: electron } = require('@playwright/test');
// ... (previous test)
test('button click changes message', async () => {
const electronApp = await electron.launch({ args: ['.'] });
const window = await electronApp.firstWindow();
// Locate the paragraph and button elements by their IDs
const messageParagraph = window.locator('#message');
const button = window.locator('#myButton');
// Wait for elements to be visible and check initial text
await messageParagraph.waitFor();
await expect(messageParagraph).toHaveText('Click the button below.');
// Click the button
await button.click();
// Assert that the text has changed
await expect(messageParagraph).toHaveText('Message Changed!');
await electronApp.close();
});E2E Workflow Check
Which of the following are key benefits of using End-to-End (E2E) testing for an Electron application?
Recap: E2E Workflows
In this lesson, we explored End-to-End (E2E) testing for Electron applications. We learned that E2E tests simulate full user interactions to ensure all parts of your app work together seamlessly.
- We identified Playwright as a powerful tool for Electron E2E.
- We set up a simple Electron app and wrote tests to launch it, interact with UI elements, and make assertions.
E2E testing is vital for building robust and reliable desktop applications!
คำถามที่พบบ่อย
บทเรียน “กระบวนการทดสอบตั้งแต่ต้นจนจบ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กระบวนการทดสอบตั้งแต่ต้นจนจบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Electron Desktop App Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กระบวนการทดสอบตั้งแต่ต้นจนจบ”
นำกลยุทธ์การทดสอบตั้งแต่ต้นจนจบไปใช้เพื่อตรวจสอบลำดับการใช้งานและฟังก์ชันการทำงานทั้งหมดของแอปพลิเคชันเดสก์ท็อป Electron คุณปฏิบัติ Electron Desktop App Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Electron Desktop App Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Electron Desktop App Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “กระบวนการทดสอบตั้งแต่ต้นจนจบ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Electron Desktop App Development นี้ได้ไหม
ได้ บทเรียน Electron Desktop App Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การดีบักกระบวนการหลักและกระบวนการแสดงผล
- การทดสอบหน่วยด้วย Spectron
- กระบวนการทดสอบตั้งแต่ต้นจนจบ
- การทดสอบ E2E สมัยใหม่ด้วย Playwright