Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

Can I send email and add an attachment using Java in Selenium Webdriver?

While running a test case, I have a try/catch mechanism and an onevent listener that produces a screenshot if an error/failure occurs. Can I also compose and email and attach that screenshot there and send it to someone?

Upvotes: 0

Views: 6371

Answers (3)

Abhijeet Vaikar
Abhijeet Vaikar

Reputation: 1636

Yes. If you are using a linux box for executing your selenium test cases, you can install mutt, a CLI mail client to send mail with attachments. Also, there is JavaMail API that you can use. JavaMail API example

If you want to use mutt, first install mutt. Then create a file .muttrc in your home folder and include the following parameters for e.g:

set smtp_url = "smtp://[email protected]@smtp.gmail.com:587"
set smtp_pass = "password"
set realname = "Foo Bar"

Example:

mutt -s "test mail" [email protected] -a attachment.zip < email_body.txt

You need to include this command in a shell script and execute the script from a Java class using Runtime.getRuntime().exec(myShellScript);

Upvotes: 0

Qwerky
Qwerky

Reputation: 18435

This is more of a job for whatever is running your integration tests. Typically one runs integration tests on some kind of Continuous Integration environment, such as Jenkins. Your CI environment should support emails/notification of failing tests.

Upvotes: 0

Karthikeyan
Karthikeyan

Reputation: 2724

There is nothing to do with selenium here, You have to Check this link and override the attachment file with your Screenshot file.

Upvotes: 5

Related Questions