Cheatsheet

مقال مجاني 5 دقيقة

مقدمة

هذه هي الورقة التي ذكرناها في الدرس السابق: كل أوامر مكتبة QAcart مع مثال لكل أمر، وفي آخرها جدول بالـ Locators الخاصة بتطبيق الـ Todo.

لا تحفظ شيئاً. افتح هذه الصفحة وأنت تكتب الاختبار، وابحث عن الأمر الذي تحتاجه.

نصيحة
احفظ رابط هذا الدرس. سترجع له في كل تمرين قادم.

Launch

لتشغيل المتصفح نستخدم launch، ونمرّر له اسم المتصفح أو اسم جهاز الموبايل.

launch("CHROME")
launch("FIREFOX")
launch("SAFARI")
launch("iPhone14")
launch("GALAXY24")

Close

لإغلاق المتصفح نستخدم close.

close()

Open

لفتح أي موقع أو تطبيق موبايل نستخدم open، ونمرّر له رابط الصفحة أو اسم التطبيق.

open("https://todo.qacart.com")
open("Reminders")

Element

نخزّن عنصراً ونعطيه اسماً حتى نستخدمه لاحقاً في الاختبار.

element emailInput = find("emailInput")
element passwordInput = find("passwordInput")

Elements

لتخزين أكثر من عنصر يشتركون بنفس الـ Locator.

elements products = find("product")
elements menuItems = find("item")
elements todos = find("todo")

Count

نعرف عدد العناصر باستخدام .count().

elements products = find("product")
products.count() // 5
// ونقدر نخزّن العدد في متغير
number numOfProducts = products.count() // 5

Nth

نختار عنصراً واحداً من القائمة باستخدام .nth(). العدّ يبدأ من صفر.

elements products = find("product") // 5
products.nth(0) // أول منتج
products.nth(1) // المنتج الثاني
products.nth(4) // المنتج الخامس
products.nth(5) // خطأ!

Click

للنقر على عنصر نستخدم .click().

element loginButton = find("loginButton")
loginButton.click()

Type

للكتابة داخل عنصر نستخدم .type().

element emailInput = find("emailInput")
emailInput.type("hatem@example.com")

Select

للاختيار من قائمة منسدلة (Dropdown) نستخدم .select().

element countriesDropDown = find("countries")
countriesDropDown.select(0)    // اختيار أول خيار
countriesDropDown.select("JO") // اختيار الأردن

Check و Uncheck

نستخدم .check() و .uncheck() للتعامل مع الـ Checkboxes.

element acceptTermsCheckbox = find("terms")
acceptTermsCheckbox.check()   // يحدّد الـ Checkbox
acceptTermsCheckbox.uncheck() // يلغي التحديد

Hover

لتمرير الماوس فوق عنصر نستخدم .hover().

element languages = find("languages")
languages.hover() // تنفتح قائمة اللغات

Get Text

نقرأ نص العنصر باستخدام .getText().

element firstTodo = find("todo").nth(0) // أول مهمة
firstTodo.getText() // Learn Automation!
// ونقدر نخزّن النص في string
string firstTodoText = firstTodo.getText() // Learn Automation!

Is Visible

نتحقق إذا كان العنصر ظاهراً باستخدام .isVisible().

element welcomeMessage = find("welcomeMessage")
welcomeMessage.isVisible() // True أو False
// ونقدر نخزّن النتيجة في boolean
boolean isWelcomeVisible = welcomeMessage.isVisible() // True

Is Checked

نتحقق إذا كان الـ Checkbox محدداً باستخدام .isChecked().

element acceptTermsCheckbox = find("terms")
acceptTermsCheckbox.isChecked() // True أو False
// ونقدر نخزّن النتيجة في boolean
boolean isTermsAccepted = acceptTermsCheckbox.isChecked() // True

Has Background Color

نتحقق إذا كان للعنصر لون خلفية معيّن باستخدام .hasBackgroundColor().

element firstTodo = find("todo").nth(0)
boolean isTodoCompleted = firstTodo.hasBackgroundColor("#E45312") // True

Wait To Be Visible

ننتظر حتى يظهر العنصر باستخدام .waitToBeVisible()، ونمرّر أقصى مدة انتظار بالملّي ثانية.

element firstTodo = find("todo").nth(0)
firstTodo.waitToBeVisible(5000)

Assert Equal

نستخدم .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

Assert Not Equal

نستخدم .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

Is True و Is False

نستخدم .isTrue() للتأكد أن الـ boolean قيمته true، و .isFalse() للتأكد أن قيمته false.

element welcomeMessage = find("welcomeMessage")
boolean isWelcomeVisible = welcomeMessage.isVisible() // True
assert(isWelcomeVisible).isTrue()  // PASS
assert(isWelcomeVisible).isFalse() // FAIL

Test

نجمع كل الخطوات والـ 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()
}

Locators تطبيق الـ Todo

بدل ما تفتح الـ 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