自动化测试框架Appium的应用

2019-07-12 22:48发布

1. Architecture 2. Framework 3. How to write a new testcase

1. Architecture

  • TestNGLayer:  Appium client,helps in Script organization, parallel execution of scripts.
  • SeleniumGrid Layer: Appium server,helps in distributed execution and scheduling of test cases.
  • AppiumLayer: Tool to automate mobile application on Android and IOS platform.


2. Framework
The main work is to override setup(), test_xxx(), tearDown(), preCondition(), keywordStep(), postCondition().
  • TestCaseInputs - all data from user input such as user name, password, location etc.
  • TestCases - all data for the test, many properties delegate to TestNode.

Sequence Diagram

  • SuiteExecutor - The automation test entry.
  • initializeSuiteConstants() - Read Suite constant from SuiteConfig.json.
  • initializeInfrastructure() - Creating test result folder in /src/test/resources/Execution_folder/
  • getRequirementFile() - Read Suite constant from Requirement_file.txt.
  • requirementLookupTable - Restore the Requirement_file data into the variable requirementLookupTable.
  • setListeners() - Setup TestNG Listeners to callback when test begins and finishes.
  • setName, setIncludedGroups - Setup XmlTest that is your test case.
  • setTests - put all test cases into XmlSuite that is the test suite ready for running.

This diagram describes detailed process when TestNG is running.
  • When TestNG begins to run, MyTestCase will be created.
  • When MyTestCase creates, TestDataProvider will initialise TestCaseInputs.
  • Before MyTestCase setUp(), TestNGListenerClass will initialise TestCases.
  • After TestCaseInputs & TestCases are available, auto-test will run.



3. How to write a new test case

1. Configure SuiteConfig.json

This file restores global environment variables for the server. The file is located at /$(YOUR_PROJECT_ROOT)/SuiteConfig.json The properties highlight in red below need to be configured according to your server platform. {
"Framework_Version": "1.0", "Test_Suite_Properties": {  "Suite_Name": "H+_Test_Suite", "Suite_ID": "H_Plus_001", "Suite_Version": "1.0",  "Requirment_File_Name": "Requirement_file", "DevicePoolURL": "http://159.99.248.223:4723", "Thread_Pool_Size": 1,
"Android_Platform_Version": "4.4.2", "Android_Package_Name": "com.honeywell.hch.airtouch", "Android_Activity_Name": "com.honeywell.hch.airtouch.library.app.splash.StartActivity", "Android_Activity_To_Wait": "", "Local_APK_Path": "/Users/e573227/E/Automation/Tools/yingyongbao-debug.apk", "IOS_Platform_Version": "8.0", "Local_IPA_Path": "/Users/lyricsecuritymac2/Documents/Lyric/Lyric.app", "Local_APP_Path": "/Users/lyricsecuritymac2/Documents/Lyric/Lyric.app", "IOS_Bundle_ID": "com.honeywell.acs.lyric.enterprise", "HUB_URL": "http://159.99.248.223:4723/wd/hub",      // Ip address is local address of your computer as server, port comes from Appium server "Global_Test_TimeOut_In_Minutes": 45,   ... ... "TestNG_Listener_Classes": ["com.honeywell.commons.coreframework.TestNGListernerClass"]
}
}  

2. Configure Requirement_file.csv

This file restores test data and global variables for the test device. The file is located at /$(YOUR_PROJECT_ROOT)/Requirement_file.csv After editing, save the file as Requirement_file.txt. The program will read the txt file and parse the properties.   For example, if add a new test case LAECOSYS_ATC_1032_add_home, just add a new row in the cvs file. 5 properties below are necessary. Group name Android Medium Device  IOS Medium Device Environment Test Data LAECOSYS_ATC_1032_add_home Huawei MT7 Iphone 7 Stage {accountAcquisitionType:Specific,testData:[{UserID:'15800349135', Password:'111111 ',Location:[{Name:BLR,Devices:[Stat 3762]}]}]} Other properties such as Android Medium Emulator Required, IOS Medium Emulator Required, Emulators Local or SauceLabs also need to be configured so that the program will know which platform (device or cloud, emulator or real device, Android or IOS) need to be tested. Test Data is all data from user input with JSON format. If you want to add new data, need to change the code first.  (1).Add methods in InputTestDataParser.java (2).Define the Excel related variables in com.honeywell.commons.coreframework.GlobalVariables     for example,
public static final String EXCEL_TEST_DATA_LOGIN_TITLE = "Login";
public static final String EXCEL_TEST_DATA_USER_ID = "UserID";
public static final String EXCEL_TEST_DATA_PASSWORD = "Password";
 

3. Create a new test case class

Here is an example for adding a new test case LAECOSYS_ATC_1032_add_home.

3.1 Create a MobileTestCase subclass

/$(YOUR_PROJECT_ROOT)/src/test/java/com/honeywell/testcases/LAECOSYS_ATC_1032_add_home.java Override @Factory(), @BeforeMethod(), @Test(), @AfterMethod(). Use Keyword.execute() to run a test. 

3.2 Create a Keyword subclass 

/$(YOUR_PROJECT_ROOT)/src/test/java/com/honeywell/keywords/AddHome.java Override @BeforeKeyword(), @KeywordStep(), @AfterKeyword(). Use MobileUtils for all user gesture functions.

3.3 Create a ObjectiveDefinition JSON file

/$(YOUR_PROJECT_ROOT)/src/test/resources/objects_definition/ObjectDefinition/AddHomePage.json This file is to define IDs of View objects. The IDs can be found by the Tool Uiautomatorviewer. {
"MeIcon": {
"Android_Medium": {
"Locator_Type": "ID",
"Locator_Value": "ds_four_bottom_btn_id"
},
... ...
"IOS_Medium": {
"Locator_Type": "XPATH",
"Locator_Value": "//*[@name='login_username_textfield']"
},
... ...
}
}, ... ... } Also you need a JSON editor toolkit, such as http://www.kjson.com/jsoneditor/

3.4 Add the test case into the test group

/$(YOUR_PROJECT_ROOT)/src/test/java/com/honeywell/suiteExecutor/SuiteExecutor.java public static void main(String[] groups) {  groups=new String[]{"LAECOSYS_ATC_1032_add_home", ... ...};   }

3.5 Code style

Strongly recommend a good code style so that the code is easy to read and reuse. For example, write the code like below, the detailed process is encapsulated in private method. @KeywordStep  public boolean keywordSteps() {  if (goToMeScreen()) {    if (goToManageHomeScreen()) {       if (goToAddHomeScreen()) {         selectCityName();         setHomeName();         MobileUtils.hideKeyboard(mTestCase);          tapToConfirmButton();         if (hasAddHomeButton()) {            checkIfHasHomeName();  } private boolean goToMeScreen {    .... ... }  

4. Run the test case

Run /$(YOUR_PROJECT_ROOT)/src/test/java/com/honeywell/suiteExecutor/SuiteExecutor.java Appium server has been launched.