Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Saturday, 13 August 2016

Cross Browser Testing using Selenium WebDriver


In this post I am going to explain how can we do Cross Browser Testing using Selenium.

If we are using Selenium WebDriver,In order to execute test cases using different browsers like Firefox, Chrome, Opera ,Internet Explorer we have to use TestNG framework.

Test.xml :
In TestNG, if we want execute same test case with different browsers we have to use Test.xml file.
Sample Code:
<suite name="CrossBrowserTesting" preserve-order="true" parallel="false" >
   
<test name="Smoketest" preserve-order="true">

<parameter name="browserName" value="firefox"></parameter>
<!-- 
<parameter name="browserName" value="ie"></parameter>
<parameter name="browserName" value="chrome"></parameter>
<parameter name="browserName" value="opera"></parameter>
-->
<classes>

<class name="Configuration.HMSLogin"></class> 
 
</classes>

</test>

</suite>

BrowserType.java
We have to pass "browserName" parameters from Test.xml and it will map with the BrowserType.java using @Parameters  annotation.
And the BrowserType.java will look like below
Sample Code:
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class BrowserType {

public static WebDriver driver;
@BeforeClass
@Parameters("browserName")
public void setup(String browserName) throws IOException, InterruptedException{

//open Firefox Browser 
//open the browser based on parameter passed in .xml file.
if(browserName.equalsIgnoreCase("firefox")){

//if you are using selenium 2.0
driver = new FirefoxDriver();

//if you are using selenium 3.0
//System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
//driver = new FirefoxDriver();
}
else if(browserName.equalsIgnoreCase("chrome")){

//open chrome browser
System.setProperty("webdriver.chrome.driver", "E:/Selenium Training/Driver Files/chromedriver.exe");
driver= new ChromeDriver();
}

else if(browserName.equalsIgnoreCase("ie")){
//open Internet Explorer Browser
System.setProperty("webdriver.ie.driver", "E:/Selenium Training/Driver Files/IEDriverServer.exe");
driver= new InternetExplorerDriver();

}
else if(browserName.equalsIgnoreCase("opera")){
//open opera Browser
System.setProperty("webdriver.opera.driver", "E:/Selenium Training/Driver Files/operadriver.exe");
driver= new OperaDriver();

}
//maximizing window
driver.manage().window().maximize();

//Navigating URL
driver.get("http://www.way2selenium.com/");

}
}

Testcase:
We have to extends BrowserType class to each test-suite and it will look like below
Sample Testcase code:
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class HMSLogin extends BrowserType {
@Test(priority=1)
public void Login() {

driver.findElement(By.xpath("//input[@name='username']")).clear();
driver.findElement(By.xpath("//input[@name='username']")).sendKeys("admin");

driver.findElement(By.xpath("//input[@name='password']")).clear();
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("admin");

driver.findElement(By.xpath("//input[@name='submit']")).click();

}

}


finally execute the Test-suite by using Test.xml file.



Friday, 12 August 2016

Basic Excel Reader useful for selenium WebDriver


In this post I am going to explain how can we read and write data on excel file with the help of Java using Apache POI jar libraries.

 Download Apache POI jar libraries in following link  and configure to your project
 Download Apache POI jar

Sample Code:

       package DriverMethods;

       import java.io.FileInputStream;
       import java.io.FileNotFoundException;
       import java.io.FileOutputStream;
       import java.io.IOException;
       import org.apache.poi.ss.usermodel.Cell;
       import org.apache.poi.xssf.usermodel.XSSFCell;
       import org.apache.poi.xssf.usermodel.XSSFRow;
       import org.apache.poi.xssf.usermodel.XSSFSheet;
       import org.apache.poi.xssf.usermodel.XSSFWorkbook;

        public class ExcelReader {
public String path;
public FileInputStream file;
public XSSFWorkbook workbook;
public XSSFSheet sheet;
public XSSFRow row;
public XSSFCell cell;
       public FileOutputStream fos;

       //constructor for path setting
public ExcelReader(String path) throws IOException{

this.path =path;
file = new FileInputStream(path);
workbook= new XSSFWorkbook(file);
}
//Reusable method for reading data from excel
public  String getCellData(String sheetName,int rowNum,int colNum){
int index = workbook.getSheetIndex(sheetName);
sheet =workbook.getSheetAt(index);
row = sheet.getRow(rowNum);
cell = row.getCell(colNum);
String data=cell.getStringCellValue();
return data;
}

//Reusable method for writing data to the excel
    public void setCellData(String sheetName,String data,int rowNum,int colNum) throws                IOException{

   int index = workbook.getSheetIndex(sheetName);
sheet =workbook.getSheetAt(index);
row = sheet.getRow(rowNum);

if(row==null){

row =sheet.createRow(rowNum);
}
cell = row.getCell(colNum);
if(cell==null){
cell= row.createCell(colNum);
}
  cell.setCellValue(data);

fos = new FileOutputStream(path);
workbook.write(fos);
fos.close();
   }
 
     //Usage
public static void main(String[] args) throws IOException{

ExcelReader ereader = new                                          ExcelReader("C:/Users/Sudharsan/Desktop/TestData.xlsx");
System.out.println(ereader.getCellData("Sheet1", 0, 0));
ereader.setCellData("Sheet2", "ShreyaGhosal", 11, 11);
}

}





Tuesday, 9 August 2016

Database connection using selenium webdriver

In order to do Database testing using Selenium you have to make a connection to the different Databases as per requirement.Please follow Below Steps

Step:1
Make a connection to the different Databases as per requirement
MySQL connection:

 public static void getMySQLConnection(String hostName,
                                                String portConn,
                                                String dbName,
                                                String userName,
                                                String passWord)
            throws ClassNotFoundException, SQLException {

        // With port
String connectionURL = "jdbc:mysql://" + hostName + ":" + portConn + "/" + dbName;

        // Without port
        String connectionURL = "jdbc:mysql://"
                                + hostName + "/"
                                + dbName
                                + "?characterEncoding=UTF-8&useSSL=false";

        // Connection
        Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
        System.out.println("--- MySQL database connected ---");
    }

SQL-Server connection by JDTS:

    public static void getSQLServerConnection_JDTS(String hostName,
                                                         String sqlInstanceName,
                                                         String database,
                                                         String userName,
                                                         String passWord,
                                                         String portConn)
                            throws ClassNotFoundException, SQLException {

        /*
         * Syntax: "jdbc:jtds:sqlserver://localhost:1433/testsimple;instance=SQLEXPRESS"
         */
        String connectionURL = "jdbc:jtds:sqlserver://"
                                + hostName + ":"
                                + portConn + "/"
                                + database
                                + ";instance="
                                + sqlInstanceName;

        Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
        System.out.println("--- SQLSERVER JTDS connected ---");

    }

SQL-Server connection by JDBC:
 public static void getSQLServerConnection_JDBC(String hostName,
                                                         String portConn,
                                                         String sqlInstanceName,
                                                         String database,
                                                         String userName,
                                                         String passWord)
                            throws ClassNotFoundException, SQLException {

        /*
         * Syntax: "jdbc:sqlserver://ServerIp:1433;instance=SQLEXPRESS;databaseName=testmydb"
         */
        String connectionURL = "jdbc:sqlserver://"
                                + hostName + ":"
                                + portConn + ";"
                                + sqlInstanceName + ";"
                                + "databaseName="
                                + database;

        Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
        System.out.println("--- SQLSERVER JDBC connected ---");

    }

Oracle connection:
public static void getOracleConnection(String hostName,
                                                 String sid,
                                                 String userName,
                                                 String password,
                                                 String port)
                            throws ClassNotFoundException, SQLException {

        //Syntax: "jdbc:oracle:thin:@localhost:1521:db11g"
        String connectionURL = "jdbc:oracle:thin:@"
                                + hostName + ":"
                                + port + ":"
                                + sid;

        Connection conn = DriverManager.getConnection(connectionURL, userName, password);
        System.out.println("--- ORACLE database connected ---");

    }

Step:2
Send Queries to the Database and retrieve the data.

//Statement Object to send queries
Statement stmt = con.createStatement();

//send the query to database use execute query and store the results in the Result Set object

ResultSet rs = stmt.executeQuery(select * from employee;);

Step:3
Process the results.