简体   繁体   中英

Sendkeys not working in Winappdriver using Appium

I am automating a desktop application using WinAppDriver using Appium in C# language. The issue that I am facing is none of the code for the keyboard interaction is working, like setText, keys.ArrowDown and so on. Initially, it was working absolutely fine, but from the past few days, it stopped working altogether. Can anyone suggest what to do?

using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
using System;

namespace UITest
{
public class Test
{
    public string Name { get; set; } = string.Format("A_EN_{0}", DateTime.Now.Ticks.ToString());
    public string Description { get; set; } = string.Format("A_ED_{0}", DateTime.Now.Ticks.ToString());
    private readonly Records records = null;
    public Test()
    {
        records = new Records();
    }

    public void SetValue(string fieldName, string value = "", bool pressTab = true)
    {
        string propValue;
        if (!string.IsNullOrWhiteSpace(value))
        {
            propValue = value;
        }
        else
        {
            propValue = Common.GetPropValue(this, fieldName).ToString();
        }
        switch (fieldName)
        {
            case "Name":
                Common.RE7Session.FindElementByAccessibilityId("30").SendKeys(propValue);
                break;
            case "Description":
                Common.RE7Session.FindElementByAccessibilityId("18").SendKeys(propValue);
                break;
        }
        if (pressTab)
        {
            Common.SendKeys(Keys.Tab);
        }
    }

      public void AddEvent(Common.SaveOptions saveOptions = Common.SaveOptions.None)
    {
        Common.RE7Session.OpenFunctionalArea("Records");
        records.OpenTask("Events");
        Common.RE7Session.FindElementByName("New Event").Click();
        Common.RE7Session.SwitchWindowWithWait(3);
        this.SetValue(nameof(this.Name));
        this.SetValue(nameof(this.Description));
        Common.RE7Session.SetText("test");
        Common.SendKeys(Keys.ArrowDown);
        Common.RE7Session.PressTab();
        Common.RE7Session.SetText(new Random().Next(10000, 99999).ToString(), false);

    }   

Here, in the last code of lines, SetText, PressTab, Key.ArrowDown, none of this is working. Following are the methods of PressTab, SendKeys:

public static void PressTab(this WindowsDriver<WindowsElement> RE7Session, int iteration = 1)
    {
        for (int i = 0; i < iteration; i++)
        {
            Common.SendKeys(Keys.Tab);
        }
    }

public static void SendKeys(string keysToSend)
    {
        System.Windows.Forms.SendKeys.SendWait(ReplaceKeyCode(keysToSend));
        Sleep(100);
    }

private static string ReplaceKeyCode(string keys)
    {
        keys = keys.Trim('+');
        //Action characters like tab, arrow down and etc
        keys = keys.Replace(Keys.Backspace, "{BACKSPACE}");
        keys = keys.Replace(Keys.Delete, "{DELETE}");
        keys = keys.Replace(Keys.ArrowDown, "{DOWN}");
        keys = keys.Replace(Keys.End, "{END}");
        keys = keys.Replace(Keys.Enter, "{ENTER}");
        keys = keys.Replace(Keys.Escape, "{ESC}");
        keys = keys.Replace(Keys.Help, "{HELP}");
        keys = keys.Replace(Keys.Home, "{HOME}");
        keys = keys.Replace(Keys.Insert, "{INSERT}");
        keys = keys.Replace(Keys.ArrowLeft, "{LEFT}");
        keys = keys.Replace(Keys.PageDown, "{PGDN}");
        keys = keys.Replace(Keys.PageUp, "{PGUP}");
        keys = keys.Replace(Keys.ArrowRight, "{RIGHT}");
        keys = keys.Replace(Keys.Tab, "{TAB}");
        keys = keys.Replace(Keys.ArrowUp, "{UP}");
        keys = keys.Replace(Keys.F1, "{F1}");
        keys = keys.Replace(Keys.F2, "{F2}");
        keys = keys.Replace(Keys.F3, "{F3}");
        keys = keys.Replace(Keys.F4, "{F4}");
        keys = keys.Replace(Keys.F5, "{F5}");
        keys = keys.Replace(Keys.F6, "{F6}");
        keys = keys.Replace(Keys.F7, "{F7}");
        keys = keys.Replace(Keys.F8, "{F8}");
        keys = keys.Replace(Keys.F9, "{F9}");
        keys = keys.Replace(Keys.F10, "{F10}");
        keys = keys.Replace(Keys.F11, "{F11}");
        keys = keys.Replace(Keys.F12, "{F12}");
        keys = keys.Replace(Keys.Add, "{ADD}");
        keys = keys.Replace(Keys.Subtract, "{SUBTRACT}");
        keys = keys.Replace(Keys.Multiply, "{MULTIPLY}");
        keys = keys.Replace(Keys.Divide, "{DIVIDE}");

        //Special Keys like control, shift and alt
        keys = keys.Replace(Keys.Control, "^");
        keys = keys.Replace(Keys.LeftControl, "^");
        keys = keys.Replace(Keys.Shift, "+");
        keys = keys.Replace(Keys.LeftShift, "+");
        keys = keys.Replace(Keys.Alt, "%");
        keys = keys.Replace(Keys.LeftAlt, "%");
        return keys;
    }

There have been some changes in the library due to which the direct keys.[key] is not working. Hence, I made the changes to the keys using the above relaceKeyCode method. But still, this is not working.

Perhaps this will help: https://github.com/Microsoft/WinAppDriver/issues/134

In short, SendKeys(Keys.Contro + "a")., for exampl,e depresses the control key but does not release it. If you want to release the control key, you need to repeat it as in SendKeys(Keys.Control + "a" + Keys.Control).

Code well and prosper!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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