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. 

Prerequisites:

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

Snippet:

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

try 
{

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

while (rs.next()) 
{
dbtime = rs.getString(1);
System.out.println(email);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) 
{
e.printStackTrace();
}

catch(SQLException e) 
{
e.printStackTrace();
}

}  //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")
Send("Username")
Send("{TAB}")
Send("Password")
Send("{ENTER}")

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.

Runtime.getRuntime().exec("C://Path//Authentication.exe");



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();
System.out.println(s);

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 {
    @Test
    public void one(){
        System.out.println("one");
    }

}

//Test class 2
public class c2 {
    @Test
    public void two(){
        System.out.println("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;

@SuppressWarnings("unused")
@RunWith(Suite.class)
@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")); 
            System.out.println(f.size());
             if(f.size()>0) { 
                   for (WebElement frameid : f){ 
                         
                         if(frameid.getAttribute("id").equals("frame_name"))
                         {
                         
                              driver.switchTo().frame(frameid);
                             
                         }
}
}

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.
import java.io.File;
import org.apache.commons.io.FileUtils;

//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();
System.out.println(browserName);
String os = cap.getPlatform().toString();
System.out.println(os);
String v = cap.getVersion().toString();
System.out.println(v);