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