Wednesday 20 September 2017

WebdriverJs - Sample programme | Mocha | chai | Javascript

Here is the selenium web automaton sample code , using java script bindings.


Software Setup:
1.Install Node.js & npm
 Node.js from https://nodejs.org/en/download/ ( latest version and please install it )
- Node installs npm software by deault

verification: open command window and type
> node -v
7.2.1
>npm -v
3.10.10

2. Installing selenium :
Create a folder called "webdriver-js" any where and open a command window from the newly cerated folder "webdriver-js" and install selenium

>npm install --save--dev selenium-webdriver


3. install testing framework chai and mocha ( globally )
>npm install chai mocha -g


4. Download chromedriver.exe 
download and place the chromedriver.exe file here ( webdriver-js folder )
https://sites.google.com/a/chromium.org/chromedriver/downloads





5. Sample Script ( save as 'test-with-assert.js' in the same folder)


-------------------------------------


// Require chai.js expect module for assertions
var chai = require('chai');
var assert = require('chai').assert;

// URL
var url = 'http://www.google.com';

// Official selenium webdriver testing setup
var selenium = require('selenium-webdriver');

describe('Google.com Application test suite', function () {
    var driver;
    beforeEach(function(){
        // Start of test use this
        driver = new selenium.Builder().
        withCapabilities(selenium.Capabilities.chrome()).
        build();
        console.log("Selenium Webdriver Chrome Started");
    });

it('test#1 : verify google.com title', function (done) {      
        driver.get(url);
this.timeout(50000);
driver.getTitle().then(function(title) {
            assert.equal(title,'Google');
done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })

    });

it('test#2 : verify google.com title when we searched for a text', function (done) {
        driver.get(url);
driver.findElement(selenium.By.name('q')).sendKeys("selenium");
driver.findElement(selenium.By.name('btnK')).click();
this.timeout(50000);
        driver.getTitle().then(function(title) {
            assert.equal(title,'selenium - Google Search');
            done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })
    });

it('test#3 : verify that page is navigated to gmail on gmail link clicked', function (done) {
        driver.get(url);
driver.findElement(selenium.By.linkText('Gmail')).click();
this.timeout(50000);
        driver.getTitle().then(function(title) {
            assert.equal(title,'Gmail');
            done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })
    });

    afterEach(function(){      
        driver.quit();
console.log("Selenium Webdriver Chrome Stopped");
    });

 
});



-------------------------------------



6.Run
>mocha test-with-assert.js

That's all .




with different way assertions : expect

------------------------
// Require chai.js expect module for assertions
var chai = require('chai');
var expect = require('chai').expect;

// URL
var url = 'http://www.google.com';

// Official selenium webdriver testing setup
var selenium = require('selenium-webdriver');

describe('Google.com Application test suite', function () {
    var driver;
    beforeEach(function(){
        // Start of test use this
        driver = new selenium.Builder().
        withCapabilities(selenium.Capabilities.chrome()).
        build();
        console.log("Selenium Webdriver Chrome Started");
    });

it('test#1 : verify google.com title', function (done) {
        driver.get(url);
        this.timeout(50000);
        driver.getTitle().then(function(title) {
            expect(title).to.equal('Google');
            done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })
    });

it('test#2 : verify google.com title when we searched for a text', function (done) {
        driver.get(url);
driver.findElement(selenium.By.name('q')).sendKeys("selenium");
driver.findElement(selenium.By.name('btnK')).click();
this.timeout(50000);
        driver.getTitle().then(function(title) {
            expect(title).to.equal('selenium - Google Search');
            done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })
    });

it('test#3 : verify that page is navigated to gmail on gmail link clicked', function (done) {
        driver.get(url);
driver.findElement(selenium.By.linkText('Gmail')).click();
this.timeout(50000);
        driver.getTitle().then(function(title) {
            expect(title).to.equal('Gmail');
            done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })
    });

    afterEach(function(){      
        driver.quit();
console.log("Selenium Webdriver Chrome Stopped");
    });

 

});
-----------------------------------