等待元素出现或可点击

时间:2019-12-04 12:01:39

标签: testing automated-tests appium qa appium-android

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

这是我的BaseTests类

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();
    }
}

这是带有页面对象的MainPage类

package PageObjects;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;



public class MainPage {
    private AndroidDriver<AndroidElement> driver;

    public MainPage(AndroidDriver<AndroidElement> driver) {
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver) ,this);
    }
    @AndroidFindBy(id = "add_task_button")
    private AndroidElement AddTaskButton;

    @AndroidFindBy(id = "com.android.packageinstaller:id/permission_allow_button")
    private AndroidElement PermissionAllowButton;

    public void ClickOnAllowButton(){
        PermissionAllowButton.click();
    }
    public boolean IsAddTaskVisible(){
        return AddTaskButton.isDisplayed();
    }
    public void ClickOnAddTaskButton() {

        AddTaskButton.click();
    }

}

和测试

@Test
public void PostTask (){
_landingPage.clickOnLoginButton();
_loginPage.TypeEmail(TestValues.Credentials.CorrectEmail);
_loginPage.TypePassword(TestValues.Credentials.CorrectPassword);
_loginPage.ClickOnLoginBUtton();
_mainPage.ClickOnAllowButton();
//here is loading spinner because app fetch data from API
_mainPage.ClickOnAddTaskButton();
_mainPage.ClickOnAllowButton();
_postTaskPage.SendPhoto();
}

在大多数情况下,即使加载微调器消失了,我的测试也无法单击“添加任务”按钮,因为我在控制台中看到了,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是正确的(有时可以成功找到它) https://i.postimg.cc/KjVYPnQF/2019-12-04-12-52-38-Appium.png 我究竟做错了什么?我应该如何正确处理“等待”以使我的测试可以抵抗这种情况。

预先感谢

1 个答案:

答案 0 :(得分:0)

首先:当您能够找到具有其他属性的元素时,请勿使用xpath。在特定情况下,您可以通过调用driver.findElementByAccessibilityId("Add offer");

来查找元素

第二:如果您不想获取NoSuchElementException,请确保该元素在页面中。在这种情况下,可以使用!driver.findElementsByAccessibilityId("Add offer").isEmpty();,如果它返回true,请找到get这个元素。

注意:该方法名称为findElement s ByAccessibilityId,并重复List<T>

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