OpenQA.Selenium.Interactions - KeyUp 似乎在 KeyDown 操作后没有生效

发布于 2025-01-11 07:16:13 字数 5637 浏览 4 评论 0原文

我目前正在接受一项巨大的挑战,继承我们的测试自动化主管的测试自动化框架。到目前为止还可以。但我需要模拟按住 Shift 键并多次按下向下箭头,然后释放 Shift 键。

与 Visual Studio 2019 一起使用,使用 C#/WinAppDriver/Appium 中的 Specflow/Selenium 的框架来测试经典窗口应用程序。

我对这一切都很陌生,所以如果我的问题没有多大意义,请耐心等待。

BDD 语句:

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

所以它是一个树列表,包含 4 个节点,我想单击顶部节点,然后使用键盘操作突出显示所有节点 - 我右键单击其中一个节点,然后选择一个选项并按 Enter 键。

示例功能测试

更新@03/03/2022 - 上述功能中所有涉及的步骤定义。

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);
}

烦人的是 KeyDown 可以工作,但是当测试完成后,我的机器上似乎仍然按住 Shift 键!即,如果我点击网站上的一篇文章或点击 Visual Studio 上的某行,则整行代码都会突出显示。随后,当第二次运行相同的测试时,KeyDown 将无法工作

已查找如何使用 KeyDown 和 KeyUp,并且看不到上面的代码有任何问题...如果有人可以帮助我,那就太好了赞赏。

注意:我什至不知道这是什么以及它有什么作用?

public static WindowsDriver<AppiumWebElement> windowsDriver;

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 = 
quot;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 = 
quot;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 = 
quot;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 = 
quot;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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2025-01-18 07:16:13

我已经弄清楚它现在在我的场景中应该如何工作。 在我的场景中,当模拟按 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文