简体   繁体   English

ZombieJS - 如何调用 JavaScript 函数或检查 JavaScript 值?

[英]ZombieJS - How do I call JavaScript functions or check JavaScript values?

var Browser = require("zombie");

// Load the page from localhost
browser = new Browser()
browser.visit("http://localhost:3000/", function () {

  // Let's say the page has window.jQuery
  // How do I access it?

});

So, How do I access the window and the javascript variables?那么,如何访问 window 和 javascript 变量? I want to check if my script tracks 'clicks' correctly.我想检查我的脚本是否正确跟踪“点击”。 I want to do something like expect(browser.document.window.myScript.click_counter).toBe 5 .我想做一些类似expect(browser.document.window.myScript.click_counter).toBe 5的事情。

I got it working with the following... having set the maxWait options我让它与以下一起工作......设置了 maxWait 选项

var opts = {
    debug: false
    , runScripts: true
    , maxWait: 10
    , waitFor: 10000
};

this.browser = new Browser(opts);

this.browser.visit(url, callback);

this.browser.wait(function(window) {
    console.log('wait...');

    return window.document.querySelector("#objectid");
}, function(e, browser) {
    console.log('wait complete ', e) ;
});

However the second callback method never fires and the querySelector call always fails.但是,第二个callback方法永远不会触发,并且querySelector调用总是失败。 I don't think the window.document DOM is updated after the javascript injection has fired.我不认为window.document DOM 在 javascript 注入被触发后更新。

[edit] [编辑]

I removed the callback from browser.visit() to make the wait callback fire.我从browser.visit()中删除了回调以触发等待回调。

this.browser.visit(url);

It depends on where you stored your variable.这取决于您存储变量的位置。
If you stored it in a global object myScript , use:如果您将其存储在全局 object myScript中,请使用:

browser.window.myScript.click_counter

If you have something like myScript.click_counter = 5; document.myScript = {... }如果你有类似myScript.click_counter = 5; document.myScript = {... } myScript.click_counter = 5; document.myScript = {... } , use: myScript.click_counter = 5; document.myScript = {... } ,使用:

browser.document.myScript.click_counter

I needed to crank maxWait up to 100. Which is strange, because, presuming that's in ms, that's short, but in seconds, its insanely long.我需要将 maxWait 提高到 100。这很奇怪,因为假设以毫秒为单位,那很短,但以秒为单位,它却长得离谱。 Its not really clear what maxWait does, but it seemed to make a difference.它不是很清楚 maxWait 的作用,但它似乎有所作为。 Unfortunately, I'm using Behat/Mink with Zombie.js and I had to hack the source of the Mink driver to make it work, which is disappointing because I can't update that driver now without problems...不幸的是,我将 Behat/Mink 与 Zombie.js 一起使用,我不得不破解 Mink 驱动程序的源代码以使其工作,这令人失望,因为我现在无法毫无问题地更新该驱动程序......

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

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