码迷,mamicode.com
首页 > 移动开发 > 详细

java-appium-527操作

时间:2018-10-04 19:29:33      阅读:622      评论:0      收藏:0      [点我收藏+]

标签:-o   iss   not   sdi   linu   exce   cte   warning   which   

https://www.w3.org/TR/webdriver/#list-of-endpoints

针对元素进行操作

  • click()
  • sendKeys()
  • clear() 清空
  • findElement() 二次定位,用的少
  • getAtttibute() 获取属性
  • getLocation()获取X Y
  • 是否展示isDisplayed()
public class XueqiuDemo {

    private AndroidDriver driver;
    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "android");
        desiredCapabilities.setCapability("deviceName", "domo");
        desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");
        desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");
        URL remoteUrl = new URL("http://localhost:4723/wd/hub");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    }

    @Test
    public void sampleTest() throws InterruptedException {
        exist_do("//*[@text=\"允许\"]").click();
        Thread.sleep(2000);
        locate("com.xueqiu.android:id/user_profile_icon").click();
        Thread.sleep(2000);
        locate("com.xueqiu.android:id/tv_login").click();
        Thread.sleep(2000);
        locate("com.xueqiu.android:id/tv_login_by_phone_or_others").click();
        Thread.sleep(2000);
        System.out.println("获取某个属性getAttribute:"+locate("com.xueqiu.android:id/register_phone_number").getAttribute("text"));
        System.out.println("获取坐标getLocation():"+locate("com.xueqiu.android:id/register_phone_number").getLocation());
        System.out.println("获取相对位置getRect():"+locate("com.xueqiu.android:id/register_phone_number").getRect());
        System.out.println("获取长度和高度getSize():"+locate("com.xueqiu.android:id/register_phone_number").getSize());
        System.out.println("获取控件的className getTagName():"+locate("com.xueqiu.android:id/register_phone_number").getTagName());
        System.out.println("获取text属性getText()"+locate("com.xueqiu.android:id/register_phone_number").getText());
        System.out.println("是否展示isDisplayed()"+locate("com.xueqiu.android:id/register_phone_number").isDisplayed());
        System.out.println("toString():"+locate("com.xueqiu.android:id/register_phone_number").toString());
        System.out.println("是否可用isEnabled():"+locate("com.xueqiu.android:id/register_phone_number").isEnabled());
        System.out.println("是否选中isSelected():"+locate("com.xueqiu.android:id/register_phone_number").isSelected());
        System.out.println("hashCode():"+locate("com.xueqiu.android:id/register_phone_number").hashCode());
        System.out.println("getClass()"+locate("com.xueqiu.android:id/register_phone_number").getClass());
        //System.out.println(""+locate("com.xueqiu.android:id/register_phone_number").);
        locate("com.xueqiu.android:id/register_phone_number").sendKeys("123456789");
        //寻找子元素
        locate("//android.widget.LinearLayout[@resource-id=\"com.xueqiu.android:id/register_module\"]").findElement(By.id("com.xueqiu.android:id/register_phone_number")).clear();

        driver.pressKeyCode(4);
    }
    public WebElement exist_do(String el){
        if (locate(el).isDisplayed()){
            return locate(el);
        }
        else{
            return  null;
        }
    }
    public WebElement locate(String locate){
        if (locate.matches("\\/\\/.*")){
            return  driver.findElementByXPath(locate);
        }else{
            return driver.findElementById(locate);
        }
    }
    @After
    public void tearDown() {
        driver.quit();
    }
}

执行结果:

objc[16328]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x10bdb24c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10ddf14e0). One of the two will be used. Which one is undefined.
十月 04, 2018 6:33:49 下午 io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0
信息: Detected dialect: W3C
获取某个属性getAttribute:请输入手机号
获取坐标getLocation():(198, 655)
获取相对位置getRect():org.openqa.selenium.Rectangle@d0006a20
获取长度和高度getSize():(849, 121)
获取控件的className getTagName():android.widget.EditText
获取text属性getText()请输入手机号
是否展示isDisplayed()true
toString():[[io.appium.java_client.android.AndroidDriver, Capabilities: {appActivity=.view.WelcomeActivityAlias, appPackage=com.xueqiu.android, databaseEnabled=false, desired={platformName=android, appActivity=.view.WelcomeActivityAlias, appPackage=com.xueqiu.android, deviceName=domo}, deviceManufacturer=Xiaomi, deviceModel=Redmi Note 5, deviceName=406e8f3, deviceScreenSize=1080x2160, deviceUDID=406e8f3, javascriptEnabled=true, locationContextEnabled=false, networkConnectionEnabled=true, platform=LINUX, platformName=Android, platformVersion=8.1.0, takesScreenshot=true, warnings={}, webStorageEnabled=false}] -> id: com.xueqiu.android:id/register_phone_number]
是否可用isEnabled():true
是否选中isSelected():false
hashCode():1573
getClass()class io.appium.java_client.android.AndroidElement

Process finished with exit code 0

java-appium-527操作

标签:-o   iss   not   sdi   linu   exce   cte   warning   which   

原文地址:https://www.cnblogs.com/csj2018/p/9742803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!