Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appium cannot type in textfield

Tags:

I'm using nativescript and nativescript-dev-appium to perform end to end tests and I'm trying to enter some text into a text field.

The textField in my xml layout;

      <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>

My spec;

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 I get;

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)

I've also tried nameField.type("3d printer") and get the same error.

Update: Try 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");

Got error

Error: [waitForElementByXPath("//*[@automationText='name']",5000)] Element condition wasn't satisfied!

Update: find by class name 'textfield' works

If I use the following in my test and the first text field which appears on the form is the name field then this works.

    const nameField = await driver.findElementByClassName(driver.locators.getElementByName("textfield"));
    await nameField.sendKeys("3d printer");

But how do I select the text field if it's not the first field?

like image 877
map7 Avatar asked Sep 11 '19 03:09

map7


1 Answers

Had to use findElementByAccessibilityId like so;

    const nameField = await driver.findElementByAccessibilityId("name");
    await nameField.sendKeys("2");

    const btnTap = await driver.findElementByAutomationText("create");
    await btnTap.click();
like image 138
map7 Avatar answered Sep 30 '22 05:09

map7