【问题标题】:Appium AndroidDriver sendKeys send text to last edited textbox even after scrolling down and sending text to the another field即使在向下滚动并将文本发送到另一个字段之后,Appium AndroidDriver sendKeys 也会将文本发送到最后编辑的文本框
【发布时间】:2016-06-24 15:53:20
【问题描述】:

我正在尝试自动化 Salesforce 本机应用程序创建联系页面。我可以在 android mobile 的第一页上看到的所有字段中单击并输入文本。但是对于我在向下滚动页面后得到的其他字段,appium 能够从 appium 日志中找到该字段,但是在单击和发送文本时,它总是发送到第一页上最后编辑的文本框。

如果在使用 appium 滚动页面后需要额外发送文本,请告诉我。

我正在使用 Android 三星 Galaxy S3 设备。下面是代码和 UIAutomator 屏幕截图。

package samsungGalaxy;

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

import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;

public class FirstTest {
    AndroidDriver driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "ce1d12134");
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
        capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.salesforce.chatter");
        capabilities.setCapability("appActivity", "com.salesforce.chatter.Chatter");
        capabilities.setCapability("appium-version", "1.4.16.1");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }

    @Test
    public void login() {

        driver.findElement(By.xpath("//android.widget.ImageView[contains(@resource-id,'home')]")).click();
        driver.findElement(By.xpath("//android.widget.TextView[@text='Contacts']")).click();
        driver.findElement(By.id("com.salesforce.chatter:id/new_button")).click();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]/..//android.view.View[contains(@content-desc,'Phone')]/../android.widget.EditText")).click();
        driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]/..//android.view.View[contains(@content-desc,'Phone')]/../android.widget.EditText")).sendKeys("5109651200");
        driver.hideKeyboard();
        driver.scrollTo("Mobile");
        driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]/..//android.view.View[contains(@content-desc,'Mobile')]/../android.widget.EditText")).click();
        driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]/..//android.view.View[contains(@content-desc,'Mobile')]/../android.widget.EditText")).sendKeys("6509651200");

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    @AfterTest
    public void End() {
        driver.closeApp();
        driver.quit();
    }

}

我也尝试使用 TouchAction 将文本发送到 Mobile 字段,如下所示。在 appium 日志中显示成功,但在移动页面上既没有点击也没有输入。

driver.swipe(200, 1140, 250, 600, 4000);
    WebElement we = driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Mobile')]/..//android.widget.EditText"));
    TouchAction touchAction = new TouchAction(driver);
    touchAction.press(we);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    boolean displayed = we.isDisplayed();
    System.out.println("Displayed :" + displayed);
    we.click();
    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Mobile')]/..//android.widget.EditText")).sendKeys("6509651200");

【问题讨论】:

    标签: java android selenium appium


    【解决方案1】:

    我猜你也在指定相同的定位器在滚动到一个元素后发送键。声明

    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]/..//android.view.View[contains(@content-desc,'Mobile')]/../android.widget.EditText")).sendKeys("5109651200"); // or replace `Mobile` by `Phone`
    

    正在定位相同的android.widget.EditText,这可能是因为您使用了/../ 的XPath 语法。引用w3schools,Xpath 语法如下:

    nodename  Selects all nodes with the name "nodename"
    /         Selects from the root node
    //        Selects nodes in the document from the current node that match the selection no matter where they are
    .         Selects the current node
    ..        Selects the parent of the current node
    @         Selects attributes
    

    所以您可能希望将代码更改为:

    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Phone')]../android.widget.EditText")).sendKeys("5109651200");
    driver.hideKeyboard();  
    driver.scrollTo("Mobile");
    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Mobile')]../android.widget.EditText")).sendKeys("6509651200");
    

    注意:在您的情况下,../..// 都应该工作,因为 应用中 android 类的层次结构。

    【讨论】:

    • 此 xpath 更改无效,并因 org.openqa.selenium.InvalidSelectorException 失败:参数是无效的选择器(例如 XPath/CSS)
    • 好的,您能否用appium inspector 屏幕截图更新问题,以便描述EditText 中的任何一个的XPath
    • 我在真实设备上运行应用程序并通过 USB 连接到窗口笔记本电脑。当我尝试刷新 appium 时,它会抛出错误为“错误:未设置应用程序;”。所以我无法在 appium 检查器上获取应用程序的屏幕截图。 appium 检查器始终为空白。
    • 如果您可以跟踪 EditText 的 XPath,这将有助于调试代码
    • 我在 UI Automator 查看器上构建的 XPath 和我在代码中使用的 XPath 应该是正确的,因为类似的 XPath 适用于第一页中可见的电话字段。 appium 检查器是否可以在带有 android 设备的窗口机器上工作?
    【解决方案2】:

    使用您的代码如下:

    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]//android.view.View[contains(@content-desc,'Phone')]//android.widget.EditText")).click();                                                                        
    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]//android.view.View[contains(@content-desc,'Phone')]//android.widget.EditText")).sendKeys("5109651200");
    driver.hideKeyboard();
    driver.scrollTo("Mobile");
    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]//android.view.View[contains(@content-desc,'Mobile')]//android.widget.EditText")).click();
    driver.findElement(By.xpath("//android.view.View[contains(@content-desc,'Create Contact Heading')]//android.view.View[contains(@content-desc,'Mobile')]//android.widget.EditText")).sendKeys("6509651200");
    

    【讨论】:

    • 此 xpath 更改也不起作用,并因 org.openqa.selenium.NoSuchElementException 失败:使用给定的搜索参数无法在页面上找到元素。
    • @Sadik android.view.View[contains(@content-desc,'Phone')]android.widget.EditText 似乎处于同一水平
    猜你喜欢
    • 2023-03-26
    • 2018-08-21
    • 1970-01-01
    • 2015-06-23
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多