Monday, November 5, 2012

Selenium and Webdriver

Here is a simple program that shows how to use selenium.
More concrete examples of webdriver/selenium and page-objects are at http://code.google.com/p/java-through-examples/source/browse/tests/org/megha/blog/example/selenium/


package org.megha.blog.example.selenium;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
/**
 * Searches for a given string on yahoo.com.
 * (just an example to show how selenium client is used.)
 */
public class YahooSearch {
        // searches for the given string and prints the top two results
        public void searchFor(String string) {
                Selenium selenium = new DefaultSelenium("LOCALHOST", 4444, "*firefox", "http://www.yahoo.com/");
                selenium.start();
                selenium.open("/");
                selenium.waitForPageToLoad("30000");
                selenium.type("name=p", string);
                selenium.click("id=search-submit");
                selenium.waitForPageToLoad("30000");
                // TODO: do something with the results
                selenium.close();
                selenium.stop();
        }
       
        public static void main(String[] args) {
                YahooSearch search = new YahooSearch();
                search.searchFor("google");
        }
}

File IO

An example of how to read/write text files in java.
http://code.google.com/p/java-through-examples/source/browse/src/org/megha/blog/example/io/#io

package org.megha.blog.example.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Reads all lines from a {@link Reader}.
 * Don't use this to read huge files!
 *
 * @author megha birmiwal
 */
public class LinesReader {

        private Reader reader;
       
        public LinesReader(Reader reader) {
                this.reader = reader;
        }

        /**
         * Reads all the lines from the provided {@link Reader}.
         *
         * @return a list of strings (lines from the file)
         * @throws IOException if there was an exception reading from the file
         */
        public List<String> readAllLines() throws IOException {
                BufferedReader bufferedReader = new BufferedReader(reader);
                List<String> lines = new ArrayList<String>();
                String line = bufferedReader.readLine();
                while (line != null) {
                        lines.add(line);
                        line = bufferedReader.readLine();
                }
                return lines;
        }
}
package org.megha.blog.example.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Reads a file and writes unique words as output into another.
 *
 * @author megha birmiwal
 */
public class UniqueWords {

        public static void main(String args[]) throws IOException {
                if (args.length != 2) {
                        System.out.println("please provide two file names: <infile> <outfile>");
                        return;
                }
               
                FileReader fileReader = null;
                try {
                        fileReader = new FileReader(args[0]);
                } catch (FileNotFoundException e) {
                        System.out.println("file not found: " + e.getMessage());
                        return;
                }
                LinesReader lineReader = new LinesReader(fileReader);
                List<String> readLines = null;
                try {
                        readLines = lineReader.readAllLines();
                } catch (IOException e) {
                        System.out.println("IOException while reading from file: " + e.getMessage());
                        return;
                } finally {
                        fileReader.close();
                }
                // Prints all the lines in the file
//              for (String line : readLines) {
//                      System.out.println(line);
//              }

                // Get all the unique words and save in a file
                Set<String> uniquewords = new HashSet<String>();
                for (String line : readLines) {
                        String[] words = line.split("[ ;,/\\(\\)\\[\\]\"\\t]");
                        for(String word : words) {
                                uniquewords.add(word);
                        }
                }

                Writer writer = new FileWriter(args[1]);
                try {
                        for(String word : uniquewords) {
        //                      System.out.println(word);
                                writer.write(word + "\n");
                        }
                } finally {
                        writer.close();
                }
        }
}

Saturday, November 3, 2012

Threads and concurrent execution


Several threads / concurrency related examples today.

Look at http://code.google.com/p/java-through-examples/source/browse/src/org/megha/blog/example/concurrency/#concurrency

Type casting


complete example here

Handling exceptions