2

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;
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
bdrummond
  • 63
  • 6
  • So which one is giving you problems? Selenium? WinAppDriver? – Greg Burghardt Mar 02 '22 at 16:48
  • Can you [edit] your question to include all the step definitions in the scenario? I can't get a handle on the problem until I see all of them. – Greg Burghardt Mar 02 '22 at 16:55
  • hi @GregBurghardt - To answer both of your questions: I am pretty sure it's the selenium issue that giving me a problem. Secondly, I have edited the question above to include all the step definitions. Thank you for your interest in helping me. much appreciate it. – bdrummond Mar 03 '22 at 17:43
  • This is good information, thanks! – Greg Burghardt Mar 03 '22 at 18:26
  • I've been thinking about this question for a while now. Sometimes you just need to attempt to do something in Selenium. Wait for the expected result. If it doesn't happen, try to do the same thing over again. In this case, do the "keyup" event, then wait for some sort of UI change that should respond to it. If you don't get the desired response, perform the "keyup" event a second time before moving on. – Greg Burghardt Mar 11 '22 at 14:08
  • 1
    hey @GregBurghardt - sorry i only be able to reply now - I haven't tried and parking it for awhile. I will try later down the line. cheers. B – bdrummond Apr 05 '22 at 15:04

1 Answers1

0

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;
    }
bdrummond
  • 63
  • 6