Thursday, June 11, 2015

 
















                           In automation testing , date selection is an important area you can never miss. Every date widget will have an input field where text input are accepted. So most of the times as a workaround, we tries to code accordingly to enter the date manually. But it's very important to automate the selection of date from the widget because there may be scenarios in which date widget may not work properly. Here, I will show you a piece of code to automate the date selection from the widget.

public void selectdob(String day, String month, String year){
            //To popup date widget
            new WebDriverWait(BrowserConf.driver, 10).until(
                    ExpectedConditions.presenceOfElementLocated(By.xpath(obj
                            .getProperty("dobbutton")))).click();
            //To select the year from dropdown
            Select yeardrop= new Select(new WebDriverWait(BrowserConf.driver, 10).until(
                    ExpectedConditions.presenceOfElementLocated(By.className(obj
                            .getProperty("yearclass")))));
            yeardrop.selectByVisibleText(year);
            //To select the month from dropdown
            Select monthdrop= new Select(new WebDriverWait(BrowserConf.driver, 10).until(
                    ExpectedConditions.presenceOfElementLocated(By.className(obj
                            .getProperty("monthclass")))));
            monthdrop.selectByVisibleText(month);
            //To select the date from the table
            WebElement table_element = BrowserConf.driver.findElement(By.className("ui-datepicker-calendar"));
            List<WebElement> tr_collection=table_element.findElements(By.xpath("id('ui-datepicker-div')/table/tbody/tr"));
            //System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
            int row_num,col_num;
            row_num=1;
            for(WebElement trElement : tr_collection)
            {
                List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
                //System.out.println("NUMBER OF COLUMNS="+td_collection.size());
                col_num=1;
                for(WebElement tdElement : td_collection)
                {
                    if(tdElement.getText().equals(day)){
                        tdElement.click();
                    }
                    col_num++;
                }
                row_num++;
            }
        }

And you can now call the above function as:

selectdob("24","Jul","2012");

 First two statements are used for selecting month and year from the dropdown in the widget of my application. You may need to have slight change if the widget is different. Last part for selecting the date will be the same.



Thursday, June 4, 2015

In this article, I will be showing you how to fetch and update data of database using JDBC. You may operate any database using the required driver of the particular database

For an example I have used connection to H2 database. H2 has both embedded and server mode. I have tried the server mode. In server mode concurrent connections can be possible.

Prerequisites:
1. Eclipse IDE, Luna or any older version
2. JDK 8 or any older
3. Download Windows installer of H2 database and install to any folder in Windows. You will find a jar file like h2-1.3.176.jar. Copy the file to the library folder of your project and import the JAR.

Now you have done the initial configuration. What you need to do now is create a database in H2.

Double click the h2.bat file in the bin folder of the H2 folder in the installed location. It will load in your default browser. Select the H2 Generic H2 (Server) in first drop down. Give a name in second field. Copy the driver class to a notepad. Create a url in JDBC url field like jdbc:h2:tcp://ipaddress/~/test. Save this to the notepad.

Open Eclipse now. Create a Java class to code the JDBC connection part.

JDBC Class
package datpackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import org.h2.tools.Server;

/**
 * @author www.javaworkspace.com
 *
 */
public class jdbcclass {
    public static ResultSet resultSet = null;
    public static Connection connection = null;
    public static Statement statement = null;
    public static Server server;
    public static void query(String sql) {       
        try {
            Class.forName("org.h2.Driver");
           
            try{
                connection = DriverManager.getConnection("jdbc:h2:tcp://ipaddres/~/test", "sa", "");
                   
            }
           catch(SQLException e2){
               e2.printStackTrace();
               return;   
               
           }
            statement = connection.createStatement();
           
            try{
                resultSet= statement.executeQuery(sql);
               
            }
        catch(SQLIntegrityConstraintViolationException e1){
            java.awt.Toolkit.getDefaultToolkit().beep();
            return;   
        }
            while (resultSet.next()) {
                System.out.println(resultSet.getString("NAME"));
            }
        } catch (SQLException e2) {
            e2.printStackTrace();
            return;   
            } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(resultSet!=null){
                resultSet.close();
                statement.close();
                connection.close();
                }
               
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static void update(String sql){
        try {
            Class.forName("org.h2.Driver");

            try{
                connection = DriverManager.getConnection("jdbc:h2:tcp://ipaddres/~/test", "sa", "");
            }
            catch(SQLException e2){
                e2.printStackTrace();
                return;   

            }
            try {
                statement = connection.createStatement();
                statement.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
                server.stop();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
You can now call statements in a different class to fetch and update data.

 To fetch data:
jdbcclass.query("SELECT * FROM H2DATABASE  NAME WHERE NAME LIKE A% ORDER BY NAME");

To update data:
jdbcclass.update("INSERT INTO H2DATABASE VALUES ('"Abc"', '"bcd"','"efg"');");




>

Selenium useful links

Powered by Blogger.

Featured Post

Benefits of having a user Interface for a Selenium Automation Suite/Regression Suite

Once you are able to work independently on Selenium scripting, another important task is how to ease your automation test execution. There a...

Video

Popular Posts

Our Facebook Page