Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium & webdriver.io how to use executeScript?

I'm trying to test a simple form using Selenium, WebDriver.io & Node.js (with Mocha). So I have something like this:

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

But in console I have: browser.executeScript is not a function. What is the right way to execute script in browser context using these tools?

like image 928
Igor Adamenko Avatar asked Mar 25 '16 22:03

Igor Adamenko


People also ask

What is Selenium mainly used for?

Selenium is an open-source tool that automates web browsers. It provides a single interface that lets you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others.

Is Selenium a Java or Python?

Selenium test automation framework can be done with a wide array of programming languages like Python, Java, C#, Ruby, etc.

What is the salary of a Selenium tester?

Selenium Automation Tester salary in India ranges between ₹ 4.0 Lakhs to ₹ 15.6 Lakhs with an average annual salary of ₹ 7.0 Lakhs.

Is Selenium a programming language?

Selenium is a widely used open source, portable software testing framework for web applications. Though Selenium comes with a test domain specific language (Selenese), other programming languages (Java, C#, Ruby, Python) can be used to script tests as well.


1 Answers

Okay, I was searching in sources and find /build/lib/protocol/execute.js. Example from there:

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

But now all commands in wdio are synchronous (proof issue). So the right way for me is:

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

expect(data.value).to.be([]);
/* note, here ^ is a property with value of execution */
like image 147
Igor Adamenko Avatar answered Oct 10 '22 14:10

Igor Adamenko