4

我在 Ubuntu 11.10 上运行的 Firefox 11 上使用 Selenium 1.7.2。直到昨天,我们还没有使用document.domainjavascript。我们需要将它添加到我们生成的 HTML 和我们的一个 javascript 文件中。现在,当我们运行 Selenium IDE 测试套件时,我们会收到以下错误:

Error: Permission denied for <http://dev.example.com> to get property Location.href

dev.example.com是我们的应用服务器(Glassfish 3.1.2 落后于 Apache+mod_jk)

如果我注释掉document.domain一切正常(至少在 Firefox 中,因为 document.domain 是为了防止 IE 阻止 PIE.htc 脚本...叹息

我尝试添加在这里找到的用户扩展脚本:

function setdom(str,doc,dom) {
  doc.domain = dom;
}

Selenium.prototype.doDocumentDomain = function(domain) {
  var lw;
  setdom('ts',frames['testSuiteFrame'].document, domain);
  setdom('tf', getTestFrame().contentWindow.document, domain);
  setdom('my', frames['myiframe'].document, domain);

  lw = LOG.getLogWindow();
  if (lw) {
    setdom('log', lw.document, domain);
    }
  setdom('doc', document, domain);
  }

但这看起来很旧,可能不再兼容。它在第一次调用该setdom('ts',frames['testSuiteFrame'].document,domain);行时返回一个错误

我没有在 HTTP 和 HTTPS 之间来回浏览,我已经阅读了许多 StackOverflow 和 Google Group 相关的问题,但没有结果。

我可以修改我们的代码以document.domain仅包含 IE,但它不是很干净......

问题:如何在设置 document.domain 时使 Selenium IDE 工作而没有安全问题?或者我怎样才能修复他上面的用户扩展以在 Selenium IDE 1.7.2 中工作?谢谢你。

4

1 回答 1

0

所以我决定更改 Selenium javascript 以允许我使用以下方法设置 document.domain:

在第 920 行的 `chrome/content/selenium-core/scripts/selenium-browserbot.js(适用于 1.7.2 版)中:

    //Samit: Fix: open command sometimes fails if current url is chrome and new is not
    windowObject = core.firefox.unwrap(windowObject);
    // -------------- Start My Change ----------------
    updateDomain(windowObject.document);
    // -------------- End My Change ----------------
    if (this._windowClosed(windowObject)) {
        LOG.debug("pollForLoad WINDOW CLOSED (" + marker + ")");
        delete this.pollingForLoad[marker];
        return;
    }

然后在 user-extensions.js 中:

var validDomain = null;

Selenium.prototype.doDocumentDomain = function(domain) {
  validDomain = domain;
}

function updateDomain(doc) {
  if(validDomain==null) {
    return;
    }
  LOG.info("Current domain: " + doc.domain);
  if(doc.domain != validDomain && (doc.domain+"").indexOf(validDomain)>0 ) {
    doc.domain = validDomain;
    }
  LOG.info("New domain: " + doc.domain);
  }

我在设置新域之前检查它是否是我要设置的域的子域。我在 Selenium IDE 中使用它:

documentDomain | example.com

所以当它打开 dev.example.com 和 static.example.com 时,它会example.com在域中找到并替换域。

于 2012-04-05T05:28:00.540 回答