首页 > 解决方案 > 用 Const 设计 JS

问题描述

我有以下代码。有没有更好的方法可以一次使用滚动选项并将其用于所有其他功能。我不确定如何将其重新设计为 const。

module.exports.xyz = function (browser, done) {
   browser
  .execute('window.scroll(0,2000)')
  .clickWaitForElementByCssWithCatch(css, isDisplayed, 30000, 1000)
  .nodeify(done);
};

module.exports.abc = function (browser, done) {
   browser
  .execute('window.scroll(0,2000)')
  .clickWaitForElementByCssWithCatch(css, isDisplayed, 30000, 1000)
  .nodeify(done);
};

module.exports.efg = function (browser, done) {
   browser
  .execute('window.scroll(0,2000)')
  .clickWaitForElementByCssWithCatch(css, isDisplayed, 30000, 1000)
  .nodeify(done);
};

标签: javascript

解决方案


字符串 ('window.scroll(0,2000)') 重复 3 次,这就是您从 Sonar 收到的投诉。

您应该在文件顶部定义一个变量:

var js_code = 'window.scroll(0,2000)';

然后将其用作参数:browser.execute(js_code)


推荐阅读