Thursday, 30 November 2017

Tricky question, How can you verify sorting working as expected via selenium Automation.

Assume that we have a grid which is hosting students information, columns names ( id, name ) and wanted to verify sorting ( name ) is working or not.

I think, you know how to loop each row in the table to get student names.

here is the tricky part to verify, weather all the rows are in sorted order or not ( ye we can do it for asc/desc order )

in Java we have String compareTo() method, this helps us.

compareTo() : is used for comparing two strings lexicographical ( alpha Numerical order )


returns 0 ; if both are equals
returns +Number , means First is Greater than Second
returns -Number , means Second is Greater than First



public class CompareToExample {
   public static void main(String args[]) {
       String str1 = "String method tutorial";
       String str2 = "compareTo method example";
       String str3 = "String method tutorial";

       int var1 = str1.compareTo( str2 );
       System.out.println("str1 & str2 comparison: "+var1);

       int var2 = str1.compareTo( str3 );
       System.out.println("str1 & str3 comparison: "+var2);

       int var3 = str2.compareTo("compareTo method example");
       System.out.println("str2 & string argument comparison: "+var3);
   }
}

out put:

str1 & str2 comparison: -16
str1 & str3 comparison: 0
str2 & string argument comparison: 0

Tuesday, 21 November 2017

log4j

https://www.mkyong.com/logging/log4j-hello-world-example/

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

Sunday, 30 July 2017

Python | Verify test Util

Here is the code for VerifyTetsUtil in python

class VerifyTestUtil(object):
    def __init__(self):
        self.results = []
    def verify(self,condition,  message):
        if (condition==False):
            self.results.append(message)

    def addMessage(self,message):
        self.results.append(message)

    def getMessages(self):
        return self.results

==================================

Usage:

import urllib.request
import os
from selenium import webdriver
import unittest 
import HTMLTestRunner
from VerifyTestUtil import VerifyTestUtil

class Test1 (unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome("C:/forSelenium/chromedriver.exe")
        self.driver.get("https://www.amazon.com")
        print("testtttt")

    def test_login(self):
        verifyTestUtil = VerifyTestUtil()
        driver=self.driver
        verifyTestUtil.addMessage("msg1")
        self.assertEqual('bar', 'bar',"resultText")
        driver.find_element_by_id("twotabsearchtextbox").send_keys("toys and games")
        verifyTestUtil.addMessage("msg2")        
        driver.find_element_by_css_selector("input[value='Go']").click()
        resultText=driver.find_element_by_id("s-result-count").text
        c1=resultText.split("of")
        c2=c1[1].split("results")
        verifyTestUtil.addMessage("msg3")
        c3=c2[0].replace("results","").replace(",","").replace(" ","")
        results=verifyTestUtil.getMessages();
        for s in results:
            print(s)

    def tearDown(self):
        self.driver.quit()

if __name__=='__main__':
    HTMLTestRunner.main()