appium 移动web样例脚本

简单介绍

这是一个使用appium操作移动端chrome浏览器的样例程序。

样例程序

脚本实现的功能是:打开浏览器-->跳转到百度首页-->输入appium-->点击搜索按钮-->如果第一条搜索结果的标题含有appium字符串,那么断言脚本运行成功。

需要注意的是,操作chrome浏览器的方式是:指明chrome安装包的位置及相应的package和activity。

package com.wwh.utils;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.*;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Created by wwh on 17/2/24.
 */
public class H5Test {
    private AndroidDriver<MobileElement> driver;

    @BeforeTest
    public void Demo() throws Exception {
// set up appium
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
//for native app set null, for web test please set chrome or firefox
        //capabilities.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
//simulator version 4.4
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
// if no need install don't add this
        File classpathRoot = new File("/Users/wwh/IdeaProjects");
        File appDir = new File(classpathRoot, "apps");
        File app = new File(appDir, "com.android.chrome.1702051940.apk");
//        capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());

//package name
//        capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.chrome.dev");
        capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.android.chrome");
// // support Chinese
        capabilities.setCapability("unicodeKeyboard", "True");
        capabilities.setCapability("resetKeyboard", "True");
// no need sign
//        capabilities.setCapability("noSign", "True");
//launcher activity
        capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,
                "org.chromium.chrome.browser.webapps.WebappLauncherActivity");
        String url = "http://localhost:4723/wd/hub";
        driver = new AndroidDriver<MobileElement>(new URL(url), capabilities);


    }

    @AfterTest
    public void Teardown(){
        driver.quit();
    }

    @Test
    public void runChromebrowser(){
        driver.get("http://www.baidu.com");//打开百度首页
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//android.widget.LinearLayout[1]/" +
                "android.widget.FrameLayout[1]/android.widget.FrameLayout[1]" +
                "/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]" +
                "/android.view.View[1]/android.widget.FrameLayout[1]/android.widget" +
                ".FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/" +
                "android.view.View[5]/android.widget.EditText[1]")).sendKeys("appium");//输入appium
        driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]" +
                "/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]" +
                "/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/" +
                "android.webkit.WebView[1]/android.view.View[1]/android.view.View[5]/android.view.View[1]" +
                "/android.widget.Button[1]")).click();//点击搜索按钮
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }//因为页面加载完成前,下面的el的xpath会指向另一个页面元素,所以加了2秒等待。
        WebElement el = driver.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[3]/android.view.View[2]"));

        String desc = el.getAttribute("contentDescription");
        System.out.println("desc: "+desc);
        Assert.assertTrue(desc.contains("appium"));//若搜索结果包含appium,则判定成功。
    }




}

 

posted @ 2017-02-27 16:57  超级宝宝11  阅读(287)  评论(0编辑  收藏  举报