Saturday, February 15, 2014

Uploading data from a file to WordPress blog using Selenium and Java-2

Doctors

package com.nueve.review.hreview;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.w3c.dom.Element;

public class PostWriter {
private static WebDriver driver;
private static String baseUrl;
private static boolean acceptNextAlert = true;
private static StringBuffer verificationErrors = new StringBuffer();
private static LinkedHashMap<String,String> columnsMap = new LinkedHashMap<String,String>();
private static Boolean upload = true;//Variable that controls the uploading part of code.
private static String[] membershipArray = {"Membership1", "Membership2", "Membership3", "Membership4"};
private static String[] experienceArray = {"Experience 1", "Experience 2", "Experience 3", "Experience 4"};

public static void main(String[] args) {
//Template creation.
columnsMap.put("Unique ID", "");
columnsMap.put("First Name", "Dr. <first name>");
columnsMap.put("Middle Name", " <middle name>");
columnsMap.put("Last Name", " <last name>");
columnsMap.put("Main area of Expertise", " works as a <main area of expertise>");
columnsMap.put("Name of Hospital 1", " in <name of hospital>");
columnsMap.put("H1 Employment Type", " as <H1 Employement type>.");
columnsMap.put("H1 Fee (Rs.)", "\nConsultation Fee: Rs. <fee>");
columnsMap.put("Other areas of expertise", "\nIs also specialized in <other areas of expertise>\n");
columnsMap.put("Graduation Degree", "\nGraduation: <graduation degree>");
columnsMap.put("Graduation College", " <graduation college>");
columnsMap.put("Post Graduation Degree", "\nPost graduation: <post graduation degree>");
columnsMap.put("Post Graduation Area", "-<Post Graduation Area>");
columnsMap.put("Post Graduation College", " <Post Graduation College>");
columnsMap.put("Specialization Degree", "\nSpecialization: <Specialization degree>");
columnsMap.put("Specialization Area", " <specialization college>");
columnsMap.put("Specialization College", "-<Specialization Area>");
columnsMap.put("Super Specialization Degree", "\nSuper Specialization: <Super Specialization degree>");
columnsMap.put("Super Specialization Area", "-<super specialization area>");
columnsMap.put("Super Specialization College", " <Super Specialization College>");
String priorExperienceString = "\n\n<strong>Prior Experience:</strong>\n";
columnsMap.put("Total Experience","<total experience>\n");
for(int i = 0; i < experienceArray.length; i++) {
String key = experienceArray[i];
String value = "<" + experienceArray[i] + ">\n";
columnsMap.put(key, value);
}
String awardsString = "\n\n<strong>Awards and Membership:</strong>\n";
columnsMap.put("Awards and Recognitions", "<Awards and Recognitions>");
for(int i = 0; i < membershipArray.length; i++) {
String key = membershipArray[i];
String value = "<" + membershipArray[i] + ">\n";
columnsMap.put(key, value);
}
String otherHospitalsString = "\n\n<strong>Other Hospitals where doctor is available:</strong>\n";
columnsMap.put("Name of Hospital 2", " <Name of Hospital 2>");
columnsMap.put("H2 Employment Type", " as <H2 Employment Type>");
columnsMap.put("H2 Fee (Rs.)", "\nConsultation fee: Rs. <h2 fee>.\n");
columnsMap.put("Name of Hospital 3", " <Name of Hospital 3>");
columnsMap.put("H3 Employment Type", " as <H3 Employment Type>");
columnsMap.put("H3 Fee (Rs.)", "\nConsultation fee: Rs. <h3 fee>.\n");

String[] column = new String[10];
String csvFileName = "/Users/dsourabh/Downloads/Sample Data 26Jan14.csv";
//String csvFileName = "/Users/dsourabh/Documents/SampleDoctor.csv";
try {
BufferedReader csvReader = new BufferedReader(new FileReader(csvFileName));
StringTokenizer stringTokenizer = null;
String curLine = csvReader.readLine();
int fieldCount = 0;
String[] csvFields = null;
if(curLine != null)
{
stringTokenizer = new StringTokenizer(curLine, ",");
fieldCount = stringTokenizer.countTokens();
if(fieldCount > 0)
{
csvFields = new String[fieldCount];
int i=0;
while(stringTokenizer.hasMoreElements())
csvFields[i++] = String.valueOf(stringTokenizer.nextElement());
}
}
if(upload) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://nueve.co.in/doctors/");
//Logging in and opening the Admin page.
driver.findElement(By.linkText("Log in")).click();
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys("username");
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys("password");
driver.findElement(By.id("wp-submit")).click();
}
int cnt = 1;
while((curLine = csvReader.readLine()) != null)
{
System.out.println("Doctor: "+cnt);
cnt++;
StringBuilder titleString = new StringBuilder();
StringBuilder sb = new StringBuilder();
stringTokenizer = new StringTokenizer(curLine, ",");
fieldCount = stringTokenizer.countTokens();
LinkedHashMap<String, String> valuesMap = new LinkedHashMap<String, String>();
//System.out.println("fieldCount = "+fieldCount);
if(fieldCount > 0)
{
int i=0;
while(stringTokenizer.hasMoreElements())
{
try
{
String curValue = String.valueOf(stringTokenizer.nextElement());
//System.out.println(curValue);
valuesMap.put(csvFields[i], curValue);
}
catch(Exception exp)
{
//System.out.println(csvFields[i]);
exp.printStackTrace();
}
i++;
}
if(valuesMap.get("First Name") != null && !valuesMap.get("First Name").equals("blank")) {
titleString.append("Dr. ");
titleString.append(valuesMap.get("First Name"));
}
if(valuesMap.get("Middle Name") != null && !valuesMap.get("Middle Name").equals("blank")) {
titleString.append(" ");
if(valuesMap.get("First Name").equals("blank")) {
titleString.append("Dr. ");
}
titleString.append(valuesMap.get("Middle Name"));
}
if(valuesMap.get("Last Name") != null && !valuesMap.get("Last Name").equals("blank")) {
titleString.append(" ");
if(valuesMap.get("First Name").equals("blank") && valuesMap.get("Middle Name").equals("blank")) {
titleString.append("Dr. ");
}
titleString.append(valuesMap.get("Last Name"));
}

Set<String> keySet = valuesMap.keySet();
Iterator<String> keySetIterator = keySet.iterator();

while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
try {
if(!valuesMap.get(key).equals("blank")) {
String templateValue = columnsMap.get(key);
String actualValue = templateValue.replaceAll("<(.*)>", valuesMap.get(key));
sb.append(actualValue);
}
if(key.equals("Super Specialization College")) {
Boolean toAppend = false;
for(int j = 0; j < experienceArray.length; j++) {
if(!valuesMap.get(experienceArray[j]).equals("blank")) {
toAppend = true;
}
}
if(!valuesMap.get("Total Experience").equals("blank")) {
toAppend = true;
}
if(toAppend) {
sb.append(priorExperienceString);
}
}
else if(key.equals(experienceArray[experienceArray.length - 1])) {
Boolean toAppend = false;
for(int j = 0; j < membershipArray.length; j++) {
if(!valuesMap.get(membershipArray[j]).equals("blank")) {
toAppend = true;
}
}
if(toAppend) {
sb.append(awardsString);
}
}
else if(key.equals(membershipArray[membershipArray.length - 1])) {
if(valuesMap.get("Name of Hospital 2") != null && !valuesMap.get("Name of Hospital 2").equals("blank")) {
sb.append(otherHospitalsString);
}
}
} catch (NullPointerException e){
System.out.println(key);
e.printStackTrace();
}
}
System.out.println(sb);
}
if(upload) {
baseUrl = "http://nueve.co.in/doctors/wp-admin/post-new.php";
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);

//baseUrl = "http://nueve.co.in/doctors/wp-admin/";
//Adding a new post with the required parameters.
//driver.get(baseUrl + "/doctors/wp-admin/");
driver.findElement(By.id("title-prompt-text")).click();
driver.findElement(By.id("title")).clear();
driver.findElement(By.id("title")).sendKeys(titleString);
//driver.findElement(By.id("content_ifr")).clear();

driver.findElement(By.id("content-html")).click();
driver.findElement(By.id("content")).clear();
driver.findElement(By.id("content")).sendKeys(sb);

/*
driver.findElement(By.id("acf-field-speciality")).clear();
driver.findElement(By.id("acf-field-speciality")).sendKeys("Speciality");
//driver.findElement(By.id("dp1390624043131")).click();
//driver.findElement(By.linkText("10")).click();
driver.findElement(By.id("acf-since")).click();
//driver.findElement(By.id("acf-since")).clear();
driver.findElement(By.id("acf-since")).sendKeys("24 Jan");
//driver.findElement(By.id("dp1390624043131")).click();
driver.findElement(By.id("acf-field-phone")).click();
driver.findElement(By.id("acf-field-phone")).clear();
driver.findElement(By.id("acf-field-phone")).sendKeys("99999999999");
driver.findElement(By.id("acf-field-address")).clear();
driver.findElement(By.id("acf-field-address")).sendKeys("Address");
*/
if (valuesMap.get("Main area of Expertise") != null && !valuesMap.get("Main area of Expertise").equals("blank")) {
driver.findElement(By.id("new-tag-post_tag")).clear();
driver.findElement(By.id("new-tag-post_tag")).sendKeys(valuesMap.get("Main area of Expertise"));
driver.findElement(By.cssSelector("input.button.tagadd")).click();
}

driver.findElement(By.linkText("Criteria")).click();
driver.findElement(By.linkText("Main")).click();
new Select(driver.findElement(By.id("ta_post_review_rating"))).selectByVisibleText("4");
driver.findElement(By.linkText("Criteria")).click();
driver.findElement(By.name("ta_post_repeatable[0][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[0][title]")).sendKeys("Quality of treatment");
driver.findElement(By.name("ta_post_repeatable[0][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[0][desc]")).sendKeys("Quality of treatment");
driver.findElement(By.name("ta_post_repeatable[0][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[0][rating]")).sendKeys("0");
driver.findElement(By.linkText("+ Add")).click();
driver.findElement(By.name("ta_post_repeatable[1][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[1][title]")).sendKeys("Cost of treatment");
driver.findElement(By.name("ta_post_repeatable[1][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[1][desc]")).sendKeys("Cost of treatment");
driver.findElement(By.name("ta_post_repeatable[1][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[1][rating]")).sendKeys("0");
driver.findElement(By.linkText("+ Add")).click();
driver.findElement(By.name("ta_post_repeatable[2][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[2][title]")).sendKeys("Waiting Time");
driver.findElement(By.name("ta_post_repeatable[2][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[2][desc]")).sendKeys("Waiting Time");
driver.findElement(By.name("ta_post_repeatable[2][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[2][rating]")).sendKeys("0");
driver.findElement(By.linkText("+ Add")).click();
driver.findElement(By.name("ta_post_repeatable[3][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[3][title]")).sendKeys("Overall patient satisfaction");
driver.findElement(By.name("ta_post_repeatable[3][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[3][desc]")).sendKeys("Overall patient satisfaction");
driver.findElement(By.name("ta_post_repeatable[3][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[3][rating]")).sendKeys("0");

driver.findElement(By.id("in-category-105")).click();

synchronized (driver) {
try {
driver.wait(4000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

if(valuesMap.get("Unique ID") != null && !valuesMap.get("Unique ID").equals("blank")) {
driver.findElement(By.id("acf-field-index")).click();
driver.findElement(By.id("acf-field-index")).clear();
driver.findElement(By.id("acf-field-index")).sendKeys(valuesMap.get("Unique ID"));
}
//driver.findElement(By.id("dp1390624043131")).click();
//driver.findElement(By.linkText("10")).click();
if(valuesMap.get("First Name") != null && !valuesMap.get("First Name").equals("blank")) {
driver.findElement(By.id("acf-field-first_name")).click();
driver.findElement(By.id("acf-field-first_name")).clear();
driver.findElement(By.id("acf-field-first_name")).sendKeys(valuesMap.get("First Name"));
}
//driver.findElement(By.id("acf-since")).clear();
//driver.findElement(By.id("acf-since")).sendKeys("24 Jan");
//driver.findElement(By.id("dp1390624043131")).click();
if(valuesMap.get("Middle Name") != null && !valuesMap.get("Middle Name").equals("blank")) {
driver.findElement(By.id("acf-field-middle_name")).click();
driver.findElement(By.id("acf-field-middle_name")).clear();
driver.findElement(By.id("acf-field-middle_name")).sendKeys(valuesMap.get("Middle Name"));
}
if(valuesMap.get("Last Name") != null && !valuesMap.get("Last Name").equals("blank")) {
driver.findElement(By.id("acf-field-last_name")).click();
driver.findElement(By.id("acf-field-last_name")).clear();
driver.findElement(By.id("acf-field-last_name")).sendKeys(valuesMap.get("Last Name"));
}

synchronized (driver) {
try {
driver.wait(6000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
driver.findElement(By.id("publish")).click();
}
sb = null;
titleString = null;
}
if(upload) {
driver.close();
}
csvReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchElementException nse) {
nse.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Uploading data from a file to Wordpress blog using Selenium and Java-1

Hospitals

package com.nueve.review.hreview;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HospitalPostWriter {
private static WebDriver driver;
private static String baseUrl;
private static boolean acceptNextAlert = true;
private static StringBuffer verificationErrors = new StringBuffer();
private static LinkedHashMap<String,String> columnsMap = new LinkedHashMap<String,String>();
private static Boolean upload = true;//Variable that controls the uploading part of code.
private static String[] contactNumbers = {"Alternate Contact Number 1","Alternate Contact Number 2"};
private static String[] membershipArray = {"Awards & Recognition", "Membership2", "Membership3"};
private static String[] emailIds = {"Email Id", "Alternate Email Id"};
private static String[] extensionNumbers = {"Extension Number 1", "Extension Number 2"};

private static String opps_map = "[oppso-map";
private static String map_id = " map_id=\"123\"";
private static String map_type = " map_type=\"ROADMAP\"";
private static String bubble = " bubble=\"\"";
private static String height = " height=\"350px\"";
private static String width = " width=\"400px\"";
private static String zoom = " zoom=\"14\"";
private static String lat = " lat=\"\"";
private static String lon = " lon=\"\"]";

//[oppso-map map_id="123" map_type="ROADMAP" bubble="" height="350px" width="400px" zoom="14" lat="12.95223" lon="77.52879"]

public static void main(String[] args) {
//Template creation.
columnsMap.put("Unique ID", "");
columnsMap.put("Hospital Name", "");
columnsMap.put("Full Address", " is located at ");
columnsMap.put("City", " ");
columnsMap.put("State", ", ");
columnsMap.put("Pincode", ", ");
columnsMap.put("Nearest Landmark", "\nNearest Landmark: ");
columnsMap.put("Locality", " in ");
columnsMap.put("Primary Contact Number", "\nFor booking appointments, please call ");

String alternateContactsString = "\n\nAlternative Contact Numbers\n";
for(int i = 0; i < contactNumbers.length; i++) {
String key = contactNumbers[i];
String value = "<" + contactNumbers[i] + ">\n";
columnsMap.put(key, value);
}

String emailString = "\nEmail ID: \n";
for(int i = 0; i < emailIds.length; i++) {
String key = emailIds[i];
String value = "<" + emailIds[i] + ">\n";
columnsMap.put(key, value);
}

String extensionString = "\n\nExtension Numbers: ";
for(int i = 0; i < extensionNumbers.length; i++) {
String key = extensionNumbers[i];
String value = "<" + extensionNumbers[i] + ">\n";
columnsMap.put(key, value);
}
columnsMap.put("Fax Number", "\nFax Number: ");
columnsMap.put("Hospital Website", "\nWebsite: ");

String aboutHospitalString = "\n\nAbout the hospital\n";

columnsMap.put("Hospital Description", "\n\n");
columnsMap.put("Hospital Speciality", "\n");

String awardsString = "\n\nAwards and Membership:\n";
for(int i = 0; i < membershipArray.length; i++) {
String key = membershipArray[i];
String value = "<" + membershipArray[i] + ">\n";
columnsMap.put(key, value);
}
columnsMap.put("Latitude", "\nLatitude: ");
columnsMap.put("Longitude", "\nLongitude: ");

String csvFileName = "/Users/dsourabh/Downloads/HospitalTrial.csv";
//String csvFileName = "/Users/dsourabh/Documents/SampleDoctor.csv";
try {
BufferedReader csvReader = new BufferedReader(new FileReader(csvFileName));
StringTokenizer stringTokenizer = null;
String curLine = csvReader.readLine();
int fieldCount = 0;
String[] csvFields = null;
if(curLine != null)
{
stringTokenizer = new StringTokenizer(curLine, ",");
fieldCount = stringTokenizer.countTokens();
if(fieldCount > 0)
{
csvFields = new String[fieldCount];
int i=0;
while(stringTokenizer.hasMoreElements())
csvFields[i++] = String.valueOf(stringTokenizer.nextElement());
}
}
if(upload) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://nueve.co.in/doctors/");
//Logging in and opening the Admin page.
driver.findElement(By.linkText("Log in")).click();
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys("username");
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys("passsword");
driver.findElement(By.id("wp-submit")).click();
}
int cnt = 1;
while((curLine = csvReader.readLine()) != null)
{
System.out.println("Hospital: "+cnt);
cnt++;
StringBuilder mapString = new StringBuilder();
StringBuilder titleString = new StringBuilder();
StringBuilder sb = new StringBuilder();
stringTokenizer = new StringTokenizer(curLine, ",");
fieldCount = stringTokenizer.countTokens();
LinkedHashMap<String, String> valuesMap = new LinkedHashMap<String, String>();
//System.out.println("fieldCount = "+fieldCount);
if(fieldCount > 0)
{
int i=0;
while(stringTokenizer.hasMoreElements())
{
try
{
String curValue = String.valueOf(stringTokenizer.nextElement());
//System.out.println(curValue);
valuesMap.put(csvFields[i], curValue);
}
catch(Exception exp)
{
//System.out.println(csvFields[i]);
exp.printStackTrace();
}
i++;
}
if(valuesMap.get("Hospital Name") != null && !valuesMap.get("Hospital Name").equals("blank")) {
titleString.append(valuesMap.get("Hospital Name"));
}

Set keySet = valuesMap.keySet();
Iterator keySetIterator = keySet.iterator();
//[oppso-map map_id="123" map_type="ROADMAP" bubble="" height="350px" width="400px" zoom="14" lat="12.95223" lon="77.52879"]
while (keySetIterator.hasNext()) {
String key = keySetIterator.next();
try {
if(!valuesMap.get(key).equals("blank")) {
String templateValue = columnsMap.get(key);
String actualValue = templateValue.replaceAll("<(.*)>", valuesMap.get(key));
if(key.equals("Latitude")) {
sb.append("\n\n");
String actualLat = lat.replaceAll("<(.*)>", valuesMap.get("Latitude"));
String actualLon = lon.replaceAll("<(.*)>", valuesMap.get("Longitude"));
sb.append(opps_map + map_id + map_type + bubble + height + width + zoom + actualLat + actualLon);
break;
}
sb.append(actualValue);
}
if(key.equals("Primary Contact Number")) {
Boolean toAppend = false;
for(int j = 0; j < contactNumbers.length; j++) {
if(!valuesMap.get(contactNumbers[j]).equals("blank")) {
toAppend = true;
}
}
if(toAppend) {
sb.append(alternateContactsString);
}
}
else if(key.equals(contactNumbers[contactNumbers.length - 1])) {
Boolean toAppend = false;
for(int j = 0; j < emailIds.length; j++) {
if(!valuesMap.get(emailIds[j]).equals("blank")) {
toAppend = true;
}
}
if(toAppend) {
sb.append(emailString);
}
}
else if(key.equals(emailIds[emailIds.length - 1])) {
Boolean toAppend = false;
for(int j = 0; j < extensionNumbers.length; j++) {
if(!valuesMap.get(extensionNumbers[j]).equals("blank")) {
toAppend = true;
}
}
if(toAppend) {
sb.append(extensionString);
}
}
else if(key.equals("Hospital Website")) {
Boolean toAppend = false;
if((valuesMap.get("Hospital Description") != null && !valuesMap.get("Hospital Description").equals("blank"))
|| (valuesMap.get("Hospital Speciality") != null && !valuesMap.get("Hospital Speciality").equals("blank"))) {
toAppend = true;
}
if(toAppend) {
sb.append(aboutHospitalString);
}
}
else if(key.equals("Hospital Speciality")) {
Boolean toAppend = false;
if((valuesMap.get("Awards & Recognition") != null && !valuesMap.get("Awards & Recognition").equals("blank"))) {
toAppend = true;
}
if(toAppend) {
sb.append(awardsString);
}
}
} catch (NullPointerException e){
System.out.println(key);
e.printStackTrace();
}
}
System.out.println(sb);
}
if(upload) {
baseUrl = "http://nueve.co.in/doctors/wp-admin/post-new.php";
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);

//baseUrl = "http://nueve.co.in/doctors/wp-admin/";
//Adding a new post with the required parameters.
//driver.get(baseUrl + "/doctors/wp-admin/");
driver.findElement(By.id("title-prompt-text")).click();
driver.findElement(By.id("title")).clear();
driver.findElement(By.id("title")).sendKeys(titleString);
//driver.findElement(By.id("content_ifr")).clear();

driver.findElement(By.id("content-html")).click();
driver.findElement(By.id("content")).clear();
driver.findElement(By.id("content")).sendKeys(sb);

/*
driver.findElement(By.id("acf-field-speciality")).clear();
driver.findElement(By.id("acf-field-speciality")).sendKeys("Speciality");
//driver.findElement(By.id("dp1390624043131")).click();
//driver.findElement(By.linkText("10")).click();
driver.findElement(By.id("acf-since")).click();
//driver.findElement(By.id("acf-since")).clear();
driver.findElement(By.id("acf-since")).sendKeys("24 Jan");
//driver.findElement(By.id("dp1390624043131")).click();
driver.findElement(By.id("acf-field-phone")).click();
driver.findElement(By.id("acf-field-phone")).clear();
driver.findElement(By.id("acf-field-phone")).sendKeys("99999999999");
driver.findElement(By.id("acf-field-address")).clear();
driver.findElement(By.id("acf-field-address")).sendKeys("Address");
*/
if (valuesMap.get("Hospital Speciality") != null && !valuesMap.get("Hospital Speciality").equals("blank")) {
driver.findElement(By.id("new-tag-post_tag")).clear();
driver.findElement(By.id("new-tag-post_tag")).sendKeys(valuesMap.get("Hospital Speciality"));
driver.findElement(By.cssSelector("input.button.tagadd")).click();
}

driver.findElement(By.linkText("Criteria")).click();
driver.findElement(By.linkText("Main")).click();
new Select(driver.findElement(By.id("ta_post_review_rating"))).selectByVisibleText("4");
driver.findElement(By.linkText("Criteria")).click();
driver.findElement(By.name("ta_post_repeatable[0][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[0][title]")).sendKeys("Quality of treatment");
driver.findElement(By.name("ta_post_repeatable[0][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[0][desc]")).sendKeys("Quality of treatment");
driver.findElement(By.name("ta_post_repeatable[0][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[0][rating]")).sendKeys("0");
driver.findElement(By.linkText("+ Add")).click();
driver.findElement(By.name("ta_post_repeatable[1][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[1][title]")).sendKeys("Cost of treatment");
driver.findElement(By.name("ta_post_repeatable[1][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[1][desc]")).sendKeys("Cost of treatment");
driver.findElement(By.name("ta_post_repeatable[1][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[1][rating]")).sendKeys("0");
driver.findElement(By.linkText("+ Add")).click();
driver.findElement(By.name("ta_post_repeatable[2][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[2][title]")).sendKeys("Quality of facilities and equipments");
driver.findElement(By.name("ta_post_repeatable[2][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[2][desc]")).sendKeys("Quality of facilities and equipments");
driver.findElement(By.name("ta_post_repeatable[2][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[2][rating]")).sendKeys("0");
driver.findElement(By.linkText("+ Add")).click();
driver.findElement(By.name("ta_post_repeatable[3][title]")).clear();
driver.findElement(By.name("ta_post_repeatable[3][title]")).sendKeys("Overall patient satisfaction");
driver.findElement(By.name("ta_post_repeatable[3][desc]")).clear();
driver.findElement(By.name("ta_post_repeatable[3][desc]")).sendKeys("Overall patient satisfaction");
driver.findElement(By.name("ta_post_repeatable[3][rating]")).clear();
driver.findElement(By.name("ta_post_repeatable[3][rating]")).sendKeys("0");

driver.findElement(By.id("in-category-106")).click();
synchronized (driver) {
try {
driver.wait(4000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

if(valuesMap.get("Unique ID") != null && !valuesMap.get("Unique ID").equals("blank")) {
driver.findElement(By.id("acf-field-index")).click();
driver.findElement(By.id("acf-field-index")).clear();
driver.findElement(By.id("acf-field-index")).sendKeys(valuesMap.get("Unique ID"));
}
//driver.findElement(By.id("dp1390624043131")).click();
//driver.findElement(By.linkText("10")).click();
if(valuesMap.get("Hospital Name") != null && !valuesMap.get("Hospital Name").equals("blank")) {
driver.findElement(By.id("acf-field-full_name")).click();
//driver.findElement(By.id("acf-since")).clear();
driver.findElement(By.id("acf-field-full_name")).clear();
driver.findElement(By.id("acf-field-full_name")).sendKeys(valuesMap.get("Hospital Name"));
}
//driver.findElement(By.id("dp1390624043131")).click();
if(valuesMap.get("Full Address") != null && !valuesMap.get("Full Address").equals("blank")) {
driver.findElement(By.id("acf-field-full_address")).click();
driver.findElement(By.id("acf-field-full_address")).clear();
driver.findElement(By.id("acf-field-full_address")).sendKeys(valuesMap.get("Full Address"));
}
//driver.findElement(By.id("acf-field-address")).clear();
//driver.findElement(By.id("acf-field-address")).sendKeys("Address");

synchronized (driver) {
try {
driver.wait(6000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
driver.findElement(By.id("publish")).click();
}
sb = null;
titleString = null;
}
if(upload) {
driver.close();
}
csvReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchElementException nse) {
nse.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Sunday, February 2, 2014

Given a sorted array, convert it into a balanced binary search tree

#include<iostream>
using namespace std;

struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* createnode(int x)
{
struct node* new_node=new struct node;
new_node->data=x;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}
int height(struct node* root)
{
if(root==NULL)
{
return 0;
}
else
{
int lheight=1+height(root->left);
int rheight=1+height(root->right);
return lheight>rheight?lheight:rheight;
}
}
struct node* createBST(int *A, int low, int high)
{
if(low>high)
{
return NULL;
}
else
{
int mid = (low + high)/2;
struct node* root = createnode(A[mid]);
root->left = createBST(A, low, mid - 1);
root->right = createBST(A, mid + 1, high);
return root;
}
}
void levelorderprint(struct node* root,int level)
{
if(root && level==0)
{
cout<<root->data<<" ";
}
else if(root)
{
levelorderprint(root->left,level-1);
levelorderprint(root->right,level-1);
}
}
void levelorder(struct node* root)
{
int h=height(root);
for(int i=0;i<h;i++)
{
levelorderprint(root,i);
cout<<endl;
}
}
int main()
{
int A[6] = {1,2,3,4,5,6};
int n = 5;
struct node* treeRoot = createBST(A,0,n-1);
levelorder(treeRoot);
}