2

I have my selenium test send an email whenever there is an error, this is hard coded into my test however I am looking to see if it is possible to call it from an external file so I can just edit the Email file instead of having to go into the code and change it.

The way i'm getting it to email is

try {

}
catch (Exception e) {
    e.printStackTrace();
    new SendMail("senderemail","reciveremail",e); } 
}

Which calls it directly from:

public SendMail(String fromMail,String tomail, Exception e ) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xxxxxxx","xxxxxx");

Question: How can I get my selenium to get a list from an external file of emails to send them out?


Edit: Updated code of what finally works.

String fileName = "/Users/cdolan/Desktop/liness.txt";
String Email = "";

try(BufferedReader br = new BufferedReader(new FileReader(fileName)) {  
    while ((Email = br.readLine()) !=null) {
        <Test code here goes here>
    }
}
catch(Exception e) {
    e.printStackTrace();
    new SendMail("email123@gmail.com",Email,e); }
}
  • I there a reason why you cannot read required parameters from a config file in SendMail()? – Peter M. Jan 23 '17 at 20:57
1

Before your SendMail class, you can include a file upload method that will upload a file, then you can read the lines of the file and pass those emails into the SendMail method in a loop:

Each email address would need to be on a new line with no blank lines.

String fileName = "c://lines.txt";

    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

        String line;
        while ((line = br.readLine()) != null) {
            //sends email to each line in the text file
            System.out.println("text line: " + line);
            new SendEmail("senderEmail", line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
  • I think your solution will work for me. How do I include a file upload method to upload the TXT file of names though? – Christian Jan 24 '17 at 0:04
  • If you update the path for your text file in the fileName variable the code to load the contents of the file is already there. With this code, all you will have to do is modify the text file and it will send emails to all the emails in that file. – Swagin9 Jan 24 '17 at 2:49
  • When I use new SendMail("senderemail", line); it tells me the constructor is undefined, even though my SendMail class clearly has it in the class. How can I fix this? – Christian Jan 24 '17 at 18:24
  • In your SendMail class, you're passing in a Exception variable. I don't think you need to pass this in, but depending on how you implement the loop you might still have use for it. But I would just make sure that the number of params in the call is the same number as the constructor calls for. – Swagin9 Jan 24 '17 at 19:13
  • 1
    Wanted to let you know I figured it out. It was right it just was not triggering the catch. I solved it. posting my answer in the thread. – Christian Jan 24 '17 at 22:48
1

Not sure why you want to send emails from your Selenium test code, but most companies let test-results be handled by a continuous integration server. Instead of writing your own test-error handling let the CI do this.

All CI-servers have options to send out e-mails to developers and testers when a test is failing. But it also has other means of reporting, like #Slack integration or sms. The CI-server can even send emails to the developers who broke the build. This by starting a test run for each commit/checkin.

Here some CI-servers for on premise

Or some in the cloud

Other reads:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.