Search code examples
c#seleniumbddwinappdriver

OpenQA.Selenium.Interactions - KeyUp seems not taking affect after a KeyDown action


I am currently undertaking a massive challenge on inheriting our test automation lead's Test Automation framework. So far is ok. but I come to the bit where I need to simulate a Hold Shift Key Down and hit the down arrow a number of times then Release the Shift keys.

Working with Visual Studio 2019, the framework using Specflow/Selenium in C#/WinAppDriver/Appium is used to test a Classic Window Application.

I am fairly very new to all of this so bear with me if my question isn't making much sense.

The BDD statement:

Scenario Outline: XXX - I Select All Wells using Shift and arrow keys
    When I Select TreeNode "TestData" In Tree "Wells"
    When I Hold "Shift" And Press "Down Arrow" For "3" Times
    When I Right Click On TreeNode "(3) Wellbore Stability Well" In Tree "Wells"
    When I Press "Down Arrow" Key
    When I Press "Enter" Key

So its a treelist, containing 4 node, I want to click on the top node then use the Keyboard action to highlights all of the nodes - I right click on one of the node then choose an option and hit Enter.

Example of the Feature Test

Updated @03/03/2022 - All involved Step Definitions from the Feature above.

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

public static class Location
{
    public static string location;
    public static string areaPath = "";
    public static string applicationType = "web";
    public static bool outputOnly = false;
    public static bool featureFileOnly = false;
    public static string epoch = "1";
    public static WindowsDriver<AppiumWebElement> windowsDriver;
    public static string installLocation = @"";
    public static int timeoutMultiplier = 1;
}

[When(@"I Select TreeNode ""(.*)"" In Tree ""(.*)""")]
public void WhenISelectTreeNodeInTree(string treeNode, string treeName)
{
    var proc = $"When I Select TreeNode {treeNode} In Tree {treeName}";

    if (CombinedSteps.OutPutProc(proc, OutputLevel.When))
    {
        if (Helpers.Tree.ClickOnTreeNodeInTree(treeNode, treeName, 3))
        {
            CombinedSteps.Success();
            return;
        }
    }

    CombinedSteps.Failure(proc);
}

[When(@"I Hold ""(.*)"" And Press ""(.*)"" For ""(.*)"" Times")]
public void WhenIHoldAndPress(string key1, string key2, string noOfTimes)
{
    var proc = $"When I Hold {key1} And Press {key2} For {noOfTimes} Times";

    if (CombinedSteps.OutPutProc(proc, OutputLevel.When))
    {
        var driver = Location.windowsDriver;
        var action = new Actions(driver);
        // initiate the key1 Hold
        switch (key1.ToLower())
        {
            case "shift":
                bool success = int.TryParse(noOfTimes, out int noOfTimesInt);
                if (success)
                {
                    action.KeyDown(Keys.Shift).Perform();
                    for (int i = noOfTimesInt; i > 0; i--) // keep hitting key2 until reaches 0
                    {
                        WhenIPressKey(key2);
                    }
                    action.KeyUp(Keys.Shift).Perform();             
                    CombinedSteps.Success();
                }
                else
                {                               
                    CombinedSteps.Failure();
                }
                return;
            default:
                CombinedSteps.Failure(proc);
                return;
        }
    }
}

[When(@"I Press ""(.*)"" Key")]
public void WhenIPressKey(string key)
{
    var proc = $"When I Press {key} key";

    if (CombinedSteps.OutPutProc(proc, OutputLevel.When))
    {
        var driver = Location.windowsDriver;
        var action = new Actions(driver);

        switch (key.ToLower())
        {
            case "alt-n":
                action.SendKeys(OpenQA.Selenium.Keys.Alt + "n").Perform();
                action.SendKeys(OpenQA.Selenium.Keys.Alt).Perform();
                CombinedSteps.Success();
                return;
            case "down arrow":
                action.SendKeys(OpenQA.Selenium.Keys.Down).Perform();
                CombinedSteps.Success();
                return;
            case "right arrow":
                action.SendKeys(OpenQA.Selenium.Keys.ArrowRight).Perform();
                CombinedSteps.Success();
                return;
            case "escape":
                action.SendKeys(OpenQA.Selenium.Keys.Escape).Perform();
                CombinedSteps.Success();
                return;
            case "return":
            case "enter":
                action.SendKeys(OpenQA.Selenium.Keys.Return).Perform();
                CombinedSteps.Success();
                return;
            case "tab":
                action.SendKeys(OpenQA.Selenium.Keys.Tab).Perform();
                CombinedSteps.Success();
                return;
            case "space":
                action.SendKeys(OpenQA.Selenium.Keys.Space).Perform();
                CombinedSteps.Success();
                return;
            default:
                CombinedSteps.Failure(proc);
                return;
        }
    }
}

[When(@"I Right Click On TreeNode ""(.*)"" In Tree ""(.*)""")]
public void WhenIRightClickOnTreeNodeInTree(string treeNode, string treeName)
{
    var proc = $"When I Right Click On TreeNode {treeNode} In Tree {treeName}";

    if (CombinedSteps.OutPutProc(proc, OutputLevel.When))
    {
        if (Helpers.Tree.RightClickOnNode(treeNode, treeName, 4))
        {
            CombinedSteps.Success();
            return;
        }
    }
    CombinedSteps.Failure(proc);
}

The annoying bit is that KeyDown Works but then when the test is finished looks as if the Shift key is still held down on my machine! i.e. if I go to click on an article on a website or click on some line on visual studio the whole lines of code ended got highlighted. Subsequently, when the same test is run for the second time the KeyDown Won't work

have looked up on how to use KeyDown and KeyUp and cannot see anything wrong with the code above... if anyone can help me it would be much much appreciated.

Note: I don't even know what this is and what it does?

public static WindowsDriver<AppiumWebElement> windowsDriver;

Solution

  • I have worked out how it is supposed to work now in my scenario. In my scenario, the following are now working when simulating Press Ctrl + f

    var driver = Location.windowsDriver;
    var action = new Actions(driver);
    
    switch (key.ToLower())
        {
            case "ctrl-f":
                action.KeyDown(OpenQA.Selenium.Keys.Control).Perform();
                action.SendKeys("f").Perform();
                action.KeyUp(OpenQA.Selenium.Keys.Control).Perform();
                CombinedSteps.Success();
                return;                    
                default:
                CombinedSteps.Failure(proc);
                return;
        }