简体   繁体   English

如何在Protractor中以编程方式设置下载路径?

[英]How to programmatically set download path in Protractor?

I am trying to dynamically set the Chrome download path in conf.js for Protractor. 我想在Protractor的conf.js中动态设置Chrome下载路径。 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. 我们的webservice导出一个文件,我正在编写一个测试,需要知道下载文件的位置才能验证它。 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函数中的getCapabilities()函数获取用户数据目录,然后将其设置为下载目录,如下所示:

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

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. 这将允许代码是动态的,但getCapabilities返回一个promise,因此上面的代码不会起作用,因为conf.js将在promise解析之前完成构造config对象。 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. 在getCapabilities上使用then函数没有用,因为我无法在then函数中构造配置对象的能力部分。 I can't call getCapabilities outside of the onPrepare function because conf.js itself doesn't have context for it. 我无法在onPrepare函数之外调用getCapabilities,因为conf.js本身没有上下文。 Setting a network path is also not feasible for the team's setup. 对于团队的设置,设置网络路径也是不可行的。

Has anyone else tackled something like this? 还有其他人解决过这样的问题吗? Is there any other way to programmatically set the download path for Chrome? 有没有其他方法以编程方式设置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. 如果用户足够精明,可以知道,查找或存储该信息。


Edit: 编辑:

You might be able to use .execute() to force the browser.getCapabilities(); 您可以使用.execute()强制执行browser.getCapabilities(); to fulfill before moving to the next statement. 在转到下一个声明之前完成。

Normally called by protractor.promise.controlFlow().execute( myPromise ) 通常由protractor.promise.controlFlow().execute( myPromise )

However, I think execute() also returns a promise, so you might be back to square one with this idea. 但是,我认为execute()也会返回一个promise,所以你可能会回到原点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM