1. 程式人生 > >java-appium-527操作

java-appium-527操作

tca sendkeys ddr local matches gis cit 選中 mat

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操作