自动化测试

Appium测试IOS App的代码编写入门

2016-11-13  本文已影响200人  野草2060

    当学习一个新工具时,经常会对于如何用该工具写代码无所适从。对于学习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("//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/

上一篇下一篇

猜你喜欢

热点阅读