如何在量角器中以编程方式设置下载路径? [英] How to programmatically set download path in Protractor?

查看:47
本文介绍了如何在量角器中以编程方式设置下载路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 conf.js 中为 Protractor 动态设置 Chrome 下载路径.我们的网络服务导出一个文件,我正在编写一个测试,需要知道下载文件的位置以便验证它.现在,我很难设置浏览器的下载路径.问题是我团队的其他成员和构建机器也将运行这个测试,我没有可以选择的单一下载路径可以在每台开发和构建机器上运行.在做了一些阅读之后,我认为最好的解决方案是从 onPrepare 函数内的 getCapabilities() 函数获取用户数据目录,然后将其设置为下载目录,如下所示:

I am trying to dynamically set the Chrome download path in conf.js for Protractor. Our webservice exports a file and I am writing a test that needs to know where the download file is going to be in order to verify it. Right now, I am hard setting the browser's download path. The problem is that other members of my team and the build machines will also be running this test and there is no single download path I could choose that would work on every dev and build machine. After doing some reading, I thought the best solution would be get the user data directory from the getCapabilities() function inside the onPrepare function, and then set that as the download directory, like so:

onPrepare: function () {
    var jasmineReporters = require('jasmine-reporters');
    jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter( { 
        savePath: 'reports', consolidateAll: true } ));
    var cap = browser.getCapabilities();
    // Set prefsDict using cap.caps_.userDataDir
},
capabilities: {
    'chromeOptions': {
        'prefs': prefsDict
    }
}

这将允许代码是动态的,但 getCapabilities 返回一个承诺,因此上面的代码将无法工作,因为 conf.js 将在承诺解析之前完成配置对象的构造.在 getCapabilities 上使用 then 函数无济于事,因为我无法在 then 函数中构造我的配置对象的功能部分.我不能在 onPrepare 函数之外调用 getCapabilities,因为 conf.js 本身没有上下文.设置网络路径对于团队的设置也是不可行的.

This would allow the code to be dynamic, but getCapabilities returns a promise, so the code above is not going to work because conf.js will finish constructing the config object before the promise resolves. Using a then function on getCapabilities doesn't help because I can't construct the capabilities section of my config object in the then function. I can't call getCapabilities outside of the onPrepare function because conf.js itself doesn't have context for it. Setting a network path is also not feasible for the team's setup.

有没有其他人解决过这样的问题?有没有其他方法可以以编程方式为 Chrome 设置下载路径?

Has anyone else tackled something like this? Is there any other way to programmatically set the download path for Chrome?

推荐答案

这可能无法以您想要的方式解决问题,但您可以运行

This probably doesn't solve the issue in the way you'd like, but you could run

protractor conf.js --capabilities.chromeOptions.prefs 'path/to/user/folder'

如果用户足够了解、查找或存储该信息.

if the user is savvy enough to know, find, or store that information.

您也许可以使用 .execute() 强制 browser.getCapabilities(); 在移动到下一个语句之前完成.

You might be able to use .execute() to force the browser.getCapabilities(); to fulfill before moving to the next statement.

通常由 protractor.promise.controlFlow().execute( myPromise )

然而,我认为 execute() 也会返回一个承诺,所以你可能会回到这个想法的原点.

However, I think execute() also returns a promise, so you might be back to square one with this idea.

这篇关于如何在量角器中以编程方式设置下载路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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