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

 

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

Saturday, 9 September 2017

Python Reporting || allure Reports + pytest

Allure Framework is a flexible lightweight multi-language test report tool gives beautiful reports for any programming language

Sample Code:
Sample.py

def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5


Install required pytest, allure adaptor libraries via pip

pip install pytest
pip install pytest-allure-adaptor


First Step:
During test execution, a small library called adapter attached to the testing framework saves information about executed tests to XML files.

> pytest Sample.py -m alluredir G:\Dir1\

- Here xml files will be genarated.

Second Step:
During report generation, the XML files are transformed to a HTML report. This can be done with a command line tool.

Second Step/ Software setup:
download allure from here https://bintray.com/qameta/generic/allure2
unzip, and set Path variable to C:\Users\userName\Downloads\allure-2.3.5\bin;

open command window > allure --version
--o/p: 2.3.5

XML to HTML file conversion
open command window > allure serve G:\Dir1\

Thats all, Automatically allure report will be opened shown like below.








Python | pytest | Quick tutorail

Install Pytest:

open command window at

C:\Users\PK\AppData\Local\Programs\Python\Python36-32\Scripts

pytest --version

pip install -U pytest

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

Our first test:

GoogleTest.py

def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5

-------------------------------
Run:

open command window at C:\Users\PK\AppData\Local\Programs\Python\Python36-32\Scripts

pytest C:\Users\PK\PycharmProjects\allure-reports-with-pytest\tests\GoogleTest.py

or

>py.test -q -s GoogleTest.py