編輯器 : TestNG
- 創立 Java Project
- 點擊 Project右鍵 configure
- 點選 Convert to Maven Project
- 出現 Create new POM 不需做額外設定,直接點 Finish
- 在 Java Project 下新增 apps ( floder )
- 在上一步生成的檔案下新增 ContactManager ( floder )
- 在 ContactManager floder 放置您要測試的App.apk
在 pom.xml 新增
.
.
.
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_junit</artifactId>
<version>2.1.20</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
.
.
.
新增完畢後,點選 pom.xml 右鍵( Run As -> Maven install )
--------------------------------------
在 Src 下,新增 AndroidContactsTest.java
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
public class AndroidContactsTest {
private WebDriver driver;
@Before
public void setUp() throws Exception {
// 不用更改(因Apk直接放在路徑下)
File classpathRoot = new File(System.getProperty("user.dir"));
// 預設路徑
File appDir = new File(classpathRoot, "/apps/ContactManager");
// 測試App的完整名稱
File app = new File(appDir, "adapterCallback.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("device", "Android");
capabilities.setCapability("platformName", "Android");
// 模擬器
capabilities.setCapability("deviceName","Android Emulator");
// 實體手機
capabilities.setCapability("deviceName","Android");
// 手機/模擬器版本
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
// Laurch "package"
capabilities.setCapability("appPackage", "com.example.adaptercallback");
// Laurch "Activity"
capabilities.setCapability("appActivity", ".MainActivity");
// Appium IP Address : Port *Appium主機位置
driver = new SwipeableWebDriver(new URL("http://192.168.0.105:4723/wd/hub")
, capabilities);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void addContact(){
// 腳本
WebElement btn = driver.findElement(By.id("button"));
btn.click();
WebElement editText = driver.findElement(By.id("editText_0"));
editText.sendKeys("Hello");
}
public class SwipeableWebDriver extends RemoteWebDriver implements
HasTouchScreen {
private RemoteTouchScreen touch;
public SwipeableWebDriver(URL remoteAddress,
Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}
@Override
public TouchScreen getTouch() {
return touch;
}
}
}
以下是腳本功能指令 :
輸入文字 :
WebElement btn_00 = driver.findElement(By.id("edit_WriteString"));
btn_00.sendKeys(new String[] { "sample567977" });
點擊按鈕 :
WebElement btn_01 = driver.findElement(By.id("button_Click"));
btn_01.click();
隱藏鍵盤 :
driver.hideKeyboard();
實體按鍵 :
driver.pressKeyCode(4);
KEYCODE_HOME is 3
KEYCODE_BACK is 4
KEYCODE_MENU is 82
利用 JUnit Test 執行
( PS : 一定要開 Appium )
參考資料 : TestNG使用Eclipse建立Test Case
參考資料 : How to click on home back and recent button in android?
參考資料 : Appium手機自動化測試模擬手勢的基本操作
參考資料 : Appium手機自動化測試從頭學 –Windows/Android環境安裝篇
參考資訊 : Android自动化工具Appium的使用
參考資訊 : maven学习教程:What、How、Why
參考資訊 : The following desired capabilities are required, but were not provided: platformName, deviceName
沒有留言:
張貼留言