Appium [已解决]xPath 在我的环境里面一次都没有运行成功过。请大侠们指点!多谢

nearly · 2014年05月06日 · 最后由 nearly 回复于 2014年05月07日 · 1244 次阅读

这里是下载的例子,button 能用 byname 找到

uiautomatorviewer 查看此 button 的属性

driver.findElements(By.xpath("//button")).get(0).click();报错

XPath 在我的环境里面一次都没有运行成功过。
请大侠们指点!多谢

共收到 14 条回复 时间 点赞

是不是我的设备还是环境版本有问题,XPath 没有一个成功的。怎么检验呢?

最好贴代码,勿贴截图

#2 楼 @xiaomayi0323 下面是整个代码

package com.guowen.appiumdemo;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
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 java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

public class AndroidContactsTest {
    private WebDriver 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.3");
        capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("app-package", "com.example.android.contactmanager");
        capabilities.setCapability("app-activity", ".ContactManager");
        driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public void addContact(){
        WebElement el = driver.findElement(By.name("Add Contact"));
        int i;
        i = driver.findElements(By.xpath("//button")).size();
        i = driver.findElements(By.xpath("//text")).size();
        el.click();
          }

    public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
        private RemoteTouchScreen touch;

        public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
            super(remoteAddress, desiredCapabilities);
            touch = new RemoteTouchScreen(getExecuteMethod());
        }

        public TouchScreen getTouch() {
            return touch;
        }
    }
}
nearly #11 · 2014年05月06日 Author

所有的 i 都等于 0,XPath 没有成功过

我试了一下,全都为空,你 Appium 版本是?

C:\Users\NearlyNie>appium -v
1.0.0

#5 楼 @xiaomayi0323
应该是最新版本吧? 你是什么版本? 用我同样的代码也为空吗!

#3 楼 @nearlynie
1.0 需要用 full class name

driver.findElements(By.xpath("//android.widget.Button"));

#8 楼 @lp19851119 官方有提到为什么会切换回来吗

#9 楼 @seveniruby
没有,只是说要这样用- -

#10 楼 @lp19851119 1.0 的版本变化太多, 这个月我也开始 review 他们的所有测试用例, 追踪下变化

nearly #12 · 2014年05月07日 Author

问题已解决 谢谢

#8 楼 @lp19851119

换成 full class name,果然 OK 了

#13 楼 @xiaomayi0323
你可以看看这个链接,遇到多的就有问题了。
http://testerhome.com/topics/802

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册