Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import java.util.Random;
  5. import java.util.concurrent.TimeUnit;
  6. import org.apache.poi.hssf.usermodel.HSSFSheet;
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.Row;
  10. import org.openqa.selenium.Alert;
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.Keys;
  13. import org.openqa.selenium.UnhandledAlertException;
  14. import org.openqa.selenium.WebDriver;
  15. import org.openqa.selenium.WebElement;
  16. import org.openqa.selenium.chrome.ChromeDriver;
  17. import org.openqa.selenium.support.ui.ExpectedConditions;
  18. import org.openqa.selenium.support.ui.WebDriverWait;
  19.  
  20. public class MassEmailTool {
  21.  
  22. public static void main(String[] args) {
  23. // TODO Auto-generated method stub
  24.  
  25.  
  26. // =======================================
  27. // Credentials
  28. // =======================================
  29. String CUsername = "bhavin@acetuitioncentre.com.au";
  30. String CPassword = "Star2016";
  31. // =======================================
  32.  
  33. // =======================================
  34. // Tool Settings
  35. // =======================================
  36. int SEmailsToBeSent = 100;
  37. int SStartFrom = 1;
  38. // =======================================
  39.  
  40.  
  41.  
  42. /**
  43. Setting Up Selenium
  44. **/
  45. String currentDir = System.getProperty("user.dir");
  46. String marionetteDriverLocation = currentDir + "/chromedriver.exe";
  47. System.setProperty("webdriver.gecko.driver", marionetteDriverLocation);
  48. WebDriver driver = new ChromeDriver();
  49.  
  50. /**
  51. Gmail Link
  52. **/
  53. String pageLink = "https://accounts.google.com/ServiceLogin?service=mail#identifier";
  54. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  55.  
  56. // Connect To GMAIL
  57. driver.get(pageLink);
  58.  
  59.  
  60. // =======================================
  61. // Excel Settings | A = 0, B = 1 and so on
  62. // =======================================
  63. int excelRow = 1;
  64. int excelSchoolColumn = 0;
  65. int excelEmailColumn = 4;
  66. int excelStatusColumn = 5;
  67. String emailPrint;
  68. boolean valid = false;
  69. // =======================================
  70.  
  71.  
  72. try {
  73.  
  74. // Enter Email Address/Username
  75. WebElement userName = (new WebDriverWait(driver, 10))
  76. .until(ExpectedConditions.visibilityOfElementLocated(By.id("Email")));
  77.  
  78. userName.sendKeys(CUsername);
  79.  
  80.  
  81. // Click Next
  82. driver.findElement(By.id("next")).click();
  83. Thread.sleep(2 * 5000);
  84.  
  85. // Enter Password
  86. WebElement passWord = (new WebDriverWait(driver, 10))
  87. .until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
  88.  
  89. passWord.sendKeys(CPassword);
  90.  
  91.  
  92. // Press Sign In
  93. driver.findElement(By.id("signIn")).click();
  94. Thread.sleep(2 * 1000);
  95.  
  96.  
  97. for (int xx = SEmailsToBeSent; xx > 0; xx--) {
  98.  
  99.  
  100. //Locating Data using Excel Settings
  101. InputStream inp = new FileInputStream("wb.xls");
  102. HSSFWorkbook wb = new HSSFWorkbook(inp);
  103. HSSFSheet sheet = wb.getSheetAt(0);
  104. Row row = sheet.getRow(excelRow);
  105. Cell school = row.getCell(excelSchoolColumn);
  106. Cell done = row.createCell(excelStatusColumn);
  107. Cell email = row.getCell(excelEmailColumn);
  108.  
  109. //Checking if the email exists
  110. if (email == null) {
  111. emailPrint = "No Email";
  112. done.setCellValue("No Email");
  113. valid = false;
  114.  
  115. System.out.println(school.toString() + "\n" + emailPrint + "\nNot Sent\n----------------------");
  116.  
  117. } else {
  118. valid = true;
  119. emailPrint = email.toString();
  120. }
  121.  
  122. //Setting up a keyboard shortcut
  123. String cntrlB = Keys.chord(Keys.CONTROL, "b");
  124.  
  125.  
  126. // =======================================
  127. // Email Settings
  128. // =======================================
  129. String ESubject = school.toString();
  130.  
  131. String EMessage = "Dear " + school.toString() + ",\n" + "\n"
  132. + "Over the past year, we’ve reached out to a selected number of schools, both public and private to use our primary math software. With all the feedback provided by all the teachers that has seen our software improved by leaps and bounds in the way our software can help teachers bring out the best in their children."
  133. + "To continue our passion and commitment to help all teachers out there, we are now making S.T.A.R. Maths Online free for everyone to use!"
  134. + "\n"
  135. + cntrlB + "Our innovative software will allow all teachers to:" + cntrlB
  136. + "- Create unlimited online math accounts for all their children."
  137. + "- A simple and intuitive year planner to assist teachers with supplementary homework to reinforce what was learnt in class."
  138. + "- Unlimited NAPLAN practice questions for years 3 & 5!"
  139. + "- Robust NAPLAN and progress reports throughout the year so that you are always informed of your children’s progress."
  140. + "- We follow the Australian Curriculum guidelines set out by the Board of Studies."
  141. + "- Link parents to their child’s account to view their results throughout the year."
  142. + "If you think this software can assist any teachers at (School’s name), please forward this email to them as registration and usage of our innovative software are free!"
  143.  
  144. + "- A link to our website and registration page are as per below:
  145. Website: http://starmathsonline.com.au/
  146. Registration: https://teacher.starmathsonline.com.au/Account/Register
  147.  
  148. We understand what it takes to be a teacher and we’re here to help!
  149.  
  150. + "We know that our teachers are passionate and are always looking for ways to bring out the best in our students. At S.T.A.R. Maths Online, we've built our software to assist our teachers in doing just that!"
  151. + "\n" + "\n"
  152. + "S.T.A.R Maths Online was built to create a fun and interactive environment for kids to learn Maths as well as alleviate some of the administrative burdens on our teachers."
  153. + "\n" + "\n"
  154. + "Our vision is to build an amazing software that would bring out the best in our students and a powerful tool for teachers to manage their classes."
  155. + "\n" + "\n" + cntrlB + "How do we benefit the kids?" + cntrlB
  156. + "– A fun and interactive learning environment where students will enjoy spending time learning Maths."
  157. + "– As students progress and excel, S.T.A.R Maths Online will automatically increase the level of difficulty to match their potential."
  158. + "– When students are having difficulties with certain concepts, our software will set the questions to be easier ensuring that their gaps are filled before they progress."
  159. + "\n" + "\n" + cntrlB + "How do we benefit the teachers?" + cntrlB
  160. + "– S.T.A.R. Maths Online follows the NSW curriculum guidelines set out by the board of studies."
  161. + "– A powerful and easy tool to plan out the year’s homework schedule while having the flexibility to adjust the teaching program weekly."
  162. + "– All questions completed by students are automatically marked and results are fed back live to your login."
  163. + "– An option to request questions to be added to S.T.A.R Maths Online tailored to a concept you have taught in class."
  164. + "– Full reports of results throughout the year to ensure that no information is missed."
  165. + "\n" + "\n" + cntrlB + "How do we benefit the parents?" + cntrlB
  166. + "– A parent’s portal to ensure parents are part of their child’s educational journey!" + "\n"
  167. + "\n"
  168. + "We firmly believe that classroom teaching supplemented by computerised education can bring out the best of every student and reduce the amount of time teachers spend on administration."
  169. + "\n" + "\n"
  170. + "As part of our pilot launch of S.T.A.R. Maths Online, we will be providing teachers and schools with free access over the next year starting from term three 2016!"
  171. + "\n" + "\n" + "https://starmathsonline.com.au/register/" + "\n" + "\n"
  172. + "If your school would like to see a demo of our innovative software before trialling, please let us know and we are more than happy to visit your school and conduct a demo.";
  173. // =======================================
  174.  
  175. //Random number generator code
  176. Random randomGenerator = new Random();
  177.  
  178. //If Email exists
  179. if (valid == true) {
  180. Thread.sleep(2 * 1000);
  181.  
  182. //Click "Compose" Button
  183. driver.findElement(By.xpath("//div[@role=\"button\" and text()=\"COMPOSE\"]")).click();
  184.  
  185. //Type the school's email address
  186. String sendTo = "javatestingtomcruise@gmail.com";
  187.  
  188. //A code to counter gmail's bot detector
  189. try {
  190. WebElement receiver = (new WebDriverWait(driver, 10))
  191. .until(ExpectedConditions.visibilityOfElementLocated(By.name("to")));
  192. receiver.sendKeys(sendTo);
  193. } catch (UnhandledAlertException eee) {
  194. Alert alert = driver.switchTo().alert();
  195. alert.accept();
  196. WebElement receiver = (new WebDriverWait(driver, 10))
  197. .until(ExpectedConditions.visibilityOfElementLocated(By.name("to")));
  198. receiver.sendKeys(sendTo);
  199. }
  200.  
  201. // Subject
  202. WebElement subjecto = (new WebDriverWait(driver, 10))
  203. .until(ExpectedConditions.visibilityOfElementLocated(By.name("subjectbox")));
  204.  
  205. subjecto.sendKeys(ESubject);
  206.  
  207.  
  208. // Message
  209. WebElement message = (new WebDriverWait(driver, 10)).until(ExpectedConditions
  210. .visibilityOfElementLocated(By.xpath("//div[@aria-label=\"Message Body\"]")));
  211.  
  212. message.sendKeys(EMessage + "\n " + email);
  213.  
  214. //Random intervals between each emails
  215. int randomInt = randomGenerator.nextInt(11) + 4;
  216. Thread.sleep(randomInt * 1000);
  217.  
  218. // Send Mail (Press Enter)
  219. String keysPressed = Keys.chord(Keys.CONTROL, Keys.RETURN);
  220. message.sendKeys(keysPressed);
  221.  
  222. //Update The Excel File
  223. FileOutputStream fileOut = new FileOutputStream("wb.xls");
  224. done.setCellValue("Email Sent");
  225. wb.write(fileOut);
  226. fileOut.close();
  227. excelRow++;
  228.  
  229. System.out.println(school.toString() + "\n" + emailPrint + "\nSent\n----------------------");
  230.  
  231. }
  232.  
  233. //If email wasn't found
  234. if (valid == false) {
  235. //Update The Excel File
  236. FileOutputStream fileOut = new FileOutputStream("wb.xls");
  237. done.setCellValue("Email Not Sent");
  238. wb.write(fileOut);
  239. fileOut.close();
  240. excelRow++;
  241. }
  242. }
  243. //Errors collector
  244. } catch (Exception e) {
  245. System.out.println("Error");
  246. System.out.println(e);
  247.  
  248. }
  249.  
  250. }
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement