Friday, July 13, 2012

executing .bat with java code




 try{
       Runtime rn =Runtime.getRuntime();
        try {
            Process p1 = rn.exec("cmd /c start D:\\temp\\a.bat");
           
        }
        catch(Exception e){};

Thursday, July 12, 2012

Retrieve the H1 tag Title :



package autoInsurance;


import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class H1tag {

public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception{

selenium =new DefaultSelenium("localhost",4444,"*chrome", "http://www.loan.com");
selenium.start();

}
@Test
public void TestH1tag() throws Exception{
selenium.open("http://www.loan.com");
selenium.windowMaximize();

// To retirve the H1 tag title selenium.gettext(xpath)
System.out.println("FirstChild of h1 with descendent child "+selenium.getText("css=h1"));
}
@AfterClass
public void tearDown() throws Exception{

selenium.stop();
}
}






To verify the Text from Source code in selenium : 


package zipQuoteNavigation;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;

import static org.testng.Assert.*;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import zipQuoteNavigation.SendFileEmail;

public class Compuquotes {
public Selenium selenium;
public SeleniumServer seleniumserver;
SendFileEmail e = new SendFileEmail();

@BeforeClass
public void setUp() throws Exception
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome","http://www.loan.com");
selenium.start();
}

@Test
public void test() throws Exception{


BufferedReader in=null;
InputStreamReader inputStream =null;
try{
inputStream = new InputStreamReader (new FileInputStream("C:\\Documents and Settings\\sgainewar\\workspace\\HQF\\src\\zipQuoteNavigation\\urls.csv"));
in =new BufferedReader(inputStream);
String line = null;
String url ="";
while ((line = in.readLine())!=null)
{
String[] data = line.split(",");
if(data.length >= 1){
url = data[0];
System.out.println("url : "+ url );
selenium.deleteCookie("", "");
Thread.sleep(500);
selenium.open(url);
Thread.sleep(4000);
selenium.waitForPageToLoad("30000");
selenium.click("id=zipCode");
selenium.type("id=zipCode", "94154");
selenium.click("id=fs-index-submit");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.getHtmlSource().matches("^[\\s\\S]*in_frame[\\s\\S]*$")) break; } catch (Exception e) {}
Thread.sleep(1000);


}
}
}
}
catch (Exception e){
e.printStackTrace();
}
finally{
try{
if(in!=null){
in.close();
}
if(inputStream !=null){
inputStream.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}

}
@AfterClass
public void tearDown() throws Exception{
selenium.stop();
Thread.sleep(2000);
e.main(null);
}
}