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();
}
}
}
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-2
Doctors
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment