Lesson 4 / الدرس 4
Testing without a page / الاختبار بلا صفحة
Behind most screens is a service that answers questions directly. Talking to it removes the browser, the layout and the waiting from your test — and lets you ask things the interface will not let you ask.
خلف معظم الشاشات خدمةٌ تجيب عن الأسئلة مباشرة. ومخاطبتها تزيل من اختبارك المتصفحَ والتخطيطَ والانتظار — وتتيح لك أن تسأل ما لا تدعك الواجهة تسأله.
When you press Sign in, the page sends a request to a server and does something with the answer. You can send that request yourself. No browser, no clicking, no waiting for fonts — just the question and the answer, in a fraction of a second. Everything the engineering course taught you about HTTP is the working knowledge this needs.
What you gain by dropping the page
-
Speed. Hundreds of cases in the time one browser test takes to load a page once. السرعة. مئات الحالات في الزمن الذي يستغرقه اختبار متصفح واحد ليحمّل صفحة مرة.
-
Certainty about what failed. A wrong status code is the service being wrong. There is no layout, no timing and no font to blame. اليقين بما رسب. فرمز الحالة الخاطئ يعني أن الخدمة مخطئة. ولا تخطيط ولا توقيت ولا خط يُلام.
-
Questions the interface refuses. The page will not let you order -1 items or set a delivery date in 1970. The service will let you try — and what it does then is a real and important answer. أسئلة ترفضها الواجهة. فالصفحة لن تدعك تطلب -1 عنصرًا ولا تضبط تاريخ تسليم في 1970. أما الخدمة فستدعك تحاول — وما تفعله حينئذ جوابٌ حقيقي ومهم.
-
Independence from the front end. You can test the service the week before the screen that uses it is built. الاستقلال عن الواجهة الأمامية. فتستطيع اختبار الخدمة في الأسبوع السابق لبناء الشاشة التي تستخدمها.
What an API test looks at
| Check | Example |
|---|---|
| The status code | 200 for success, 401 for not signed in, 404 for missing, 422 for invalid |
| The body | The fields you expect, with the types you expect |
| What is NOT there | No password hash, no other user's data, no internal error text |
| The effect | After a POST, does a GET show the new thing? |
| The time | Did it answer within a reasonable limit? |
The third row is the one testers forget and the one that matters most. An endpoint that returns the right thing plus a field it should never expose has passed every check except the one that mattered.
// The same sign-in check as before, without a browser.
const res = await fetch('https://api.example.com/sign-in', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: 'sara@example.com', password: 'totallywrong' })
});
expect(res.status).toBe(401);
const body = await res.json();
expect(body.message).toBe('Email or password is incorrect');
expect(body.token).toBeUndefined(); // nothing was handed out
expect(JSON.stringify(body)).not.toContain('hash'); // and nothing leaked
Check yourself / اختبر نفسك
1. What can an API test ask that a browser test usually cannot?
The page filters what it will send. The service is where the rule must actually hold, and asking it directly is the only way to find out whether the limit lives in the markup or in the system.
2. Which check do testers most often forget on an API response?
Assertions naturally describe what should be present. An endpoint returning everything correct plus one field it should never expose passes every positive check while leaking — which is why absence is worth asserting explicitly.
3. Why is testing an API against production more dangerous than using the website?
Every guard rail you are used to — the confirmation dialog, the disabled button, the rate at which a human can click — lives in the page. Remove the page and a mistyped loop is a thousand real records.
Score / النتيجة: 0 / 3