تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يساعد اختبار تفاعلات المستخدمين في ضمان عدم حصول المستخدمين على نتائج غير متوقّعة
أو تجربة سيئة عند التفاعل مع تطبيقك. يجب أن تحرص على
إنشاء اختبارات لواجهة المستخدم إذا كنت بحاجة إلى التأكّد من أنّ
واجهة المستخدم في تطبيقك تعمل بشكل صحيح.
من بين طرق اختبار واجهة المستخدم أن يُجري أحد المختبِرين مجموعة من عمليات المستخدِم في التطبيق المستهدَف للتأكّد من أنّه يعمل بشكل صحيح. ومع ذلك،
قد تستغرق هذه الطريقة اليدوية وقتًا طويلاً وتزيد من احتمالية حدوث أخطاء. من الطرق الأكثر فعالية لكتابة اختبارات واجهة المستخدم هي تنفيذ إجراءات المستخدمين بطريقة
مبرمَجة. يتيح لك النهج المبرمَج إجراء اختباراتك بسرعة وبطريقة موثوقة وقابلة للتكرار.
تعمل اختبارات واجهة المستخدم على تشغيل تطبيق (أو جزء منه)، ثم تحاكي تفاعلات المستخدمين، ثم تتحقق من أنّ التطبيق قد استجاب بشكل مناسب. وهي اختبارات دمج
يمكن أن تتراوح بين التحقّق من سلوك مكوّن صغير واختبار تنقّل
كبير ينتقل عبر مسار مستخدم كامل. وهي مفيدة للتحقّق من
التراجعات والتأكّد من التوافق مع مستويات واجهة برمجة التطبيقات المختلفة والأجهزة
المختلفة.
إجراء اختبارات واجهة المستخدم
لتشغيل اختبارات واجهة المستخدم المزوّدة بأدوات قياس باستخدام "استوديو Android"، عليك تنفيذ
رمز الاختبار في مجلد اختبار Android منفصل - src/androidTest/java. ينشئ مكوّن Android Gradle الإضافي تطبيقًا تجريبيًا استنادًا إلى رمز الاختبار، ثم يحمِّل التطبيق التجريبي على الجهاز نفسه الذي يعمل عليه التطبيق المستهدَف. في رمز الاختبار، يمكنك استخدام إطارات عمل اختبار واجهة المستخدم لمحاكاة تفاعلات المستخدمين على التطبيق المستهدَف، وذلك لتنفيذ مهام الاختبار التي تغطي سيناريوهات استخدام معيّنة.
يمكنك أيضًا استخدام Robolectric لتشغيل اختبارات واجهة المستخدم على آلة Java الافتراضية.
التصميم وإعداد الاختبار
يجب أن تسمح بنية تطبيقك للاختبارات باستبدال أجزاء منه لاختبار
النُسخ المطابقة، ويجب استخدام مكتبات توفّر أدوات مساعدة في
الاختبار. على سبيل المثال، يمكنك استبدال وحدة مستودع بيانات بنسخة
منها في الذاكرة توفّر بيانات زائفة ومحدّدة للاختبار.
الشكل 3: اختبار واجهة مستخدم من خلال استبدال العناصر التي تعتمد عليها بعناصر زائفة
إنّ الأسلوب المُقترَح لاستبدال التبعيات هو إدخال الاعتمادية. يمكنك
إنشاء نظامك الخاص يدويًا، ولكننا ننصحك باستخدام إطار عمل DI
مثل Hilt لهذا الغرض.
لماذا يجب اختبار واجهات المستخدم تلقائيًا؟
يمكن أن يستهدف تطبيق Android آلاف الأجهزة المختلفة على مستوى العديد من مستويات واجهة برمجة التطبيقات
وعوامل الشكل، ويؤدي المستوى العالي من التخصيص الذي يوفّره نظام التشغيل للمستخدمين
إلى احتمال ظهور تطبيقك بشكل غير صحيح أو حتى تعطُّله على بعض الأجهزة.
يتيح لك اختبار واجهة المستخدم إجراء اختبار التوافق، ما يؤكّد سلوك التطبيق
في سياقات مختلفة. قد تحتاج إلى إجراء اختبارات واجهة المستخدم على الأجهزة التي تختلف
بالطُرق التالية:
مستوى واجهة برمجة التطبيقات: 21 و25 و30
اللغة: الإنجليزية والعربية والصينية
الاتجاه: عمودي وأفقي
بالإضافة إلى ذلك، يجب أن تتحقّق التطبيقات من السلوك على الأجهزة الأخرى غير الهواتف. يجب إجراء الاختبار على
أجهزة الكمبيوتر اللوحي والأجهزة القابلة للطي والأجهزة الأخرى. اطّلِع على مزيد من المعلومات عن اختبار أحجام شاشة مختلفة.
أنواع اختبارات واجهة المستخدم
يتناول هذا القسم نوعَين من اختبارات واجهة المستخدم:
تُحلِّل اختبارات السلوك التسلسل الهرمي لواجهة المستخدم لإجراء تأكيدات بشأن
خصائص عناصر واجهة المستخدم.
تلتقط اختبارات لقطات الشاشة لقطات شاشة لواجهة المستخدم وتقارنها
بالصور التي تمت الموافقة عليها سابقًا.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Automate UI tests\n\nTesting user interactions helps ensure users don't encounter unexpected results\nor have a poor experience when interacting with your app. You should get into\nthe habit of creating user interface (UI) tests if you need to verify that the\nUI of your app is functioning correctly.\n\nOne approach to UI testing is to have a human tester perform a set of user\noperations on the target app and verify that it is behaving correctly. However,\nthis manual approach can be time-consuming and error-prone. A more efficient\napproach is to write your UI tests such that user actions are performed in an\nautomated way. The automated approach lets you run your tests quickly and\nreliably in a repeatable manner.\n\nUI tests launch an app (or part of it), then simulate user interactions, and\nfinally check that the app reacted appropriately. They are integration tests\nthat can range from verifying the behavior of a small component to a large\nnavigation test that traverses a whole user flow. They are useful to check for\nregressions and to verify compatibility with different API levels and physical\ndevices.\n\nRun UI tests\n------------\n\n- To run [instrumented](/training/testing/instrumented-tests) UI tests using Android Studio, you implement your test code in a separate Android test folder - `src/androidTest/java`. The [Android Gradle Plugin](/studio/releases/gradle-plugin) builds a test app based on your test code, then loads the test app on the same device as the target app. In your test code, you can use UI testing frameworks to simulate user interactions on the target app, in order to perform testing tasks that cover specific usage scenarios.\n- You can also use [Robolectric](/training/testing/local-tests/robolectric#ui-testing) to run UI tests on the JVM.\n\nArchitecture and test setup\n---------------------------\n\nThe architecture of your app should let tests replace parts of it for [testing\ndoubles](/training/testing/fundamentals/test-doubles) and you should use libraries that provide utilities to help with\ntesting. For example, you can replace a data repository module with an in-memory\nversion of it that provides fake, deterministic data to the test.\n\n\u003cbr /\u003e\n\n**Figure 3**: Testing a UI by replacing its dependencies with fakes.\n\n\u003cbr /\u003e\n\nThe recommended approach to replace dependencies is dependency injection. You\ncan create your own system [manually](/training/dependency-injection/manual) but we recommend using a DI framework\nlike [Hilt](/training/dependency-injection/hilt-android) for this.\n\nWhy test UIs automatically?\n---------------------------\n\nAn Android app can target thousands of different devices across many API levels\nand form factors, and the high level of customization that the OS brings to the\nuser means your app could be rendered incorrectly or even crash on some devices.\n\nUI testing lets you do *compatibility testing*, verifying the behavior of an app\nin different contexts. You might want to run your UI tests on devices that vary\nin the following ways:\n\n- **API level**: 21, 25, and 30.\n- **Locale**: English, Arabic, and Chinese.\n- **Orientation**: Portrait, landscape.\n\nMoreover, apps should check the behavior beyond phones. You should test on\ntablets, foldables, and other devices. Read more about [testing different screen\nsizes](/training/testing/different-screens).\n\nTypes of UI Tests\n-----------------\n\nThis section covers two types of UI tests:\n\n- [Behavior tests](/training/testing/ui-tests/behavior) analyze the UI hierarchy to make assertions on the properties of the UI elements.\n- [Screenshot tests](/training/testing/ui-tests/screenshot) take screenshots of a UI and compares them with a previously-approved images."]]