Subscribe:

Ads 468x60px

wibiya widget

Blogroll

Blogger templates

Blogger news

Tuesday, June 12, 2012

Run as test suite ......

Create the other unit test files (test cases) in same package as image be shown below


You need to create all test cases in same package after that you need to create a runner class for run the all test cases as test suit. Now we think how to run the separate junit files .... :-/
we can create a runner file for run all test case .

The runner java file be shown below 

package search.test;

import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

public class Runner {

  public static void main(String[] args) {
    JUnitCore runner = new JUnitCore();
    System.out.println("test Suite results..");
    runner.addListener(new TextListener(System.out));
    runner.run(search.class,searchFB.class);
  }

}

The image be shown below ..


After that you export the project as Runnable JAR file. Right click your project and click the export link to create below images are shown 




Make sure launch configuration and Export Destination.

After creating the jar file run the selenium RC server in command prompt and run the export jar file in another command prompt , 

command :

java -jar <test suite name>.jar

Enhancement 

Create a properties file for Server Name , Browser, Base URL and Port No.it should be easy for change the server name and other things. Properties file be shown below 
config.properties

server= localhost
browser= *firefox
Url= http://www.google.lk/


And change the each and every test cases /junit file 

public void setUp() throws Exception {
try {

            File file = new File("config.properties");
            FileInputStream fileInput = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fileInput);
            fileInput.close();
            serv=properties.getProperty("server");
            brow=properties.getProperty("browser");
            url=properties.getProperty("Url");
            

        }
     catch (Exception e1) {
        e1.printStackTrace();
    }
selenium = new DefaultSelenium(serv, 4444, brow, url);
selenium.start();
}



Just because something doesn't do what you planned it to do doesn't mean it's useless.
-Thomas Alva Edison







Saturday, June 9, 2012

About Lucene ....

Lucene is a search library .It indexed the files and content as a doc and store temporally after that according the quarry it process and provide the result

first i take you a sample indexing and searching code ..

ohhh....First you need to download the Apache lucene and add as external library in your eclipse project.


then you can see the below java code 

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class simpleSearch {
public static void main(String[] args) throws IOException, ParseException {

   // 0. Specify the analyzer for tokenizing text.
   //    The same analyzer should be used for indexing and searching 
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
    
   // 1. create the index

   Directory index = new RAMDirectory();
   IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
   IndexWriter w = new IndexWriter(index, config);
   addField(w,"ravi","23","colombo");
   addField(w,"sasi","23","kandy");   
   addField(w,"kamal","45","jaffna"); 
   w.close(); 
   
   // 2. query
   String querystr =  "J*"; 
     String[] s = new String[]{"Name","Age","Address"};
    Query  q =new MultiFieldQueryParser(Version.LUCENE_35,s, analyzer).parse(querystr);
  
   // 3. search
   int hitsPerPage = 10;
   IndexReader reader = IndexReader.open(index);
   IndexSearcher searcher = new IndexSearcher(reader);
   TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
   searcher.search(q, collector);
   ScoreDoc[] hits = collector.topDocs().scoreDocs;
  
   // 4. display results
   System.out.println("Found " + hits.length + " hits.");
   for(int i=0;i<hits.length;++i) {
     int docId = hits[i].doc;        
     Document d = searcher.doc(docId);
     for (int t=0;t<s.length;t++){
    System.out.println((i + 1) + ". " + d.get(s[t]));
     }
   }

   // searcher can only be closed when there
   // is no need to access the documents any more.
   searcher.close();
 }


private static void addField(IndexWriter w, String name, String age,String address) throws CorruptIndexException, IOException {
 Field senderNameField = new Field("Name", name,
               Field.Store.YES,
               Field.Index.ANALYZED);
  
Field subjectField = new Field("Age", age,
            Field.Store.YES,
            Field.Index.ANALYZED);
Field addressField = new Field("Address", address,
             Field.Store.YES,
             Field.Index.ANALYZED);
 
Document doc=new Document();
doc.add(subjectField);
doc.add(senderNameField);
doc.add(addressField);
w.addDocument(doc);
 }
}


Print :

Found 1 hits.
1. kamal
1. 45
1. jaffna



Indexing classes

First i take you the important core indexing classes .These are the five main index class provide by the lucene 
listed below :
  1. IndexWriter
  2. Document
  3. Analyzer 
  4. Directory 
  5. Field
IndexWriter- This class is used for the index the new or existing document and also it use for add , remove and update the document. After index it want to a place to store the new indexed document so it used the Directory class for that .

In the example :

IndexWriter w = new IndexWriter(index, config);

Document - It represent collection of the field as virtual document.It is a text you extract from the binary document and add as a field.

In the example :

Document doc=new Document();
doc.add(subjectField);
doc.add(senderNameField);
doc.add(addressField);



Analyzer - It is a abstract class and it passed to the index writer constructor.And it deal with the provide the token to text.

In the example :

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);

Directory- It is a abstract class and it store the indexed document and it have some other sub classes listed below 
  • SimpleFSDirectory 
  • NIOFEDirectory 
  • MMapDirectory
  • RAMDirectory
  • FileSwitchDirectory
In the example:

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);

Field - In a document have several field use this class and add the values and bunch of options to the each field.and if you want to give the priority for the field then we can 

senderNameField.setBoost(1.5f); 

then priority of searching result for goes to the sender name for an example if we have name "Daya" and address "Dehiwala" we seach the "D*" this one the first result should be "Daya"

In the example :


Field senderNameField = new Field("Name", name,
                Field.Store.YES,
                Field.Index.ANALYZED);
   
Field subjectField = new Field("Age", age,
             Field.Store.YES,
             Field.Index.ANALYZED);

Searching classes 

Now we go through the searching classes it has some core class listed below :
  1. IndexSearcher
  2. Query
  3. TopDocs
IndexSearcher - first we need to open the indexed document use the index reader class after that pass the object to InndexSearcher constructor.It used for searching the index document 

In the example :

IndexReader reader = IndexReader.open(index);
 IndexSearcher searcher = new IndexSearcher(reader);

Query - Query class has several concrete classes those are listed below 
  • BooleanQuery
  • PhraseQuery
  • PrefixQuery
  • PhrasePrefixQuery
  • TermRangeQuery
  • NumericRangeQuery
  • FilteredQuery
  • SpanQuery
In the example :

String querystr =  "J*"; 
String[] s = new String[]{"Name","Age","Address"};
Query  q =new MultiFieldQueryParser(Version.LUCENE_35,s, analyzer).parse(querystr);












Things should be made as simple as possible, but not any simpler.
-Albert Einstein

Run selenium script....

After finished your recording save the test case as JUnit4 and give the file name as .java for an example i saved this file as search.java


File should be shown as below 

package com.example.tests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class search extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.lk/");
selenium.start();
}

@Test
public void testSearch() throws Exception {
selenium.open("/");
selenium.type("id=gbqfq", "blog");
selenium.click("id=gbqfb");
selenium.open("/");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

How to use the eclipse for run the selenium script as a junit test 

1.Download and install the eclipse indigo .

Friday, June 8, 2012

About Selenium...

What is the selenium ?

Selenium is a automation tool .it is used for web based application and it more useful for regression testing and functional testing.

Selenium have four type projects :

  • Selenium IDE
  • Selenium Remote control 
  • Selenium Web Driver
  • Selenium Grid
I used the selenium IDE and selenium RC for my industrial training because of that i discussed  these tool how to used in web based application automation.

Selenium IDE .
  1. Download the selenium IDE from this link .it's used for the recording purpose.this is a add on for Mozilla Firefox browser. So you open your Mozilla browser and go to this link and click the download version it should be automatically install as a add on . after that browser prompt the restart message then you will restart your browser .Now you can show your selenium IDE .


   2.  Click the selenium IDE this window should be prompt .


3.Now you can insert your web  URL(what do you want to automate)  in Base URL text box on your selenium IDE .