Monday, December 3, 2012

Selenium Database Connectivity using Java

We can easily connect Database with Selenium. The below example illustrates how to use/connect MySQL with Selenium using Java. 


mysql-connector-java-5.1.0-bin.jar  or above versions
Create a table with some data


import java.sql.*;
import javax.sql.*;

public class dbconnection
public static void main(String args[])
String email;
String dbUrl = "jdbc:mysql://localhost:3306/test";  //This URL is based on your IP address
String username="username"; //Default username is root
String password="password"; //Default password is root
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select email from users where user_id = 1;";


Connection con = DriverManager.getConnection (dbUrl,username,password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while ( 
dbtime = rs.getString(1);
} //end while

} //end try

catch(ClassNotFoundException e) 

catch(SQLException e) 

}  //end main

}  //end class

Tuesday, August 21, 2012

Handling alert box using AutoIT

AutoIT is a scripting language designed for automating the Windows GUI. It uses simulated keystrokes, mouse movements and window manipulations.

Here we are going to use AutoIT for handling authentication boxes like below,

Step 1: 

Download the latest version of AutoIT.

Step 2: 

Write the code and Save it as 'Authentication.au3' 
(au3 is the AutoIT format)

Use the below code
WinWaitActive("Authentication Required")

Step 3:

After that, right click on your Authentication.au3 file and click 'Compile Script'
This will covert your AutoIT script into an executable file

Step 4:

Now go to your Selenium code, and add the below line.


Thursday, August 9, 2012

Handling alerts in Selenium webdriver

Handling alerts in Webdriver :

Import this package in your script.
#import org.openqa.selenium.Alert;

For accepting the alert box:
Alert al = driver.switchTo ().alert ();
al.accept ();
For cancelling the alert box:
Alert al = driver.switchTo ().alert ();
al.dismiss ();

For getting the text of the alert box:

Alert al = driver.switchTo ().alert ();
String s = al.getText();

Monday, August 6, 2012

Junit test suite for Selenium Webdriver 2.0

Using Junit test suite, we can run the test cases in Selenium Webriver. 

Here is the sample snippet.

// Test class1

public class c1 {
    public void one(){


//Test class 2
public class c2 {
    public void two(){


//JUnit suite

import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@SuiteClasses({c1.class, c2.class})
public class suite {


Monday, June 25, 2012

Working with Frames

java.util.List<WebElement> f = driver.findElements(By.tagName("iframe")); 
             if(f.size()>0) { 
                   for (WebElement frameid : f){ 

Tuesday, May 29, 2012

How to capture the full page screenshot in webdriver

In order to get the full page screenshot using web driver,

//Include these packages in the script.

//Add these snippet to your code.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\path\\filename.jpg"),true);

Tuesday, May 22, 2012

Getting Browser details using Selenium web driver 2.0

Browser capabilities:
Getting Browser details in selenium is quite easy. Using this we can run our scripts across various machines, various OS, and various browsers (It will be explained in Selenium Grid concept).

Include these two packages into your script
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.Capabilities;

Add the snippet in your script.
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
String os = cap.getPlatform().toString();
String v = cap.getVersion().toString();