恢弘志士之气,不宜妄自菲薄。这篇文章主要讲述java-appium-527操作相关的知识,希望能为你提供帮助。
【java-appium-527操作】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{
returnnull;
}
}
public WebElement locate(String locate){
if (locate.matches("\/\/.*")){
returndriver.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():[email
protected]
获取长度和高度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.AndroidElementProcess finished with exit code 0
推荐阅读
- How Many to Be Happy?
- Android - ViewPager
- Android Menu
- AnnotationConfigApplicationContext
- Android 博客导航
- java/android ?????????????????????????????????(17)
- Appium自动化WebView中元素的操作
- [CF348B]Apple Tree
- Appium环境搭建(Java版本)