Thursday, April 19, 2012

datadriven test in selenium

package mortgageLender;
import static org.testng.AssertJUnit.assertTrue;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import com.thoughtworks.selenium.*;

public class DebtConsolidation extends SeleneseTestCase{
private static final String[] data = null;
public DefaultSelenium selenium;

private void verifytrue(boolean textPresent) {
// TODO Auto-generated method stub

}

@BeforeClass
public void setUp()throws Exception{

selenium = new DefaultSelenium ("localhost", 4444, "*chrome", "http://www.google.com/");
selenium.start();
}
@Test(description="Debt Consolidation")

public void Test() throws Exception{
// Select the field "Debt Consolidation"
// Intialization of the user input ;
BufferedReader in=null;
InputStreamReader inputStream =null;
try{
// define the path of date in excel
inputStream = new InputStreamReader (new FileInputStream("C:\\Documents and Settings\\sgainewar\\workspace\\google\\src\\mortgageLender\\Pages.csv"));
in =new BufferedReader(inputStream);
String line = null;
String url ="";
String category ="";
// define the loop that loop should executed till last row
while ((line = in.readLine())!=null){
// Declare to column is separated with coma  
String[] data = line.split(",");
if(data.length >= 1){
// Declare first row -first column
url = data[0];
category = data[1];
System.out.println("url : "+ url );
System.out.println("Category selected :"+ category);

Thread.sleep(500);
// define data should taken from url column
selenium.open(url);
Thread.sleep(3000);
selenium.click("id=service_debtConsolidation_label");
Thread.sleep(50);
selenium.click("xpath=//form[@id='lenders-form']/div/fieldset/div[2]/button");
//selenium.click("id=PropertyType");
selenium.waitForPageToLoad("30000");
Thread.sleep(3000);
selenium.type("id=property_zipcode", "94154");
Thread.sleep(200);
selenium.mouseOver("xpath=//form[@id='3stepform']/div/div/div/div[2]/button");
selenium.clickAt("xpath=//form[@id='3stepform']/div/div/div/div[2]/button","5,5");
Thread.sleep(400);
try{
Assert.assertEquals("Rate Your Credit:", selenium.getText("xpath=//form[@id='3stepform']/div/div/div[2]/h2/strong"));
System.out.println("Rate Your Credit: is present");
}
catch(Exception e){
System.out.println("Rate Your Credit: is NOT present");
}
selenium.select("id=credit_rating", "label=Good (620-719)");
selenium.mouseOver("xpath=//form[@id='3stepform']/div/div/div[2]/div[3]/button");
selenium.click("css=#slide_page2 > div.clearfix > button.next");

//selenium.clickAt("xpath=//form[@id='3stepform']/div/div/div[2]/div[3]/button", "5,5");
//selenium.mouseOver("xpath=//form[@id='3stepform']/div/div/div[2]/div[3]/button");
Thread.sleep(100);


verifyTrue(selenium.isTextPresent("How would you like to consolidate your debt?"));

selenium.click("id=debtcon1");
selenium.click("id=debtcon");
selenium.click("css=#slide_page3 > div.clearfix > button.next");
try{
Assert.assertEquals("Property Value:", selenium.getText("xpath=//form[@id='3stepform']/div/div/div[3]/h2/strong"));
}
catch(Exception e){
System.out.println("Property Value: is not present");
}
selenium.click("css=#slide_page3 > div.clearfix > button.next");
Thread.sleep(1000);
try{

verifyTrue(selenium.isTextPresent("Total Mortgage Balance:"));
assertTrue(selenium.isTextPresent("Include 1st & 2nd mortgages plus any equity loans or credit lines."));
assertTrue(selenium.isTextPresent("OK to estimate"));

}
catch(Exception e){
System.out.println("Total Mortgage Balance:,OK to estimate is NOT present");
}
selenium.click("css=#slide_page4 > div.clearfix > button.next");
try{
assertTrue(selenium.isTextPresent("Additional Cash Needed:"));
assertTrue(selenium.isTextPresent("Get extra cash for any expense or pay-off credit cards or loans."));
}
catch(Exception e){
System.out.println("Additional Cash Needed: is NOT present");
}
selenium.click("css=#slide_page5 > div.clearfix > button.next");
try{
assertTrue(selenium.isTextPresent("Is your existing mortgage an FHA Loan?"));
}
catch(Exception e){
System.out.println("Is your existing mortgage an FHA Loan? is NOT present");
}

selenium.click("css=#slide_page7 > div.clearfix > button.next");
try{
assertTrue(selenium.isTextPresent("Have you ever had a bankruptcy?"));
}
catch(Exception e){
System.out.println("Have you ever had a bankruptcy? is NOT present");
}

selenium.click("css=#slide_page6 > div.clearfix > button.next");
try{
assertTrue(selenium.isTextPresent("Are you a veteran or active military?"));
}
catch (Exception e){
System.out.println("Are you a veteran or active military? is NOT present");
}
selenium.click("id=military_eligibility");
selenium.click("xpath=(//input[@id='military_eligibility'])[2]");
selenium.click("css=#slide_page7 > div.clearfix > button.next");
selenium.type("id=first_name", "test");
selenium.type("id=last_name", "test");
selenium.type("id=address", "tewst");
selenium.click("link=change");
selenium.type("id=email_address", "test@qs.com");
selenium.type("id=phone_number_1", "321");
selenium.type("id=phone_number_2", "321");
selenium.type("id=phone_number_3", "2133");
selenium.click("id=get_lenders");
verifytrue(selenium.isTextPresent("What is the property zip code?"));

}
}
}
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();

}*/


}