Testing 等待元素出现或可单击

Testing 等待元素出现或可单击,testing,automated-tests,appium,qa,appium-android,Testing,Automated Tests,Appium,Qa,Appium Android,我在尝试测试的应用程序上遇到了等待元素的问题。有时Appium找不到元素 这是我的基本测试课程 package Base; import helpers.TestValues; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.AndroidElement; import org.openqa.selenium.remote.DesiredCapabilities;

我在尝试测试的应用程序上遇到了等待元素的问题。有时Appium找不到元素

这是我的基本测试课程

package Base;

import helpers.TestValues;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class BaseTests {
    protected AndroidDriver<AndroidElement> driver;

    @BeforeMethod
    public void setup () throws MalformedURLException {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("deviceName", TestValues.devicesInfo.Honor10);
        caps.setCapability("platformName", TestValues.devicesInfo.PlatformName);
        caps.setCapability("platformVersion", TestValues.devicesInfo.Android810Version);
        caps.setCapability("skipUnlock", TestValues.devicesInfo.SkipUnlock);
        caps.setCapability("appPackage", TestValues.devicesInfo.AppPackageWageName);
        caps.setCapability("appActivity", TestValues.devicesInfo.AppPackageWageActivity);
        caps.setCapability("noReset","false");
        driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),caps);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
       }

    @AfterMethod
    public void teardown(){
        driver.quit();
    }
}
在大多数情况下,我的测试在单击AddTask按钮时失败,即使LoadingSpinner消失,Appium也会尝试定位它,因为我在控制台中看到

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“add_task_button”,“context”:"",“multiple”:false}}

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'add_task_button' using 'ID' with the contextId: '' multiple: false

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=io.wageapp.android:id/add_task_button]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/add_task_button]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=add_task_button]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=add_task_button, INSTANCE=0]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'add_task_button' using 'ID' wi
但大多数情况下,它会因错误而失败

org.openqa.selenium.NoSuchElementException: Can’t locate an element by this strategy: By.chained({By.id: add_task_button})
但id是正确的(有时它会成功地找到它) 我做错了什么?我应该如何正确地处理“等待”,使我的测试能够抵抗这种情况


提前感谢

首先:当您能够找到具有其他属性的元素时,不要使用
xpath
。在您的特定情况下,您可以通过调用
driver.findElementByAccessibilityId(“addoffer”)找到您的元素

第二:我告诉你,你不想得到
NoTouchElementException
,确保元素在页面中。在这种情况下,您可以使用
!driver.findElementsByAccessibilityId(“添加报价”).isEmpty()
如果返回
true
,则查找get此元素

注意:该方法名为
findElement
s
ByAccessibilityId
,并返回
列表


第三:确保您的元素具有相应的属性,如
vivable
clickable
等(根据您的需要)

我使用了id和accessibility id,在这两种情况下,我得到了相同的结果,即使元素存在(并且它的attrivutes clickable和enabled设置为true),它也发现了一次,有两三次没有找到,然后又找到了它,依此类推:(应该是这样的:``AndroidElement=null;boolean elementisVisible=!driver.findElementsByAccessibilityId(“添加要约”).isEmpty();if(elementisVisible)element=driver.findElementByAccessibilityId(“添加要约”);元素。单击()```
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“id”,“selector”:“add_task_button”,“context”:"",“multiple”:false}}

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'add_task_button' using 'ID' with the contextId: '' multiple: false

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=io.wageapp.android:id/add_task_button]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/add_task_button]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=add_task_button]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[DESCRIPTION=add_task_button, INSTANCE=0]

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.

    [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'add_task_button' using 'ID' wi
org.openqa.selenium.NoSuchElementException: Can’t locate an element by this strategy: By.chained({By.id: add_task_button})