با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
آزمایش تعاملات کاربر کمک میکند تا اطمینان حاصل شود که کاربران هنگام تعامل با برنامه شما با نتایج غیرمنتظره مواجه نمیشوند یا تجربه ضعیفی ندارند. اگر نیاز به بررسی درستی عملکرد رابط کاربری برنامه خود دارید، باید عادت به ایجاد تست های رابط کاربری (UI) کنید.
یکی از روشهای تست رابط کاربری این است که آزمایشگر انسانی مجموعهای از عملیات کاربر را روی برنامه مورد نظر انجام دهد و بررسی کند که آیا درست عمل میکند. با این حال، این رویکرد دستی می تواند زمان بر و مستعد خطا باشد. یک رویکرد کارآمدتر این است که آزمایشهای رابط کاربری خود را بنویسید تا اقدامات کاربر به روشی خودکار انجام شود. رویکرد خودکار به شما امکان می دهد تست های خود را سریع و قابل اعتماد به روشی تکرار شونده اجرا کنید.
آزمایشهای رابط کاربری یک برنامه (یا بخشی از آن) را راهاندازی میکنند، سپس تعاملات کاربر را شبیهسازی میکنند و در نهایت بررسی میکنند که آیا برنامه واکنش مناسبی نشان داده است. آنها تستهای یکپارچهسازی هستند که میتوانند از تأیید رفتار یک مؤلفه کوچک تا یک آزمایش ناوبری بزرگ که یک جریان کاربر کامل را طی میکند، متغیر باشد. آنها برای بررسی رگرسیون ها و تأیید سازگاری با سطوح مختلف API و دستگاه های فیزیکی مفید هستند.
تست های UI را اجرا کنید
برای اجرای تستهای UI ابزاردار با استفاده از Android Studio، کد آزمایشی خود را در یک پوشه تست Android جداگانه پیادهسازی میکنید - src/androidTest/java . پلاگین Android Gradle یک برنامه آزمایشی را بر اساس کد آزمایشی شما ایجاد می کند، سپس برنامه آزمایشی را در همان دستگاهی که برنامه مورد نظر قرار دارد بارگیری می کند. در کد آزمایشی خود، میتوانید از چارچوبهای تست رابط کاربری برای شبیهسازی تعاملات کاربر در برنامه هدف استفاده کنید تا کارهای آزمایشی را که سناریوهای استفاده خاصی را پوشش میدهند، انجام دهید.
همچنین میتوانید از Robolectric برای اجرای آزمایشهای UI در JVM استفاده کنید.
معماری و راه اندازی تست
معماری برنامه شما باید به تستها اجازه دهد بخشهایی از آن را برای آزمایش دوتایی جایگزین کنند و باید از کتابخانههایی استفاده کنید که ابزارهای کمکی را برای کمک به آزمایش ارائه میدهند. برای مثال، میتوانید یک ماژول مخزن داده را با یک نسخه درون حافظه آن جایگزین کنید که دادههای جعلی و قطعی را برای آزمایش فراهم میکند.
شکل 3 : آزمایش یک UI با جایگزینی وابستگی های آن با جعلی.
رویکرد توصیه شده برای جایگزینی وابستگی ها تزریق وابستگی است. شما می توانید سیستم خود را به صورت دستی ایجاد کنید، اما توصیه می کنیم از یک چارچوب DI مانند Hilt برای این کار استفاده کنید.
چرا UI ها را به صورت خودکار آزمایش کنیم؟
یک برنامه Android میتواند هزاران دستگاه مختلف را در بسیاری از سطوح API و فاکتورهای شکل مورد هدف قرار دهد، و سطح بالای سفارشیسازی که سیستمعامل برای کاربر به ارمغان میآورد به این معنی است که برنامه شما ممکن است نادرست رندر شود یا حتی در برخی دستگاهها خراب شود.
تست UI به شما امکان میدهد تست سازگاری انجام دهید و رفتار یک برنامه را در زمینههای مختلف تأیید کنید. ممکن است بخواهید آزمایشهای رابط کاربری خود را روی دستگاههایی اجرا کنید که به روشهای زیر متفاوت هستند:
سطح API : 21، 25، و 30.
محل : انگلیسی، عربی، و چینی.
جهت : پرتره، منظره.
علاوه بر این، برنامه ها باید رفتار فراتر از تلفن ها را بررسی کنند. باید روی تبلت ها، تاشوها و سایر دستگاه ها تست کنید. درباره آزمایش اندازه های مختلف صفحه نمایش بیشتر بخوانید.
انواع تست های رابط کاربری
این بخش دو نوع تست UI را پوشش می دهد:
آزمونهای رفتار سلسله مراتب UI را تجزیه و تحلیل میکنند تا در مورد ویژگیهای عناصر UI اظهارنظر کنند.
تست های اسکرین شات از یک رابط کاربری اسکرین شات می گیرند و آن ها را با تصاویر تایید شده قبلی مقایسه می کنند.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]