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();
}
}
}
A blog on programming and software design interview problems and solutions. Also there are some application development related stuff.
Pages
Saturday, February 15, 2014
Uploading data from a file to Wordpress blog using Selenium and Java-1
Hospitals
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment