هذه هي الورقة التي ذكرناها في الدرس السابق: كل أوامر مكتبة QAcart مع مثال لكل أمر، وفي آخرها جدول بالـ Locators الخاصة بتطبيق الـ Todo.
لا تحفظ شيئاً. افتح هذه الصفحة وأنت تكتب الاختبار، وابحث عن الأمر الذي تحتاجه.
لتشغيل المتصفح نستخدم launch، ونمرّر له اسم المتصفح أو اسم جهاز الموبايل.
launch("CHROME")
launch("FIREFOX")
launch("SAFARI")
launch("iPhone14")
launch("GALAXY24")لإغلاق المتصفح نستخدم close.
close()لفتح أي موقع أو تطبيق موبايل نستخدم open، ونمرّر له رابط الصفحة أو اسم التطبيق.
open("https://todo.qacart.com")
open("Reminders")نخزّن عنصراً ونعطيه اسماً حتى نستخدمه لاحقاً في الاختبار.
element emailInput = find("emailInput")
element passwordInput = find("passwordInput")لتخزين أكثر من عنصر يشتركون بنفس الـ Locator.
elements products = find("product")
elements menuItems = find("item")
elements todos = find("todo")نعرف عدد العناصر باستخدام .count().
elements products = find("product")
products.count() // 5
// ونقدر نخزّن العدد في متغير
number numOfProducts = products.count() // 5نختار عنصراً واحداً من القائمة باستخدام .nth(). العدّ يبدأ من صفر.
elements products = find("product") // 5
products.nth(0) // أول منتج
products.nth(1) // المنتج الثاني
products.nth(4) // المنتج الخامس
products.nth(5) // خطأ!للنقر على عنصر نستخدم .click().
element loginButton = find("loginButton")
loginButton.click()للكتابة داخل عنصر نستخدم .type().
element emailInput = find("emailInput")
emailInput.type("hatem@example.com")للاختيار من قائمة منسدلة (Dropdown) نستخدم .select().
element countriesDropDown = find("countries")
countriesDropDown.select(0) // اختيار أول خيار
countriesDropDown.select("JO") // اختيار الأردننستخدم .check() و .uncheck() للتعامل مع الـ Checkboxes.
element acceptTermsCheckbox = find("terms")
acceptTermsCheckbox.check() // يحدّد الـ Checkbox
acceptTermsCheckbox.uncheck() // يلغي التحديدلتمرير الماوس فوق عنصر نستخدم .hover().
element languages = find("languages")
languages.hover() // تنفتح قائمة اللغاتنقرأ نص العنصر باستخدام .getText().
element firstTodo = find("todo").nth(0) // أول مهمة
firstTodo.getText() // Learn Automation!
// ونقدر نخزّن النص في string
string firstTodoText = firstTodo.getText() // Learn Automation!نتحقق إذا كان العنصر ظاهراً باستخدام .isVisible().
element welcomeMessage = find("welcomeMessage")
welcomeMessage.isVisible() // True أو False
// ونقدر نخزّن النتيجة في boolean
boolean isWelcomeVisible = welcomeMessage.isVisible() // Trueنتحقق إذا كان الـ Checkbox محدداً باستخدام .isChecked().
element acceptTermsCheckbox = find("terms")
acceptTermsCheckbox.isChecked() // True أو False
// ونقدر نخزّن النتيجة في boolean
boolean isTermsAccepted = acceptTermsCheckbox.isChecked() // Trueنتحقق إذا كان للعنصر لون خلفية معيّن باستخدام .hasBackgroundColor().
element firstTodo = find("todo").nth(0)
boolean isTodoCompleted = firstTodo.hasBackgroundColor("#E45312") // Trueننتظر حتى يظهر العنصر باستخدام .waitToBeVisible()، ونمرّر أقصى مدة انتظار بالملّي ثانية.
element firstTodo = find("todo").nth(0)
firstTodo.waitToBeVisible(5000)نستخدم .isEqual() للتأكد أن قيمتين متساويتان.
element firstTodo = find("todo").nth(0) // أول مهمة
string firstTodoText = firstTodo.getText() // Learn Automation!
assert(firstTodoText).isEqual("Learn Automation") // Pass
assert(firstTodoText).isEqual("Learn automation") // Failنستخدم .isNotEqual() للتأكد أن قيمتين غير متساويتين.
element firstTodo = find("todo").nth(0) // أول مهمة
string firstTodoText = firstTodo.getText() // Learn Automation!
assert(firstTodoText).isNotEqual("Learn Automation") // Fail
assert(firstTodoText).isNotEqual("Learn automation") // Passنستخدم .isTrue() للتأكد أن الـ boolean قيمته true، و .isFalse() للتأكد أن قيمته false.
element welcomeMessage = find("welcomeMessage")
boolean isWelcomeVisible = welcomeMessage.isVisible() // True
assert(isWelcomeVisible).isTrue() // PASS
assert(isWelcomeVisible).isFalse() // FAILنجمع كل الخطوات والـ Assertions داخل اختبار واحد له اسم.
test("first todo should be completed") {
launch("CHROME")
open("https://todo.qacart.com")
element emailInput = find("emailInput")
emailInput.type("hatem@example.com")
element passwordInput = find("passwordInput")
passwordInput.type("Test1234")
element welcomeMessage = find("welcome")
welcomeMessage.waitToBeVisible(5000)
string welcomeText = welcomeMessage.getText() // Good morning Hatem
assert(welcomeText).isEqual("Good morning Hatem")
element firstTodo = find("todo").nth(0)
boolean isFirstTodoCompleted = firstTodo.hasBackgroundColor("#E45312") // True
assert(isFirstTodoCompleted).isTrue() // PASS
close()
}بدل ما تفتح الـ Inspect كل مرة، هذه كل الـ Locators التي تحتاجها في التمارين.
| الصفحة | العنصر | الـ Locator |
|---|---|---|
| Login Page | Email Input | email-input |
| Password Input | password-input |
|
| Login Button | login-button |
|
| Signup Page | First Name Input | first-name-input |
| Last Name Input | last-name-input |
|
| Email Input | email-input |
|
| Password Input | password-input |
|
| Confirm Password Input | confirm-password-input |
|
| Signup Button | signup-button |
|
| Create new account | create-account-button |
|
| Tasks Page | Welcome Message | welcome-message |
| Todo Item | todo |
|
| Todo checkbox | todo-checkbox |
|
| Todo trash | todo-trash |
|
| Plus Button | plus-button |
|
| New Todo Page | New Todo Input | new-todo-input |
| Add New Todo Button | new-todo-button |