Showing posts with label appiumlibrary. Show all posts
Showing posts with label appiumlibrary. Show all posts

Automation Testing iOS App With Robotframework On Mac OS using Appiumlibrary

Chào các bạn!
Hôm nay 24/12, mình ở nhà trông con, nhân lúc con ngủ thì chúng ta lại gặp nhau trong loạt bài automation testing mobile application.
Bài trước chúng ta đã tìm hiểu cách Automation Testing Android With Robotframework, bài viết này chúng ta sẽ tìm hiểu cách Automation Testing iOS App With Robot framework using Appiumlibrary
Các công cụ cần thiết
  1. Brew
  2. Python
  3. Node JS
  4. Appium server
  5. Cấu hình Xcode lần đầu tiên cài đặt
  6. Cài đặt robot framework
  7. IDE hoặc editor để code robot framework
Bước 1: Cài Brew tại trang chủ
https://brew.sh/ 
Brew là gì? hiểu ngắn gọn nó là trình quản lý các gói phần mềm trên mac os, có thể cài các phần mềm được hỗ trợ = 1 dòng lệnh mà ko cần phải lên tìm, down và cài phần mềm đó.
Truy cập trang chủ, copy và paste dòng đó vào terminal để cài đặt brew.
Sau khi cài xong thì cài thêm các thư viện sau


brew install ideviceinstaller
brew install carthage
brew install libimobiledevice --HEAD
sudo gem install xcpretty    

Bước 2: Cài python 
Cài python qua brew bằng lệnh sau

brew install python

Bước 3: Cài Node JS
Cài node JS qua brew bằng lệnh sau

brew install node

Chú ý: khi cài node js, nếu cần phân quyền thì chạy lệnh sau

sudo chmod 755  /usr/local/lib/node_modules/

Bước 4: Cài Appium server
Cài thông qua node js bằng lệnh sau
4.1 : Để cài phiên bản mới nhất thì chạy lệnh này

npm install -g appium

- Để cài một phiên bản cụ thể thì chạy lệnh này

npm install -g appium@1.9.1

- Cài các thành phần khác liên quan:

npm install -g ios-deploy
npm install -g deviceconsole

4.2:  Nên cài cả Appium Desktop để có thêm lựa chọn selector element
Vào trang chủ, tải và cài đặt Appium desktop http://appium.io/


Bước 5: Cài đặt Xcode
Đăng ký tài khoản, tải và cài đặt Xcode từ trang chủ https://developer.apple.com/xcode/
Sau khi cài đặt xong, chạy Xcode lên và cấu hình như sau:
5.1 : Vào menu Xcode > Preferences > Locations
Mặc định là combobox Command Line Tools sẽ bị trống, click vào ô đó để hiển thị phiên bản hiện tại của xcode


Bước 6: Cài đặt robot framework và appium library
Chạy các lệnh sau để cài đặt robot FW và thư viện Appiumlibrary

pip install robotframework
pip install robotframework-appiumlibrary
pip install six

Bước 7:  Editor để code robotframework
Mình khuyến khích dùng atom, vì gợi ý keyword khá ok

7.1: down và cài đặt Atom tại trang chủ https://atom.io/
7.2: sau khi cài xong, thì chạy lần lượt các lệnh sau trên terminal

apm install language-robot-framework
apm install autocomplete-robot-framework
apm install hyperclick
apm install hyperclick-robot-framework

Bước 8: Viết test case đơn giản cho ios simulator
Mình sẽ làm 1 testcase đơn giản là tạo mới 1 contact trong ứng dụng contact trên ios simualtor
8.1: Bật ios simulator bằng cách mở Xcode lên, vào menu Xcode > Open Developer Tool > Simulator
8.2: Mở appium desktop để bắt element

cách để bắt element trên ios
8.3: Sau khi đã có các element cần thiết thì viết test case đơn giản


*** Settings ***
Library           AppiumLibrary
*** Variables ***
${REMOTE_URL}          http://127.0.0.1:4723/wd/hub
${PLATFORM_NAME}    iOS
${PLATFORM_VERSION}    12.0
${DEVICE_NAME}    iPhone 8 Plus
${BUNDLE_ID}      com.apple.MobileAddressBook
${AutomationName}    XCUITest

*** Keywords ***
Open App Contact
    Open Application    ${REMOTE_URL}    platformName=${PLATFORM_NAME}    platformVersion=${PLATFORM_VERSION}    deviceName=${DEVICE_NAME}      bundleId=${BUNDLE_ID}

Input Phone Number
    [Arguments]     ${phoneNumber}
    Input Text    //XCUIElementTypeTextField[@name="home"]     ${phoneNumber}

Add New Contact
    Wait Until Element Is Visible    //XCUIElementTypeNavigationBar/XCUIElementTypeButton[2]
    Click Element    //XCUIElementTypeNavigationBar/XCUIElementTypeButton[2]


Add New Information
    Wait Until Element Is Visible     //XCUIElementTypeTextField[@name="First name"]
    Input Text    //XCUIElementTypeTextField[@name="First name"]     nguyen    #input first name
    Input Text    //XCUIElementTypeTextField[@name="Last name"]    hai       #input last name
    Capture Page Screenshot
    Click Element    //XCUIElementTypeCell[@name="add phone"]       #click icon add phone
    Input Phone Number    123456679
    Capture Page Screenshot
    Click Element    //XCUIElementTypeButton[@name="Done"]         #click button Done

Verify add new contact success
    Sleep    3s
    Element Should Be Visible    //XCUIElementTypeNavigationBar[@name="CNContactView"]
    Capture Page Screenshot


*** Test Cases ***
TC1_add new contact
    Open App Contact
    Add New Contact
    Add New Information
    Verify add new contact success
    Close Application


code trên atom nhìn khá thích :D
Kết quả sau khi chạy TC, nó thêm vào contact nguyen hai như trong ảnh
Chú ý:
bundleId lấy ở đâu? Nếu là app mặc định của ios, thì google sẽ có :D ví dụ với ios 12 , còn nếu là app do công ty làm thì hỏi dev cho nhanh nhé 

Hẹn gặp các bạn ở bài tiếp theo!
Bài sau mình sẽ giới thiệu các cách bắt element trên ios và android app nhé!

Automation Testing Android With Robotframework On Mac OS using Appiumlibrary

Xin chào các bạn!
Hôm nay chúng ta cùng nhau tìm hiểu và cấu hình để automation test cho android  với robotframework sử dụng thư viện appiumlibrary
Note: bài viết này mình dùng mac os để cấu hình và chạy.
các thứ cần chuẩn bị như sau:

  1. Cài đặt brew
  2. Cài JDK
  3. Cài Android SDK
  4. Cài Robot framework
  5. Cài Appium
  6. Cài Android Emulator
  7. Editor or IDE code Robot Framework


Bước 1: Cài Brew tại trang chủ
https://brew.sh/
Brew là gì? hiểu ngắn gọn nó là trình quản lý các gói phần mềm trên mac os, có thể cài các phần mềm được hỗ trợ = 1 dòng lệnh mà ko cần phải lên tìm, down và cài phần mềm đó.
Truy cập trang chủ, copy và paste dòng đó vào terminal để cài đặt brew.

Bước 2: Cài JDK bằng terminal
chạy lệnh sau để cài JDK

brew cask install java

Bước 3: Cài android SDK tại Trang chủ https://developer.android.com/studio/ 
Sau khi cài xong, cần vào Config để lấy đường dẫn của android SDK


Tiếp đó thì cần set path cho android SDK và java như sau:
3.1: chạy lệnh sau = terminal


sudo open -a TextEdit  ~/.bash_profile

nhấn password của máy và enter để mở bash_profile bằng textedit trên mac os

thêm các dòng sau vào textedit :
chú ý: xxx là tên user của máy bạn.

export ANDROID_HOME=/Users/xxx/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH



export JAVA_HOME="$(/usr/libexec/java_home)"
export PATH=$PATH:$JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin

3.2: lưu text edit lại, sau đó chạy lệnh này để update path cho systems

source ~/.bash_profile

Bước 4: Cài robotframework và appiumlibrary
chạy các lệnh sau trên terminal để cài đặt
Note: các keyword và cách dùng của appiumlibrary http://serhatbolsu.github.io/robotframework-appiumlibrary/AppiumLibrary.html

pip install robotframework

pip install robotframework-appiumlibrary

pip install six


Bước 5: Cài Appium.
Các bạn xem lại bài viết tại đây để cài appium nhé



Bước 6: Cài máy ảo android
Các bạn có thể dùng máy ảo của android, nhưng mình thấy khá chậm, mình khuyên dùng genymotion nhé
Note: nhớ tạo account và login rồi tải về


Bước 7:  Editor để code robotframework
Mình khuyến khích dùng atom, vì gợi ý keyword khá ok

7.1: down và cài đặt Atom tại trang chủ  https://atom.io/
7.2: sau khi cài xong, thì chạy lần lượt các lệnh sau trên terminal


apm install language-robot-framework
apm install autocomplete-robot-framework
apm install hyperclick
apm install hyperclick-robot-framework


Bước 8: First testcase for Android using RobotFramework
Chúng ta cùng làm 1 testcase đơn giản cho caculator của android 8 như sau:



*** Settings ***
Library           AppiumLibrary

*** Variables ***
${REMOTE_URL}     http://127.0.0.1:4723/wd/hub    # URL to appium server
${Version Android}    8.0
${name_device}    Android Emulator
*** Keywords ***
Open App Calulator
    Open Application     ${REMOTE_URL}     automationName=UiAutomator2     platformName=Android    platformVersion=${Version Android}    deviceName=${name_device}    appPackage=com.android.calculator2          appActivity=com.android.calculator2.Calculator      noReset=true

*** Test Cases ***
TC1
    Open App Calulator
    Click Element    id=com.android.calculator2:id/digit_9
    Click Element    id=com.android.calculator2:id/op_add
    Click Element    id=com.android.calculator2:id/digit_6
    ${result}=    Get Text    id=com.android.calculator2:id/result
    Should Be Equal As Integers    ${result}    15

Và đây là kết quả sau khi chạy TC



Note:
  1. Trước khi chạy testcase thì cần chạy Appium server + Android Emulator
  2. Trên Mac thì chạy TC của Robotframework bằng terminal như sau:
python -m robot -d a demo.robot