在端到端测试中使用跨平台键盘快捷键 [英] Using cross-platform keyboard shortcuts in end-to-end testing

查看:190
本文介绍了在端到端测试中使用跨平台键盘快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在我们相当大的测试代码库中,我们使用了不同的键盘快捷键。例如,要复制所选文本,我们使用 CTRL / COMMAND + C ,粘贴 CTRL / COMMAND + v ,打开一个新选项卡 CTRL / COMMAND + T 等。

In our rather big test codebase, we are using different keyboard shortcuts. For instance, to copy selected text we are using CTRL/COMMAND + C, to paste CTRL/COMMAND + v, to open a new tab CTRL/COMMAND + T etc.

为了让测试在多个平台上运行,我们想自动生成 CTRL vs COMMAND 选项,具体取决于目标浏览器在哪个平台上运行的。为了确定目标平台,我们目前正在使用以下帮助函数,该函数使用 navigator.appVersion

To keep tests work on multiple platforms, we'd like to make the CTRL vs COMMAND choice automatic depending on what platform the target browser is running on. To determine a target platform, we are currently using the following helper function which uses navigator.appVersion:

this.getControlKey = function () {
    return browser.executeScript("return navigator.appVersion.indexOf('Mac');").then(function (isMac) {
        return isMac ? protractor.Key.COMMAND : protractor.Key.CONTROL;
    });
};

这种方法的问题是 getControlKey()返回一个promise,每次我们使用它时,我们都必须明确地解决这个问题:

The problem with this approach is that getControlKey() returns a promise and, every time we use it, we have to resolve the promise explicitly:

helpers.getControlKey().then(function (controlKey) {
    elm.sendKeys(protractor.Key.chord(controlKey, "c"));
});

问题:


  1. 是否可以避免嵌套并简化 getControlKey()的使用?理想情况下,我希望它的工作简单:

  1. Is it possible to avoid the nestedness and simplify the use of getControlKey()? Ideally I would like it work as simple as:

elm.sendKeys(protractor.Key.chord(helpers.getControlKey(), "c"));


  • 使用 navigator.appVersion 确定目标平台的最佳方法,还有更好的方法吗?

  • Is using navigator.appVersion the best approach to determine a target platform, and is there a better way?


    推荐答案

    这是我最好的回答你的第一个问题,对于这两种情况 -

    Here's my best to answer your first question, for the two scenarios -


    • 如果多个测试人员在其中运行脚本自己的机器,辅助方法可以放在 onPrepare()函数中,将值赋给一个常量全局变量,该变量可用于所有测试。

    • If multiple testers run the scripts in their own machine, the helper method can be placed in onPrepare() function assigning the value to a constant global variable, which will be available for all the tests.

    如果所有测试都在分布式平台上运行,其中所有测试都随机分配给不同的机器,那么编写辅助方法将值分配给一个常量局部变量在中测试beforeAll()函数将非常有用。

    If all the tests are run on a distributed platform where all tests are randomly assigned to different machines, in that case writing the helper method assigning the value to a constant local variable for that test in beforeAll() function will be useful.

    转到第二个问题,我们还可以通过另一种方式获得正在执行测试规范的平台d使用量角器的 getCapabilities() 方法。

    Moving to your second question, there is also another way where we can get the platform on which the test spec is being executed using protractor's getCapabilities() method.

    获取平台类型的代码 -

    //Below code can be placed either in `onPrepare()` function or `beforeAll()` function depending the need.
    //If the below code is placed in the `beforeAll()` function then i guess there won't be any need for a global variable.
    
    browser.controlKey = protractor.Key.CONTROL; //browser.controlKey is a global variable and can be accessed anywhere in the test specs
    browser.getCapabilities().then(function(capabilities){
        if(capabilities.caps_.platform === "MAC")
            browser.controlKey = protractor.Key.COMMAND;
    });
    

    用法:

    elm.sendKeys(protractor.Key.chord(browser.controlKey, "c")); //if its stored as global variable
    

    希望它有所帮助。

    这篇关于在端到端测试中使用跨平台键盘快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆