【发布时间】: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