5

私はSelenium、WebDriver.io & Node.js(Mocha付き)を使って簡単なフォームをテストしようとしています。だから私はこのようなものを持っている: Selenium&webdriver.io executeScriptの使い方は?

var webdriverio = require('webdriverio'); 
var expect = require('expect'); 

describe('Test form', function(){ 
    beforeEach(function() { 
     browser.url('/'); 
    }); 

    it('should save object', function() { 
     expect(browser.executeScript('return window.data;')).to.be([]); 
    }); 

    afterEach(function() { 
     if (this.currentTest.state !== "passed") { 
      browser.saveScreenshot(); 
     } 
    }); 
}); 

マイ wdio.conf.js

var selenium = require('selenium-standalone'); 
var seleniumServer; 

exports.config = { 
    host: '127.0.0.1', 
    port: 4444, 

    specs: [ 
     'test/*.spec.js' 
    ], 

    capabilities: [{ 
     browserName: 'chrome' 
    }], 

    baseUrl: 'http://localhost:8080', 
    framework: 'mocha', 

    mochaOpts: { 
     ui: 'bdd' 
    }, 

    onPrepare: function() { 
     return new Promise((resolve, reject) => { 
      selenium.start((err, process) => { 
       if(err) { 
        return reject(err); 
       } 
       seleniumServer = process; 
       resolve(process); 
      }) 
     }); 
    }, 

    onComplete: function() { 
     seleniumServer.kill(); 
    } 
}; 

しかし、コンソールに私が持っている:browser.executeScript is not a functionを。これらのツールを使用してブラウザのコンテキストでスクリプトを実行する正しい方法は何ですか?

答えて

5

さて、ソースで検索したところ、/build/lib/protocol/execute.jsが見つかりました。そこからの例:

client.execute(function(a, b, c, d) { 
    // browser context - you may not access neither client nor console 
    return a + b + c + d; 
}, 1, 2, 3, 4).then(function(ret) { 
    // node.js context - client and console are available 
    console.log(ret.value); // outputs: 10 
}); 

しかし、wdioのすべてのコマンドは同期しています(proof issue)。ですから、私にとって正しい方法は:

var data = browser.execute(function() { 
    return window.data; 
}); 

expect(data.value).to.be([]); 
/* note, here^is a property with value of execution */ 
関連する問題