I thought to get the current indian (NSE) stock quotes by using Selenium tool. I chose Selenium RC and Java language. I have developed a java class for all file related tasks. Another java program used to get the stock values from set of given scrips.
Selenium RC required set of configuration to run the tests. We need to give the proxy details (with or without proxy) for internet connection. I'm planning to cover the configuration in upcoming posts. Below I have given the java code to get current stock values.
Current Stock quotes - Selenium RC with Java
Note: FileHandlerLib class - file related tasks.
/* To get all stockquotes * To Compile: javac GetAllStockQuotes.java -Xlint:unchecked * To Run: java junit.textui.TestRunner com.palani.tests.GetAllStockQuotes * To Run: java GetAllStockQuotes * Note: Recompile with -Xlint:unchecked for details. * javac FileHandlerLib.java -Xlint:unchecked */ package com.palani.tests; // import FileHandlerLib; import com.palani.utils.*; import com.thoughtworks.selenium.*; import junit.framework.*; import java.util.regex.Pattern; import java.io.*; import java.util.*; import java.text.DateFormat; import java.text.SimpleDateFormat; public class GetAllStockQuotes extends SeleneseTestCase { private Selenium selenium; private FileHandlerLib fhNew = new FileHandlerLib(); // @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.nseindia.com/"); selenium.start(); } // @Test public void testGetAllStockQuotes() throws Exception { // Variables String sScripsFile; String sQuotesFile; String sUrlPart; String sCurrentDate; String sQuote; String sQuoteValue; String sOutput; ArrayList aData = new ArrayList(); // Initialization // sCurrentDate = "20100101" ; sCurrentDate = fhNew.getDateTime(); sQuotesFile = "D:\\Stocks\\"+sCurrentDate+"_stocks.txt"; fhNew.DeleteFile (sQuotesFile); sScripsFile = "D:\\Stocks\\StockScrips.txt"; sUrlPart="/marketinfo/companyinfo/companysearch.jsp?cons="; aData = fhNew.ReadFileToArray (sScripsFile); Object[] elements = aData.toArray(); System.out.println ("SCRIPs File: " + sScripsFile); System.out.println ("Quotes File: " + sQuotesFile); selenium.open("/content/equities/cmquote.htm"); Thread.sleep(2000); for(int iIndex=0; iIndex < elements.length ; iIndex++) { sQuote=(String)elements[iIndex]; sQuote=sQuote.trim(); sQuoteValue = "0"; System.out.println("Quote:"+elements[iIndex]+ " ,index: " + iIndex); if (!(sQuote.equals("") ) ) { if (selenium.isElementPresent("companyname")) { selenium.type("companyname", sQuote); } else { selenium.type("company", sQuote); } selenium.click("submit1"); Thread.sleep(2000); for (int second = 0;; second++) { if (second >= 150) System.out.println("timeout"); try { if (selenium.isElementPresent("//div[@id='PI4']/b")) { sQuoteValue = selenium.getText("//div[@id='PI4']/b"); System.out.println ("Quote Value: " + sQuoteValue); sOutput=sQuote.trim()+","+sQuoteValue.trim(); fhNew.FileWrite (sQuotesFile,sOutput); break; } // end if } catch (Exception e) { System.out.println("Selenium Run: " + e.getMessage() + "\n"); e.printStackTrace(); } Thread.sleep(1000); } } // End If } // end for loop } // @After public void tearDown() { selenium.stop(); } // To run Selenium Testcase using JUnit public static Test suite() { return new TestSuite(GetAllStockQuotes.class); } // To run Selenium Testcase public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } }