【发布时间】:2019-09-11 03:21:13
【问题描述】:
我正在使用 nativescript 和 nativescript-dev-appium 来执行端到端测试,并且我正在尝试在文本字段中输入一些文本。
我的 xml 布局中的 textField;
<StackLayout padding="10">
<Label text="Date of Purchase"></Label>
<DatePickerField hint="Select Date" [(ngModel)]="_dataItem.date_of_purchase"></DatePickerField>
</StackLayout>
<StackLayout padding="10">
<Label text="Name"></Label>
<TextField automationText="name" hint="Asset Name" [(ngModel)]="_dataItem.name" class="input">
</TextField>
</StackLayout>
我的规格;
import { AppiumDriver, createDriver, SearchOptions, nsCapabilities } from "nativescript-dev-appium";
import { assert } from "chai";
const addContext = require('mochawesome/addContext');
describe("sample scenario", () => {
let driver: AppiumDriver;
before(async function(){
nsCapabilities.testReporter.context = this;
driver = await createDriver();
});
after(async function () {
await driver.quit();
console.log("Quit driver!");
});
afterEach(async function () {
if (this.currentTest.state === "failed") {
await driver.logTestArtifacts(this.currentTest.title);
}
});
it("should find an element by text", async function () {
const nameField = await driver.findElementByAutomationText("name");
await nameField.sendKeys("3d printer");
const btnTap = await driver.findElementByAutomationText("create");
await btnTap.click();
});
});
我得到的错误;
Error: [element.sendKeys("3d printer")] Error response status: 12, InvalidElementState - An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element).
Selenium error: io.appium.uiautomator2.common.exceptions.InvalidElementStateException: Cannot set the element to '3d printer'.
Did you interact with the correct element? at io.appium.uiautomator2.handler.SendKeysToElement.safeHandle(SendKeysToElement.java:97) at
io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:37) at
io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:252) at
io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:243) at
io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:44) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at
io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at
io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435) at
io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) at
io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) at
io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at
io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at
io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at
io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at
io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911) at
io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at
io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611) at
io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552) at
io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) at
io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140) at
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) at java.lang.Thread.run(Thread.java:761)
我也尝试过 nameField.type("3d printer") 并得到同样的错误。
更新:试试 Xpath
it("should find an element by text", async function () {
const nameField = await driver.findElementByXPath("//*[@automationText='name']");
await nameField.click();
await nameField.sendKeys("3d printer");
有错误
Error: [waitForElementByXPath("//*[@automationText='name']",5000)] Element condition wasn't satisfied!
更新:按类名“textfield”查找有效
如果我在测试中使用以下内容,并且表单上出现的第一个文本字段是名称字段,则此方法有效。
const nameField = await driver.findElementByClassName(driver.locators.getElementByName("textfield"));
await nameField.sendKeys("3d printer");
但是如果不是第一个字段,我该如何选择文本字段呢?
【问题讨论】:
-
尝试使用
xpath的查找元素更改.findElementByAutomationText,并使用以下值://*[@automationText='name']。在sendkey之前,先点击一下。 -
现在根本找不到元素。
标签: android mobile automated-tests appium nativescript