简体   繁体   English

setValue和sendKeys引发错误,以使用Appium和Android在EditText中设置文本

[英]setValue and sendKeys throw error to set a text in EditText with Appium and Android

What I try to do in order to test my LoginActivity is to set a text in the email Edittext using Appium and Android Studio. 为了测试LoginActivity ,我LoginActivity是使用Appium和Android Studio在电子邮件Edittext设置文本。

My 1st version of the test included a WebDriver and a Webelement for the EditText , so i used sendKeys() to set the text. 我的第一个测试版本包括一个WebDriver和一个EditTextWebelement ,因此我使用sendKeys()设置了文本。

This is the code: 这是代码:

WebDriver driver;

@Before
public void testApp() throws MalformedURLException, InterruptedException {
    String apkpath = "C:\\Users\\0013498\\Desktop\\qa-XXX-2-2-4-2.apk";
    File app = new File(apkpath);
    DesiredCapabilities capabilities= new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME,"Android");
    capabilities.setCapability("deviceName","Emulator");
    capabilities.setCapability("platformName","Android");
    capabilities.setCapability("app",app.getAbsolutePath());
    capabilities.setCapability("appPackage", "com.XXX.XXX");
    capabilities.setCapability("appActivity", "com.XXX.XXX.SplashScreenActivity");
    driver = new AppiumDriver(new URL("http://127.0.0.1:4725/wd/hub"),capabilities);
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

}


@Test
public void appiumExampleTest() throws Exception {
String path="//android.widget.EditText[contains(@resource-id,\"email\")]";
    click(path);
    elementSendText(path, "@");
}
public void click(String element) {
    WebElement webElement = driver.findElement(By.xpath(element));
    webElement.click();
    System.out.println("Click element: "+element+" index = "+0);

}


public void elementSendText(String element,  String text) {
    WebElement webElement = driver.findElement(By.xpath(element));
    webElement.sendKeys(text);

}

this is the screenshot from UI Automator Viewer for the LoginActivity where the email EditText is : 这是UI Automator Viewer中用于LoginActivity的屏幕快照,其中电子邮件EditText为:

在此处输入图片说明

BUT sendKeys() throws the following error: 但是sendKeys()会引发以下错误:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.

So, after some digging, i changed my code to start using AndroidDriver and AndroidElement like this: 因此,经过一些挖掘,我将代码更改为开始使用AndroidDriverAndroidElement如下所示:

AndroidDriver driver;
@Before
public void testApp() throws MalformedURLException, InterruptedException {
    String apkpath = "C:\\Users\\0013498\\Desktop\\qa-XXX-2-2-4-2.apk";
    File app = new File(apkpath);
    DesiredCapabilities capabilities= new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME,"Android");
    capabilities.setCapability("deviceName","Emulator");
    capabilities.setCapability("platformName","Android");
    capabilities.setCapability("app",app.getAbsolutePath());
    capabilities.setCapability("appPackage", "com.XXX.XXX");
    capabilities.setCapability("appActivity", "com.XXX.XXX.SplashScreenActivity");       
    driver = new AndroidDriver(new URL("http://127.0.0.1:4725/wd/hub"),capabilities);//<----
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

}
@Test
public void appiumExampleTest() throws Exception {
AndroidElement emailET= (AndroidElement)driver.findElementById("com.XXX.XXX:id/email");//<----
    emailET.click();//<----
    emailET.setValue("@");
}

and now, setValue() throws the following error: 现在,setValue()引发以下错误:

org.openqa.selenium.WebDriverException: Not yet implemented.

And honestly, I don't know how to move forward, i'm so stuck here. 老实说,我不知道如何前进,我被困在这里。 This can't be that difficult, what am i missing? 这不是那么困难,我想念的是什么?

Thanks in advance. 提前致谢。

EDIT: 编辑:

as requested in one of the answers, this is the full error: 根据答案之一的要求,这是完整的错误:

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 77 milliseconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'PC0C283Q', ip: '192.168.0.101', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_76-release'
Driver info: io.appium.java_client.AppiumDriver
Capabilities [{app=C:\Users\0013498\Desktop\qa-XXX-2-2-4-2.apk, appPackage=com.XXX.XXX, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, deviceName=WKS8DAV8GEYTQGB6, platform=LINUX, appActivity=com.XXX.XXX.SplashScreenActivity, desired={app=C:\Users\0013498\Desktop\qa-interview-2-2-4-2.apk, appPackage=com.XXX.XXX, appActivity=com.XXX.XXX.SplashScreenActivity, platformName=Android, deviceName=Emulator}, platformVersion=6.0, webStorageEnabled=false, locationContextEnabled=false, browserName=Android, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: 3f61a37c-0f35-487c-be76-97154a8b6e04

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at io.appium.java_client.DefaultGenericMobileElement.execute(DefaultGenericMobileElement.java:45)
at io.appium.java_client.MobileElement.execute(MobileElement.java:1)
at io.appium.java_client.android.AndroidElement.execute(AndroidElement.java:1)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:98)
at com.cheerle.mongentesting01.test02.elementSendText(test02.java:150)
at com.cheerle.mongentesting01.test02.appiumExampleTest(test02.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true

EDIT: 编辑:

maybe build.gradle has sth to do with the issue? 也许build.gradle与这个问题有关?

    apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
    applicationId "com.cheerle.mongentesting01"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/java-client-5.0.0-BETA3.jar')
    compile files('libs/selenium-server-standalone-3.0.1.jar')
}

in the project: 在项目中:

在此处输入图片说明

Also, Run Configuration settings I have my test02 like this: 另外,运行配置设置我的test02像这样:

在此处输入图片说明

This is Appium server screen & configs: 这是Appium服务器的屏幕和配置: 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Would this help? 这会有所帮助吗?

Try emailET.sendKeys("@") ; 试试emailET.sendKeys(“ @”) ; with AndroidDriver code which you posted. 与您发布的AndroidDriver代码一起使用。

Once you click on the EditText element. 一旦单击EditText元素。 Just add the following code instead of you setValue or sendKeys code. 只需添加以下代码,而不是您的setValue或sendKeys代码。

driver.getKeyboard().sendKeys("textToBeTyped");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM