【问题标题】:I get an error while running an JS script with capybara/selenium/ruby使用 capybara/selenium/ruby 运行 JS 脚本时出现错误
【发布时间】:2017-09-18 03:37:21
【问题描述】:

我是自动化新手,我正在使用 Selenium、ruby、capybara 来执行这个 JS 脚本,我收到了这个错误消息,感谢 TIA 的任何帮助 错误信息 Selenium::WebDriver::Error::UnknownError: unknown error: Runtime.evaluate throw exception: SyntaxError: Invalid or unexpected token

page.execute_script('(function() {

function renderField($el, mode) {
  var limitMet,
      field = $el.data(\'add-field\'),
      section = $el.data(\'section\');

  window.DADI.editor.freeSections.forEach(function(freeSection) {
    if (section === freeSection.name) {
        freeSection.fields.forEach(function(sectionField) {
          if(field === sectionField.source) {
              var count = $(\'#section-\' + section).find(\'[data-field="\'+field+\'"]\').length;
          if (sectionField.max && count >= sectionField.max) {
              limitMet = true;
          }
          }
          }.bind(this))
          }
          }.bind(this))

          if (!limitMet) {

              if (!window.DADI.editor.types[field]) return false;

              var template = window.DADI.editor.types[field]._local.layouts.article[0].replace(\'.dust\', \'\');
              var html;
              if (template) {
                  var templateData = window.DADI.editor.types[field];
              templateData.params = window.DADI.editor.params,
                  templateData.free = true;
              templateData.fieldName = field;

              window.DADI.render.render(\'fields/\' + template, \'#section-\' + section, templateData, {mode: mode}, function (err, out) {


                if (err) {
                    html = err;
                } else {
                    html = $(out);

                if ($(\'.selectize\', html) && $(\'.selectize\', html).length) {
                    dadiSelect($(\'.selectize\', html));
                }

                if (html.attr(\'data-ql-editable\')) {
                    createEditor(html, 0);
                html.focus();
                }

                var fieldType = window.DADI.editor.types[field]._remote._publishType;
                var handler = window.DADI.editor.handlers[fieldType];

                if (handler && (typeof handler.initialiseField === \'function\')) {
                    handler.initialiseField(html);
                }
                }
                });

                return html;
                }
                }
                }

                # This takes the button (source) element and drops it into the target area and subsequently renders the appropriate cms fields.
                function simulateDragAndDrop(source, target)
                {
                    var $clone = source.clone();
                $(target).prepend($clone, target);
                if ($clone.hasClass(\'dadiCells-library__element\')) {
                    renderedHtml = renderField($clone, \'none\');
                $clone.replaceWith(renderedHtml);
                }
                }

                var source = $($(\'.dadiCells-library__element\')[1]); #E.g. The hero video button
                var target = $(\'#section-hero\'); #The target drop zone.
                    simulateDragAndDrop(source, target);

                })()')

【问题讨论】:

  • '#' 对 JS 中的 cmets 无效,因此这可能是导致错误的原因,但是尝试将这么多 JS 与 execute_script 一起使用确实滥用了主要为小型设计的方法一个衬垫。在测试中使用这么多 JS 往往表明您并没有真正测试您的应用程序。
  • 谢谢它确实修复了 pbm,Thomas,它确实有帮助! Capybara/Selenium webdriver 的正常拖放也不起作用,因此必须与 JS 一起使用,但感谢您的帮助!

标签: javascript ruby selenium-webdriver capybara


【解决方案1】:

我遇到了类似的问题。解决我的问题是将单反斜杠/ 替换为双反斜杠//

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 2019-05-12
    • 1970-01-01
    • 2018-01-30
    • 2023-04-03
    • 2017-02-05
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多