即使在向下滚动并将文本发送到另一个字段后,Appium AndroidDriver sendKeys也会将文本发送到上次编辑的文本框

时间:2016-03-10 01:02:39

标签: java android selenium appium

我正在尝试自动化salesforce原生应用程序创建联系页面。我可以在Android移动设备的第一页上看到的所有字段中单击并输入文本。但是对于我在向下滚动页面后得到的其他字段,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();
    }

}

UIAutomator viewer pic

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

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");

appiumInspectImage

2 个答案:

答案 0 :(得分:0)

我猜你是指定相同的定位器,也可以在滚动到元素后发送密钥。声明

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类层次结构。

答案 1 :(得分:0)

使用您的代码如下:

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");