標籤:des android c style class blog
1. 安裝ContactManagers.spk:
路徑apps/ContactManager/ContactManagers.spk
2. 開啟用例到IntelliJ:
Open -- 選中appium/sample-code/examples/java/junit/pom.xml開啟
3. 環境配置:
如果依賴的jar包沒有載入進來,可能需要配置maven的路徑
4. 運行測試案例:
右點選中AndroidContactsTest — Run‘AndroidContactsTest‘
用例欣賞:
package com.saucelabs.appium;import io.appium.java_client.AppiumDriver;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.remote.CapabilityType;import org.openqa.selenium.remote.DesiredCapabilities;import java.io.File;import java.net.URL;import java.util.List;public class AndroidContactsTest { private AppiumDriver driver; @Before public void setUp() throws Exception { // set up appium File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "../../../apps/ContactManager"); File app = new File(appDir, "ContactManager.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("device","Android"); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability(CapabilityType.VERSION, "4.4"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("app-package", "com.example.android.contactmanager"); capabilities.setCapability("app-activity", ".ContactManager"); driver = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void addContact(){ WebElement el = driver.findElement(By.name("Add Contact")); el.click(); List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText"); textFieldsList.get(0).sendKeys("Some Name"); textFieldsList.get(2).sendKeys("[email protected]"); //driver.swipe(100, 500, 100, 100, 2); driver.findElementByName("Save").click(); }}
二. 建立自己的測試工程
步驟: New Project -- Maven -- 輸入Project name -- Project location -- Next -- Next
加入pom依賴
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <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>1.0.2</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> <scope>test</scope> </dependency> <!-- Includes the Sauce JUnit helper libraries --> <dependency> <groupId>com.saucelabs</groupId> <artifactId>sauce_junit</artifactId> <version>1.0.18</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>saucelabs-repository</id> <url>https://repository-saucelabs.forge.cloudbees.com/release</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
用例開始
package com.wirelessqa.android;import io.appium.java_client.AppiumDriver;import org.junit.After;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.*;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.remote.CapabilityType;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;import java.io.File;import java.net.URL;import java.util.concurrent.TimeUnit;/** * Created by bixiaopeng on 14-5-13. */public class appium { private AppiumDriver driver; @Before public void setUp() throws Exception { File classpathroot = new File(System.getProperty("user.dir")); File appDir = new File(classpathroot, "apk"); File app = new File(appDir, "xiamimusic.apk"); System.out.print(app.getAbsolutePath()); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability(CapabilityType.VERSION, "4.4"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("app-package", "fm.xiami.main"); capabilities.setCapability("app-activity", "fm.xiami.bmamba.activity.StartActivity"); driver = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities); } @Test public void login() throws Exception { //判斷某個元素是否顯示 if (driver.findElement(By.name("是否建立案頭捷徑")).isDisplayed()) { driver.findElement(By.name("確定")).click(); } driver.findElement(By.name("我的音樂")).click(); driver.findElementByName("點擊頭像登入").click(); driver.findElementByName("蝦米賬戶登入").click(); //輸入字元 driver.findElementByName("輸入郵箱地址").sendKeys("********"); //通過id尋找 driver.findElement(By.id("fm.xiami.main:id/edit_password")).sendKeys("****"); driver.findElementById("fm.xiami.main:id/btn_login").click(); String userName = driver.findElementById("fm.xiami.main:id/user_name").getText(); //斷言 assertEquals(userName, "老畢"); } @After public void tearDown() throws Exception { driver.quit(); }}
注意JDK版本的選擇
Open Module Setting -- Language level: 5.0
公眾帳號: wirelessqa
關於
作者: 畢小朋 | 老 畢 郵箱: [email protected]
微博: @WirelessQA 部落格: http://blog.csdn.net/wirelessqa