我正在使用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打印机”)并得到相同的错误。
更新:尝试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");
但是如果不是第一个字段,如何选择文本字段?
答案 0 :(得分:3)
必须像这样使用findElementByAccessibilityId;
const nameField = await driver.findElementByAccessibilityId("name");
await nameField.sendKeys("2");
const btnTap = await driver.findElementByAutomationText("create");
await btnTap.click();
答案 1 :(得分:0)
如果您先尝试清理元素,例如myTextField.clean()然后再发送sendKeys,该怎么办