? ? 當學習一個新工具時,經常會對于如何用該工具寫代碼無所適從。對于學習Appium而言,一種是學習appium中的sample代碼,一種是錄制代碼。當然,熟練后,一般都不會再錄制代碼來來實現自己想實現的自動化功能,而是直接參考api來編寫測試代碼。
Appium的sample代碼
? ? Appium作為一種流行的ios測試工具,為了讓使用者易于使用,提供了多種語言編寫測試代碼,如java、node、perl、php、python、ruby等。在我的電腦中,例子代碼的目錄是/Applications/Appium.app/Contents/Resources/node_modules/appium/sample-code/examples。里面有上面提到的多種語言編寫的示例代碼,也有被測的例子App。該用例測試的是一個示例用的計算器App。
SimpleTest.java中的兩個junit測試用例代碼如下:
private void populate() {
//populate text fields with two random number
List elems = driver.findElements(By.className("UIATextField"));
Random random =newRandom();
for (WebElement elem: elems) ?{
int rndNum = random.nextInt(MAXIMUM - MINIMUM +1) + MINIMUM;
elem.sendKeys(String.valueOf(rndNum));
values.add(rndNum);
}
}
@Test
public void testUIComputation() throws Exception {
// populate text fields with values
populate();
// trigger computation by using the button
WebElement button = driver.findElement(By.className("UIAButton"));
button.click();
// is sum equal ?
WebElement texts = driver.findElement(By.className("UIAStaticText"));
assertEquals(String.valueOf(values.get(0) + values.get(1)), texts.getText());
}
@Test
public void testBasic Alert() throws Exception {
driver.findElement(By.xpath("http://UIAButton[2]")).click();
Alert alert= driver.switchTo().alert();
//check if title of alert is correct
assertEquals("Cool title this alert is so cool.", alert.getText());
alert.accept();
}
? ? 在SimpleTest.java中,有十多個這樣的示例;在相同的目錄中,還有多個ios和android測試用例,需要學習的,可運行一下這些用例,也學習下其代碼。
使用Inspector錄制代碼
? ? 在Inspector可以點擊錄制后,進行操作,可以生成對應的代碼。Appium Inspector如下圖:
? ? 使用左下角的各個按鈕,即可完成點擊、滑動、搖動、填寫文本、確認或取消alert等操作,并給出示例腳本。
? ? Inspector中,每個按鈕的含義如下:
INSPECTOR WINDOW
?Show Invisible Filter: Elements which are not visible will be displayed in the DOM 3-column-view.
?Show Disabled Filter: Elements which are not enabled will be displayed in the DOM 3-column-view.
?Record Button: Opens the recording Panel and starts recording actions performed usingcontrols in the Appium Inspector.
?Refresh Button: Refreshes the DOM 3-column view and the screenshot.
?Screenshot Area: Displays the last screenshot taken for the app. You can click this area toselect elements in the DOM.
?Details Area: Displays details about the currently selected element.
ACTION PALETTE
The action pallete contains buttons that willtrigger actions on the device under test. You can try out actions here or enterthem to be recorded.
?Touch Section: Contains buttons to perform touch events like tapping and swiping.
?Text Section: Contains buttons to perform text events like typing and executingJavaScript.
?Alerts Section: Contains buttons to perform events on alerts and actionsheets.
RECORDER DRAWER
The recorder draw contains the code generatedby recorded actions you’ve performed while recording is enabled.
?Language Selection Dropdown: Changes the language your recorded actions are displayed in.
?Add Boilerplate Checkbox: Checking this block will display code used to setup the Selenium instancealong with code related to the actions you have recorded. Unchecking this boxwill only show only the code from the actions you have recorded.
?XPath Only Checkbox: Checking this will cause all element identifiers to be created usingXPath.
?Replay Button: Replays the actions that you have recorded.
?Undo Button: Deletes the last recorded action.
?Redo Button: Adds back the last undone action.
?Clear Button: Removes all recorded actions.
Appium的java API文檔地址
? ? 寫代碼,api文檔是重要的參考。Java的api文檔見:http://appium.github.io/java-client/。