/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/MyFirstGUIProject - Daniel/src/main/java/com/mycompany/myfirstguiproject/EmailAutomation.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package com.mycompany.myfirstguiproject; 
  7 
  8 import com.google.gson.Gson;
  9 import java.awt.AWTException;
 10 import java.awt.Robot;
 11 import java.io.File;
 12 import java.util.ArrayList;
 13 import java.util.concurrent.TimeUnit;
 14 import javax.swing.DefaultListModel;
 15 import org.openqa.selenium.By;
 16 import org.openqa.selenium.Keys;
 17 import org.openqa.selenium.WebDriver;
 18 import org.openqa.selenium.chrome.ChromeDriver;
 19 import org.openqa.selenium.chrome.ChromeOptions;
 20 
 21 /**
 22  *
 23  * @author 16939
 24  */
 25 public class EmailAutomation {
 26     
 27     //The survey link should be retrieved from the maingui - email tab - text field
 28     private static final String SURVEYLINK = "https://www.surveymonkey.com/analyze/browse/Q3QX6VcTZ8y_2F2w6PdfKMEGvjwPbqWiR4VVbxqErVN7g_3D";
 29     //The class date should be retrieved from the main gui - email tab - TF/CB???
 30     public static String CLASSDATE;
 31     static DefaultListModel emails = new DefaultListModel();
 32     public static int  progressCounter = 0;
 33     
 34     public static void setProperty(){
 35         System.setProperty("webdriver.chrome.driver", "/Users/16939/Desktop/MyFirstGUIProject/Nest/chromedriver");
 36         
 37     }
 38     public static ChromeOptions headless(){
 39         ChromeOptions options = new ChromeOptions();
 40         options.addArguments("--headless");
 41         return options;
 42     }
 43     public static void waitSec(int seconds) throws InterruptedException{
 44         TimeUnit.SECONDS.sleep(seconds);
 45     }
 46     
 47     public static void main(String[] args) throws InterruptedException, AWTException {
 48         setProperty();
 49         //ExtractEmails(SURVEYLINK, CLASSDATE);
 50         //sendEmail(EmailSubjectTF.getText(), BodyTextTF.getText());
 51         sendEmail();
 52     }
 53     
 54     public static DefaultListModel ExtractEmails(String surveyLink, String classDate, String surveyEmail, String surveyPassword) throws InterruptedException{
 55         CLASSDATE = classDate;
 56         WebDriver driver = new ChromeDriver();
 57         driver.get(surveyLink);
 58         logIntoSurveyMonkey(driver, surveyEmail, surveyPassword);
 59         waitSec(15);
 60         getTotalRespondentNum(driver);
 61         waitSec(2);
 62         MainGUI.mainGUI.threadProgressBar().start();
 63         while(notRespondent1(driver)){
 64             //A PROBLEM  is that it is not printing out the first email (the latest respondent)
 65             if(checkClassDate(driver)){
 66                 String emailAnswer1 = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[3]/div[3]/div/div/div[3]/div[2]/div/div[2]/div/div[1]/div/div[2]/div/p")).getText();
 67                 if(!emailAnswer1.equals("")){
 68                     System.out.println(emailAnswer1);
 69                     emails.addElement(emailAnswer1);
 70                 }
 71                 String emailAnswer2 = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[3]/div[3]/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div/div[2]/div/p")).getText();
 72                 if(!emailAnswer2.equals("")){
 73                     System.out.println(emailAnswer2); 
 74                     emails.addElement(emailAnswer2);
 75                 } 
 76             }
 77             progressCounter++;
 78             waitSec(5);
 79             waitSec(5);
 80             driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[2]/div/div/a[1]")).click();
 81       }
 82         MainGUI.mainGUI.ExtractionProgressBar.setValue(MainGUI.mainGUI.ExtractionProgressBar.getMaximum());
 83         return emails;
 84     }
 85     
 86     public static String getTotalRespondentNum(WebDriver driver){
 87         String respondentNumber = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[2]/div/a")).getText();
 88         System.out.println(respondentNumber);
 89         MainGUI.respondentsTotal = Integer.parseInt(respondentNumber.substring(respondentNumber.length()-1));
 90         return respondentNumber.substring(respondentNumber.length()-1);
 91     }
 92     
 93     public static boolean notRespondent1(WebDriver driver) throws InterruptedException{
 94         waitSec(15);
 95         String respondentNumber = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[2]/div/a")).getText();
 96         if(!respondentNumber.equalsIgnoreCase("Respondent #1")){
 97             return true;
 98         }
 99         else{
100             return false;
101         }
102     }
103     
104     public static boolean checkClassDate(WebDriver driver){
105         //For some reason, the date question has several different xpaths- if this is encountered, then we can make a method which checks the valid xpaths 
106         
107         if(driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[3]/div[3]/div/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div/ul/li/span")).getText().equals(CLASSDATE)){
108             return true; 
109         }
110         else if(driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/table/tbody/tr/td[2]/div[3]/div[3]/div/div/div[2]/div[2]/div/div[2]/div/div[2]/div/div[2]/div/ul/li/span")).getText().equals(CLASSDATE)){
111             return true;
112         }
113         else{
114             return false;
115         }
116     }
117     
118     public static void logIntoSurveyMonkey(WebDriver driver, String username, String password) throws InterruptedException{
119         waitSec(5);
120         driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div/div[2]/div[1]/div[2]/div/div/p[1]/a")).click();//Login via gmail
121         waitSec(3);
122         driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input")).sendKeys(username + Keys.ENTER); //Putting in email
123         waitSec(3);
124         driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input")).sendKeys(password + Keys.ENTER); //Putting in pw
125     }
126     
127     @SuppressWarnings("unchecked")
128     public static void sendEmail() throws InterruptedException, AWTException{
129         DefaultListModel testEmails = new DefaultListModel();
130         testEmails.addElement("daniel@gmail.com");
131         testEmails.addElement("olivia@gmail.com");
132         testEmails.addElement("eggplant@gmail.com");
133         WebDriver driver = new ChromeDriver();
134         driver.get("https://www.gmail.com");
135         waitSec(5);
136         driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input")).sendKeys("16939@students.isb.ac.th" + Keys.ENTER);
137         waitSec(3);
138         driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input")).sendKeys("reDblue0" + Keys.ENTER);
139         waitSec(5);
140         driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div/div/div/div[1]/div/div")).click();
141         waitSec(5);
142         //Emails list needs to be edited so it gets every element(email) within the DefaultListModel
143         Robot bot = new Robot();
144         String [] emails = new String[testEmails.size()];// will eventually change to EmailAutomation.emails.size()
145         for(int i = 0; i < testEmails.size(); i++){ // will eventually change to EmailAutomation.emails.size()
146             emails[i] = testEmails.get(i).toString();// will eventually change to EmailAutomation.emails
147             for (int j = 0; j < emails[i].length(); j++){
148                 bot.keyPress(emails[i].charAt(j));
149                 bot.keyRelease(emails[i].charAt(j));
150             }
151             bot.keyPress(',');
152             bot.keyRelease(',');
153         }
154         
155         /*driver.findElement(By.xpath("/html/body/div[20]/div/div/div/div[1]/div[3]/div[1]/div[1]/div/div/div/div[3]/div/div/div[4]/table/tbody/tr/td[2]/form/div[2]")).sendKeys(EmailAutomation.emails.toString()
156                 + Keys.TAB + emailSubject + Keys.TAB + emailBodyText);*/
157         //driver.findElement(By.xpath("/html/body/div[17]/div/div/div/div[1]/div[3]/div[1]/div[1]/div/div/div/div[3]/div/div/div[4]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div/div/div[4]/table/tbody/tr/td[1]/div/div[2]/div[1]")).click();
158     
159     }
160 }
161