用idea編寫appium用例

來源:互聯網
上載者:User

標籤: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();    }}
二. 建立自己的測試工程
  1. 步驟: New Project -- Maven -- 輸入Project name -- Project location -- Next -- Next

  2. 加入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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.