5

Tôi đang thử nghiệm một biểu mẫu đơn giản bằng cách sử dụng Selenium, WebDriver.io & Node.js (với Mocha). Vì vậy, tôi có một cái gì đó như thế này:Selenium & webdriver.io cách sử dụng 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(); 
     } 
    }); 
}); 

My 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(); 
    } 
}; 

Nhưng trong giao diện điều khiển tôi có: browser.executeScript is not a function. Cách đúng để thực thi kịch bản trong ngữ cảnh trình duyệt bằng cách sử dụng các công cụ này là gì?

Trả lời

5

OK, tôi đã tìm kiếm trong các nguồn và tìm thấy /build/lib/protocol/execute.js. Ví dụ từ đó:

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 
}); 

Nhưng bây giờ tất cả các lệnh trong wdio là đồng bộ (proof issue). Vì vậy, đúng cách cho tôi là:

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

expect(data.value).to.be([]); 
/* note, here^is a property with value of execution */ 
Các vấn đề liên quan