无法在Appium中执行click或sendkeys

时间:2018-01-24 12:40:40

标签: java android selenium appium

我正在尝试使用Appium点击Android应用中的按钮,但没有运气。

UIAutomatorViewer中的元素

enter image description here

使用以下代码:

PageClass

public class MyProfilePage 
{
    AndroidDriver<MobileElement> driver;

    @FindBy(id="com.rawalinfocom.rcontact:id/image_right_left") 
    public WebElement profileEditIcon;

    @FindBy(id="com.rawalinfocom.rcontact:id/input_first_name") 
    public WebElement firstName;

    @FindBy(id="com.rawalinfocom.rcontact:id/input_last_name")  
    public WebElement lastName;

    @FindBy(id="com.rawalinfocom.rcontact:id/button_name_update")   
    public WebElement updateButton;

    @FindBy(id="com.rawalinfocom.rcontact:id/snackbar_text")    
    public WebElement notificationMessage;

    public MyProfilePage(AndroidDriver<MobileElement> driver)
    {
        PageFactory.initElements(driver, this);
        this.driver=driver;
    }

    public void clickProfileEditIcon()
    {
        profileEditIcon.click();
    }

    public String getFirstName()
    {
        return firstName.getText();
    }

    public void enterFirstName(String name)
    {
        firstName.clear();
        firstName.sendKeys(name);
    }

    public void enterLastName(String name)
    {
        lastName.clear();
        lastName.sendKeys(name);
    }

    public void clickUpdateButton()
    {
        updateButton.click();
    }

    public String getNotificationMessage()
    {
        return notificationMessage.getText();
    }
}

这里是测试用例类

@Test
public void updateFirstNameAndVerifyChanges()
{
    homepage.clickBreadCrumbIcon();
    drawer.clickUsername();
    myprofilepage.clickProfileEditIcon();
    WaitHelper.waitUntilElementGetClickable(driver, myprofilepage.firstName);
    myprofilepage.enterFirstName(reader.getCellData(0, 4, 1));
    myprofilepage.clickUpdateButton();
    WaitHelper.waitUntilElementHasText(driver, myprofilepage.notificationMessage);
    System.out.println(myprofilepage.getNotificationMessage());
}

但是无法对此活动执行单一操作(单击或发送键)。始终获得NoSuchElementException

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

public class MyProfilePage 
{
    AndroidDriver<MobileElement> driver;

    @FindBy(id="image_right_left") 
    public WebElement profileEditIcon;

    @FindBy(id="input_first_name") 
    public WebElement firstName;

    @FindBy(id="input_last_name")  
    public WebElement lastName;

    @FindBy(id="button_name_update")   
    public WebElement updateButton;

    @FindBy(id="snackbar_text")    
    public WebElement notificationMessage;

    public MyProfilePage(AndroidDriver<MobileElement> driver)
    {
        PageFactory.initElements(driver, this);
        this.driver=driver;
    }

    public void clickProfileEditIcon()
    {
        profileEditIcon.click();
    }

    public String getFirstName()
    {
        return firstName.getText();
    }

    public void enterFirstName(String name)
    {
        firstName.clear();
        firstName.sendKeys(name);
    }

    public void enterLastName(String name)
    {
        lastName.clear();
        lastName.sendKeys(name);
    }

    public void clickUpdateButton()
    {
        updateButton.click();
    }

    public String getNotificationMessage()
    {
        return notificationMessage.getText();
    }
}

如果仍然无效,请在代码中使用Xpath代替id。

答案 1 :(得分:0)

我得到了解决方案:

实际上这个问题与虚拟设备有关。我使用 NEXUS 5 API 24 Android版本7 虚拟设备

我已更改为 Google Nexus 6 API 23 Android版本6.0 虚拟设备并且工作正常

可能是设备相关问题