java-appium-527操作

Posted singleSpace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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{
            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():[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.AndroidElement

Process finished with exit code 0

以上是关于java-appium-527操作的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode 如何操作用户自定义代码片段(快捷键)

代码片段 - Golang 实现集合操作

Apollo Codegen 没有找到生成代码的操作或片段

操作栏标签片段中的片段?