I am not going to explain what is data driven framework or what is its benefits.  All of them are pretty well-known . If not, just google it.

Here I am going to explain a sample code which can be used to read from xml data files. This will be helpful to implement a data driven frame work for BDD testing , using Specflow or Cucumber

Pre - requiste

Code is written in Csharp . We need to add below reference to visual studio solution

  1. Add reference to System.xml

  2. Add reference to System.Xml.Linq

XML Format

[code language=“xml” ]

[/code]

Node names in above example are Scenario1, Scenario2 and Scenario3

Element or Attribute name are username, password, Email .

You can add any number of nodes and attributes depending on the test scenario

Read specific Value from XML

Below code provides a solution to read values of existing Key/Attribute from a specific node.

1
2
3
4
5
6
7
8
public static string ReadDataFromXML(string FileName, string NodeName, string KeyName)
{
 string _basePath = AppDomain.CurrentDomain.BaseDirectory.ToString();
string _datafilePath = _basePath + @"..\..\Data\" + FileName;
XDocument xmlDoc = XDocument.Load(_datafilePath);
data = xmlDoc.Root.Element(NodeName).Attribute(KeyName).Value;
return data;
}

 

Read All values for a Scenario from XML

This code gives a solution for reading details of all attributes/key from a node. This comes very handy for reading all data required for a scenario and adding them to scenario context , so that data can be shared across specflow/cucumber step definitions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void ReadAllDataFromXml(string xFileName, string xNodeName)
{
 string data = string.Empty;
// Load path of xml file . Data Folder in below is folder name
string _basePath = AppDomain.CurrentDomain.BaseDirectory.ToString();
string _datafilePath = _basePath + @"..\..\Data\" + xFileName;
XDocument xmlDoc = XDocument.Load(_datafilePath);

var cols = xmlDoc.Descendants(xNodeName).First();

foreach (XAttribute xAtt in cols.Attributes())
{
Console.WriteLine("{0},{1}", xAtt.Name, xAtt.Value); // --printing values --
string temp = xAtt.Name.ToString();
ScenarioContext.Current.Add(temp, xAtt.Value);
// The values are added to scenario context as key value pair.can be modified
}

Write Data into XML

  Below function gives a solution to update value on an existing key/attribute in data sheet . Sometime the data sheet will be copied over to bin folder when we build the solution. That makes it necessary to update both original data sheet and the one in bin folder  so that modified data can be used in same test without another rebuild.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void WriteIntoXML(string xData, string xElement, string xAttribute, string xFileName)
{
 // This solution updates value of an existing key.
 // Xdoc refers to the xmlsheet in the Solution explorer
 // xDoc_2 refers to the xmlsheet inside bin folder while running

 // This solution write them separately in below code.
 // Below is the path to xml file after building solution
 XDocument xDoc_2 = XDocument.Load(Path.Combine(Environment.CurrentDirectory, "Data Folder", xFileName));

 //Below should be the path of xml file
 XDocument xDoc = XDocument.Load(Path.Combine(Path.GetFullPath(@"../../Data Folder"), xFileName));
 xDoc_2.Root.Element(xElement).Attribute(xAttribute).Value = data.ToString();
 xDoc.Root.Element(xElement).Attribute(xAttribute).Value = data.ToString();
 xDoc_2.Save(Path.Combine(Environment.CurrentDirectory, "Data Folder", xFileName));
 xDoc.Save(Path.Combine(Path.GetFullPath(@"../../Data Folder"), xFileName));
}

In previous blog post, I have explained about how use XML for making a data driven framework for automation testing . It can be found here. I have also written about how to use jxl library for reading from excel and writing into Excel.

Below is another code snippet to read all values of a row and save it into a hash map for accessing later during automation test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public  HashMap GetAllDataForARow  (String sheet, int Row){
        HashMap DataMap = new HashMap();
        try {
            Workbook wrk1 =  Workbook.getWorkbook(new File(dataPath));

            //Obtain the reference to the first sheet in the workbook
            Sheet sheet1 = wrk1.getSheet(sheet);
            int x =0;


            Cell colArow1 , colArow2;
            do {
                colArow1 = sheet1.getCell(x,0);
                colArow2 = sheet1.getCell(x,Row);
                DataMap.put(colArow1.getContents(), colArow2.getContents());
                x=x+1;
            }while (colArow1.getContents() != "");

        }

        catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (IndexOutOfBoundsException e){
        }
        return DataMap;

    }

This post is just for making notes during my learning of Node.js through various courses and online material. This will be always a work in progress blog post

Node.js is an open source server side runtime environment, which is cross platform.It uses Javascript as its language

check node version node --version

Making web request in Node we can make webrequest by using inbuilt http or by using ‘request’ example : For making web request by http

1
2
3
4
5
6
var http = require('http');
var req = http.request('http://www.google.com/finance/info?infotype=infoquoteall&q=NSE:TCS', function(response) {
    console.log(response.statusCode);
    response.pipe(process.stdout);
});
req.end();

example: Please note that there is no space between key and : while defining options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var http = require('http');

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/finance/info?infotype=infoquoteall&q=NSE:TCS',
  method: 'GET'
};

var req = http.request(options, function(response) {
    console.log(response.statusCode);
    response.pipe(process.stdout);
});
req.end();

Example: We can simply by giving GET . There is no need to close request since we are not going to send any more information to request

1
2
3
4
5
6
7
8
9
10
11
12
13
var http = require('http');

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/finance/info?infotype=infoquoteall&q=NSE:TCS',
  method: 'GET'
};

http.get(options, function(response) {
    console.log(response.statusCode);
    response.pipe(process.stdout);
});

Another option is

1
2
3
4
5
6
7
8
9
10
11
12
13
http.get(options, function(res){
    var body = '';

    res.on('data', function(chunk){
        body += chunk;
    });

    res.on('end', function(){
        console.log("Got a response: ", body);
    });
}).on('error', function(e){
      console.log("Got an error: ", e);
});

Starting with Node.js and Express

1
npm init

Above command will create a package.json. Leave details are default or change accordingly

1
npm install express --save

Above will install express and also add it as dependency in package.json

1
touch app.js

Above will create a app.js file. In package.json add below

1
2
3
4
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },

Now, if we run “npm start” from command line, it will run app.js

Bower

Package manager for web/front end. It is installed with NPM and have flat package hierarchy(doesnt install dependency underneath one level) .It works similar to NPM and have Bower.json for dependency managament.

Create a .bowerrc file and have project specific settings. Now move the components from bower_component to public folder defined earlier. update .bowerrc with below

1
2
3
  {
      "directory" : "public/lib"
  }

now run below

1
2
3
4
  bower init
  \\ leave all settings as default
  bower install --save bootstrap
  \\ it will install bootstrap. It create a directory under public/lib and under that it will have bootstrap andjquery since jquery is a dependency for bootstrap

Gulp

It is a task manager for web projects. It have code based config. It is packaged base so that we can use different external packages

Mongo DB

  • Install MongoDB from website
  • mongoD is command for running server
  • mongo is command for running another terminal for interacting with mongo db
  • show dbs will show list of db
  • Install MongoDB Node.js driver using NPM ```

A couple of months back , I helped out to organize clinical examination for RACP. I created a tool ( Excel Macro) for finalizing exam schedules for all attendees. The roster should consider employee preference, examiner availability, timeslot, venue and other parameters. Below is what I got in return ( even though they misspelled my name).

Difference between / and // in XPath

/ is used to create absolute XPath which starts from root. This is highly brittle since any changes to UI will change element locator.

// is used to create relative XPath . The XPath is created relative to another element in UI

Difference between driver.get() and driver.navigate().to()

Both driver.get() and driver.navigate().to() will try to open a webpage and wait for the page to load. It means, it will wait till Onload event has fired. But it will not wait for all AJAX calls to trigger and process. Both of them are essentially the same. How ever Navigate() interface exposes the ability to move backward and forward in browser history.

1
2
3
4
driver.get("http://www.google.com");
driver.navigate().to("http://www.yahoo.com");
driver.navigate().forward();
driver.navigate().back();

Difference between driver.close() and driver.quit()

driver.close() will close the window which is currently accessed by webdriver. driver.quit() will close all windows that are opened by the webdriver during current execution.

In my previous blog post , I have mentioned how to read from an excel file using jxl jar files in Java. It can be found here

In this post, I will explain how to write into an excel using same library. Below example will update the excel cell content with the value passed and also update its formatting . The color of the cell will change depending on value we pass. We can use similar functions for updating any other cell format.

Below is the import section

1
2
3
4
5
6
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.*;

Below is the function for writing into excel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public  void WriteDataIntoExcelCell (String sheet, String field_name, int Row, String input){
    try {

        Workbook wrk1 =  Workbook.getWorkbook(new File(dataPath));

        //Obtain the reference to the first sheet in the workbook
        Sheet sheet1 = wrk1.getSheet(sheet);
        int x =0;
        int y =0;
        int Col=0;
      // Find Column number from excel by iteration first row and comparing the names
        Cell colArow1 = sheet1.getCell(x,y);
        do {

            colArow1 = sheet1.getCell(x,y);
            if (colArow1.getContents().equalsIgnoreCase(field_name) ){
                Col = colArow1.getColumn();
                break;
            }
            x=x+1;

        }while (colArow1.getContents() != "");
      // write to file
        File exlFile = new File(dataPath);
        WritableWorkbook writableWorkbook = Workbook.createWorkbook(exlFile,wrk1);
        WritableSheet writableSheet = writableWorkbook.getSheet(sheet);
        //WritableCellFormat writableCell = writableWorkbook.getSheet(sheet).
      // Update cell content and format
        String Varcolour ;
        Label label;
        if (input.equalsIgnoreCase("PASS")){
            label = new Label(Col,Row,input,getCellFormat(Colour.GREEN));
        }
        else if (input.equalsIgnoreCase("FAIL"))
        {
            label = new Label(Col,Row,input,getCellFormat(Colour.RED));
        }
        else {
            label = new Label(Col,Row,input);
        }
        //Label label = new Label(Col,Row,input);

        writableSheet.addCell(label);

        writableWorkbook.write();
        writableWorkbook.close();


    }

    catch (BiffException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (WriteException e) {
        e.printStackTrace();
    }



}


private static WritableCellFormat getCellFormat(Colour colour) throws WriteException {
    WritableFont cellFont = new WritableFont(WritableFont.TAHOMA, 10);
    WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
    cellFormat.setBackground(colour);
    return cellFormat;
}

Below is a code snippet for reading a specific cell from Excel using Java.

It is done by using importing jxl jar files which can be found here.

Import below in class file

1
2
3
4
5
6
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.*;

Below is function for reading value from specific cell in Excel

Read From Excel based on Row and coulmn Number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 public  String readexcel(String sheet, int intRow, int intCol){
        try {

            //Create a workbook object from the file at specified location.
            //Change the path of the file as per the location on your computer.

            Workbook wrk1 =  Workbook.getWorkbook(new File(dataPath));

            //Obtain the reference to the first sheet in the workbook
            Sheet sheet1 = wrk1.getSheet(sheet);
            //Obtain reference to the Cell using getCell(int col, int row) method of sheet
          // Add " - 1" to both intRow , intCol depending how whether we consider excel start with row & column number as 0 or 1
            Cell colArow1 = sheet1.getCell(intRow , intCol );


            //Read the contents of the Cell using getContents() method, which will return
            //it as a String
            String strReturn = colArow1.getContents();

            return strReturn;

         /*  //Display the cell contents
           System.out.println("Contents of cell Col A Row 1: \""+str_colArow1 + "\"");
           System.out.println("Contents of cell Col B Row 1: \""+str_colBrow1 + "\"");
           System.out.println("Contents of cell Col A Row 2: \""+str_colArow2 + "\"");*/


        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sheet;
    }

Below is an example of how to read from a cell based on row number and Column NAME

Read From Excel Based On Column Name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 public  String GetDataBasedOnRowNumAndColName  (String sheet, String field_name, int Row){
        try {
            Workbook wrk1 =  Workbook.getWorkbook(new File(dataPath));

            //Obtain the reference to the first sheet in the workbook
            Sheet sheet1 = wrk1.getSheet(sheet);
            int x =0;
            int y =0;
            int Col=0;
            boolean FOUND = false;
           // Find corresponding Column number based on Name
            Cell colArow1 = sheet1.getCell(x,y);
            do {

                colArow1 = sheet1.getCell(x,y);
                if (colArow1.getContents().equalsIgnoreCase(field_name) ){
                    Col = colArow1.getColumn();
                    // System.out.println(Col);
                    FOUND = true;
                    break;
                }
                x=x+1;

            }while (colArow1.getContents() != "");
            if (FOUND)
            {
                colArow1 = sheet1.getCell(Col,Row-1);
                // System.out.println(colArow1.getContents());
                return colArow1.getContents();
            }
            else
            {
                return " ";
            }
        }

        catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (IndexOutOfBoundsException e){
            return "";
        }

        return sheet;
    }