簡體   English   中英

我在Android模擬器中使用Appium-Eclipse-TestNG框架單擊或點擊項目時遇到問題

[英]I have an issue clicking or tapping an item using Appium-Eclipse-TestNG framework in android emulator

Appium的新功能,不勝感激

我的代碼:

public void testmethod() {
    List<MobileElement> buttonlist = driver.findElements(By.xpath("//android.app.Dialog//android.view.View//android.widget.Button"));
    buttonlist.forEach(webElement -> log.info(webElement.getAttribute("name")));
    for (MobileElement button : buttonlist) {
        System.out.println(".................button name=>" + button.getAttribute("name") + "<");
        if ("Send Email ".equals(button.getAttribute("name"))) {
            driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
            button.click();
            break;
        }
    }

}

C#for loop打印列表中的所有項目,而使用Java則僅打印其中的一項。 難道我做錯了什么? 我看到它迅速在android屏幕頂部單擊並關閉,而沒有單擊實際元素。

您必須使用唯一的元素ID來訪問按鈕。 您可以使用idresource-idaccessibility idxpath訪問該元素。

不推薦使用xpath

如果您的應用程序元素中沒有唯一的ID,則可以要求開發人員將其放入他們的代碼中。

您可以使用Appium Desktop Inspector來檢查元素。 了解確切的元素后,可以單擊按鈕,如下所示:

MobileElement button= driver.findElementByAccessibilityId("your element's accessibility id");
button.click();
//or
MobileElement button= driver.findElementById("your element's id or resource id");
button.click();

您可以從appium桌面檢查器獲取或ID,資源ID或可訪問性ID

如果要為按鈕使用xpath,請從appium桌面檢查器中獲取它,並將其用作:

MobileElement button= driver.findElement(By.xpath("your element's xpath shown in appium"));
button.click();

我在您的代碼中看到的問題很少。

首先,您不需要在for循環中調用隱式等待。 在初始化驅動程序且時間超過1分鍾或更長時間后立即使用一次。

其次,您在if條件中添加了額外的空間 ,應為

if ("Send Email".contains (button.getAttribute("name"))) {
  button.click();
  break;
}

第三,更多是一個建議。 代替click ,使用類似AndroidTouchAction類的tap方法,

AndroidTouchAction touch = new AndroidTouchAction (driver);
touch.tap (TapOptions.tapOptions ()
  .withElement (ElementOption.element (e)))
  .perform ();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM