【问题标题】:Unable to perform click or sendkeys in Appium无法在 Appium 中执行 click 或 sendkeys
【发布时间】:2018-07-03 12:21:19
【问题描述】:

我正在尝试使用 Appium 单击 Android 应用中的按钮,但没有成功。

UIAutomatorViewer 中的元素

同样使用下面的代码:

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

【问题讨论】:

  • 您声明您正在尝试单击一个按钮,但您从未说明是哪个按钮。我提醒你发布一个 MCVE,但你已经发布了大约 70 行代码。我们不需要完整的页面对象和完整的测试,只需发布​​您用于单击按钮的相关代码行、发布不同的定位器尝试等以及任何错误消息。这些都不是不合理的要求,并且都包含在我上面发布的链接中。
  • @JeffC 他们试图点击的元素在他们的图片中以红色框突出显示。他们的代码没有不必要的冗长,这要归功于所讨论的语言。
  • @Rob 图片中突出显示的元素是文本字段,而不是按钮。 OP声明,@ 987654326@。我对 Java 很熟悉……你不需要 70 行代码来单击一个按钮。 MCVE 的重点是只有重现问题所需的代码。这不是 MCVE。
  • @NarendraR 我确实看到了,但不清楚你所说的“活动”是什么。您可能应该澄清这个问题。无需发布整个课程,只需发布​​与 MCVE 相关的代码即可。
  • @JeffC,我在我的问题中有标签appium 和android,并且在 android 中有一个名为 Activity paste what is activity in android 在谷歌上的屏幕。如果你不明白这个问题。那么对不起,这不是我的错。

标签: java android selenium appium


【解决方案1】:

我得到了解决方案:

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

我对 Google Nexus 6 API 23 Android 版本 6.0 虚拟设备进行了更改,并且运行良好

可能是设备相关问题

【讨论】:

  • 你的appium版本是多少?
  • @akhileshgulati v1.7.1
【解决方案2】:

试试下面的代码:

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。

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2016-05-14
    • 2019-08-26
    相关资源
    最近更新 更多