对于 android webview app 之前 selendroid 是无法通过 JS 注入输入中文的.
经过和 selendroid 团队沟通,fix,目前 0.8 版本大家可以通过如下方式注入 JS,解决中文输入问题:
Util.driver.execute_script('arguments[0].value = arguments[1];',element,"中文可以的。。。。")
哦? 我去试试 4.1 以上可以不
问个问题,第一个参数是什么意思。。。
可以,我是 4.3 的。。。第一个参数就是 elemen 对象
非 webview 的话, 他应该也可以输入中文吧. 毕竟是插桩体系.
输入中文有问题的主要是基于 uiautomator 的方式. uiautomator 没法像插桩一样调用内部方法.
@sundaxian ,和语言无关,只要 java 能 run javascript.
#7 楼 @sundaxian 就是给对应的 element 设置它的 value.....argument[0] 代表 element, argument[1] 代表 value
#8 楼 @luis 嗯 我在非 webview app 试了下, appium 用 android 作为 device 的时候 是不支持的 报错说此方法还没有实现,
在用 selendroid 的时候
public static void inputChinese(WebElement ele, String str) {
js = (JavascriptExecutor) driver();
js.executeScript("arguments[0].value = arguments[1];",ele,"中文可以的。。。。");
也报错,是不是这个只支持 webview app 而不支持 非 webview app 吧。
@sundaxian 帖子就说此方法只适用 android webview app......而且 selendroid 必须是 0.8 以上
#10 楼 @luis
WebElement ele = driver.findElement(By.id("leaderIdea_text"));
js =(JavascriptExecutor) driver;
js.executeScript("arguments[0].value = arguments[1];",ele,"中文呢");
这样输入中文报错呢?
debug: Request received with params: {"args":[{"ELEMENT":"0c0414aa-4384-43e1-93db-c7e383f2f7
cd"},"中文呢"],"script":"arguments[0].value = arguments[1];"}
debug: Proxying command to localhost:8080
info: Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/aea4bd37-6
d4b-474b-90f0-cc3ae01f445a/execute","method":"POST","json":{"args":[{"ELEMENT":"0c0414aa-438
4-43e1-93db-c7e383f2f7cd"},"中文呢"],"script":"arguments[0].value = arguments[1];"}}
info: Responding to client with error: {"status":13,"value":{"message":"An unknown server-si
de error occurred while processing the command.","origValue":"{\"message\":\"Cannot set prop
erty 'value' of undefined\"}"},"sessionId":"aea4bd37-6d4b-474b-90f0-cc3ae01f445a"}
POST /wd/hub/session/aea4bd37-6d4b-474b-90f0-cc3ae01f445a/execute 500 196ms - 255b
debug: Appium request initiated at /wd/hub/session/aea4bd37-6d4b-474b-90f0-cc3ae01f445a
WebElement el = driver.findElement(By.name("Add Contact"));
el.click();
List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield"));
((JavascriptExecutor)driver).executeScript("arguments[0].value = arguments[1];",textFieldsList.get(0),"王阳");
执行后报错:
org.openqa.selenium.WebDriverException: Not yet implemented. Please help us: http://appium.io/get-involved.html (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 6 milliseconds
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:55:11'
System info: host: 'WangYang', ip: '192.168.51.64', os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_51'
Session ID: 7518b917-c78e-4181-9a2c-3e3bef935b03
Driver info: com.guowen.appiumdemo.AndroidContactsTest$SwipeableWebDriver
Capabilities [{app=C:\Workspace\appdemo\apps\ContactManager.apk, platform=WINDOWS, javascriptEnabled=true, databaseEnabled=false, browserName=, app-package=com.example.android.contactmanager, webStorageEnabled=false, device=Android, app-activity=.ContactManager, takesScreenshot=true, version=4.2.2}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:482)
at com.guowen.appiumdemo.AndroidContactsTest.addContact(AndroidContactsTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
@young ,你 device=Android,当然不行,我这个是给 hybrid app 用的。。。