#13 楼 @xiaomayi0323
你可以看看这个链接,遇到多的就有问题了。
http://testerhome.com/topics/802
问题已解决 谢谢
问题已解决 谢谢
#5 楼 @xiaomayi0323
应该是最新版本吧? 你是什么版本? 用我同样的代码也为空吗!
C:\Users\NearlyNie>appium -v
1.0.0
如下是全部的代码,i 都是=0,XPath 在我的代码里面没有成功获取过
package com.saucelabs.appium;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
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 org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
public class AndroidTest {
private static WebDriver driver;
private static JavascriptExecutor js;
private static WebDriverWait driverWait;
@Before
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "api.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", "io.appium.android.apis");
capabilities.setCapability("app-activity", ".apis");
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 apiDemo(){
WebElement el;
el = driver.findElement(By.name("OS"));
List<WebElement> els = driver.findElements(By.className("android.widget.TextView"));
assertEquals("OS", els.get(8).getText());
int i;
i= -1;
//i = driver.findElements(By.xpath("//*")).size();
i = driver.findElements(By.xpath("//*[@clickable=true]")).size();
i = driver.findElements(By.xpath("text[@text=OS]")).size();
i = driver.findElements(By.xpath("//text[@text=OS]")).size();
i = driver.findElements(By.xpath("text[@text='OS']")).size();
i = driver.findElements(By.xpath("//text[@text='OS']")).size();
i = driver.findElements(By.xpath("TextView[@text=OS]")).size();
i = driver.findElements(By.xpath("//TextView[@text=OS]")).size();
i = driver.findElements(By.xpath("TextView[@text='OS']")).size();
i = driver.findElements(By.xpath("//TextView[@text='OS']")).size();
i = driver.findElements(By.xpath("//FrameLayout/View/FrameLayout/ListView/TextView[@text=OS]")).size();
i = driver.findElements(By.xpath("//FrameLayout/View/FrameLayout/ListView/TextView[@text='OS']")).size();
i = driver.findElements(By.xpath("//FrameLayout/ListView/TextView[@text=OS]")).size();
i = driver.findElements(By.xpath("//FrameLayout/ListView/TextView[@text='OS']")).size();
i = driver.findElements(By.xpath("//frame/view/frame/list/text[@text=OS]")).size();
i = driver.findElements(By.xpath("//frame/view/frame/list/text[@text='OS']")).size();
i = driver.findElements(By.xpath("//frame/list/text[@text=OS]")).size();
i = driver.findElements(By.xpath("//frame/list/text[@text='OS']")).size();
i = driver.findElements(By.xpath("//frame/list/text[0]")).size();
i = driver.findElements(By.xpath("//frame/list/text[1]")).size();
i = driver.findElements(By.xpath("//FrameLayout[1]/View[1]/FrameLayout[1]/ListView[1]/TextView[@text=OS]")).size();
i = driver.findElements(By.xpath("//FrameLayout[1]/View[1]/FrameLayout[1]/ListView[1]/TextView[@text='OS']")).size();
i = driver.findElements(By.xpath("//FrameLayout[1]/ListView[1]/TextView[@text=OS]")).size();
i = driver.findElements(By.xpath("//FrameLayout[1]/ListView[1]/TextView[@text='OS']")).size();
i = driver.findElements(By.xpath("//frame[1]/view[1]/frame[1]/list[1]/text[@text=OS]")).size();
i = driver.findElements(By.xpath("//frame[1]/view[1]/frame[1]/list[1]/text[@text='OS']")).size();
i = driver.findElements(By.xpath("//frame[1]/list[1]/text[@text=OS]")).size();
i = driver.findElements(By.xpath("//frame[1]/list[1]/text[@text='OS']")).size();
}
public static void takeScreenShot(WebDriver driver,String screenShotName)
{
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShotFile, new File("D:\\AutoScreenCapture\\" + getCurrentDateTime()+ "_" + screenShotName + ".jpg"));
}
catch (IOException e) {e.printStackTrace();}
}
public static String getCurrentDateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
return df.format(new Date());
}
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;
}
}
}
所有的 i 都等于 0,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;
}
}
}
是不是我的设备还是环境版本有问题,XPath 没有一个成功的。怎么检验呢?
还是找不到
#1 楼 @seveniruby
driver.findElements(By.xpath("//text[@text='OS']")).size();
//String strtest = testList.get(0).getAttribute("text");
driver.findElements(By.xpath("//text")).size();
由于两个 size()都是 0,所有中间那行报错。是不是我的设备还是环境版本有问题,XPath 没有一个成功的。