How to Send Emails in Selenium Webdriver Using Commons email API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone this is mukesh from learn - adoption comm today in this video i will talk about how to send email after text execution in selenium-webdriver so till now I have covered so far like how to trigger email from Jenkins right so we have some default settings in Jenkins there we can set up some Gmail's and some simple smtp server settings and you can send emails but most of the times it happens like we don't have to use Jenkins or we use some other CI tools and we have to send email through that right so you have so many options like you can use certain plugins like postman plugin of maven or you can use some other help of CI but in this video we'll talk about one API which will help you to send email directly from your code itself so we'll talk about one new API called Commons API so full name is Commons email API and we'll also talk with different options which we can use in comments API so this Commons API Commons email API has options like you can send simple email multimedia email and so many options are that I will show you in few minutes then I will write a very basic program to send email using this API and once they're done with this I will show you how you can integrate this with your existing selenium test and you can also add certain conditions you want to send email after every execution or based on certain conditions like if test case powers of failing okay so let me go back so you just start with comments email just note down this name comments email so when you search you will get this comments APA it is also I will say not also it's part of your existing email API so previously we have used Java email API and validation or activation Java files to send email so this email comments email ap is on top of that so you can see this email is built on top of Java mail API why they created additional API now if you go back to my one of the post which I had written long back we use the same Java mail API if you see here and we notice we had very lengthy code so we used our email a pay and if you see the code we have very lengthy code and it's a little bit complex as well right so just to avoid this complexity they have created another API which is really very easy you just have to call simple methods and you can send emails so they have created like multiple classes interfaces methods to send email and these are the format's which they will support so you can send simple email multi-part email HTML email image with HTML and email attachment so I am going to show you the simplest one today and the second videos will talk about the different options as well so if you're using normal of Java project you can go and download the zip file and you will get one jar file and if you are using this approach you also have to add two different Java sorry jar files separately because internally it use that Java email and activation jar file but if you are using maven you don't have to do this for maven user you just have to use one dependency and everything will comes automatically ok so you can see it's quite easy if you want to see how to use if you click on user guide you can see here by default if you want to send simple text email this will be the code just create an object send host server gives authentic authentications make this settings to true from subjects message add and that's all send and if you want to send with attachment then they have all these options so we'll talk about this as well as just now let us try with simplest program and check whether it's working fine or not so we'll go back to eclipse so if you have normal Java project you have to add download this API plus you have to download two more APs but if I am a VIN user you just have to add one dependency so I will create a maven project because I don't have maven project as of now and I will simply give a name email sent and I will simply create a project if you do not know what is maven how to create maven project you can refer my previous videos as of now if you are using the latest version Eclipse by default you will get an options to create maven project and by default this will be the structure as of now I just have to go directly to format xml file and if I click here format xml i have this poem dot xml so first of all i need to create when dependencies tag which is a parent tag for all the dependencies here i can keep adding the dependencies so they have dependency for comments email if you just simply search a comments email you will get this option apache 2 dot and this is just dependency so just copy this four lines of dependency and paste here and press ctrl s and when you do ctrl s it will download all the dependencies and it will add in your project so now if i go back to my project i will see maven dependency and you can see here i have three jobs here one is comments email another is mail java mail EP and third one is activation 1.1.1 jar if you're using normal java project you have to download all these three and use so this is what we need to do from installation or configuration point of view now let's create a fresh class and I will say send email Java package I will create I'd say demo one and I will take public static void main so this is my default program which I am creating so we can directly copy paste answer because it's very easy code if they have just predefined methods you just have to use now when you copy paste it will ask you what is this email to put mouse over and it will give you suggestions that you need to import this mail class from commons dot mail again we have to import this class which is also coming from Commons made now one last constructor which is default authentical it is also removed from that email okay so these methods will throw you some exception so just put mouse over and put throw exception if you want to use try catch also it's fine but I will use throws as of now now this is what is the program now you need to give some valid settings okay so first of all I have to give some valid hosting like from the from which server I have to trigger this email so I'm using Google of Gmail SMTP server but if you have your company network you can give that SMTP server details if you don't have you can contact your IT team or some if somebody else using the SMTP server you can take so as of now the smtp of gmail is smtp.gmail.com port is 465 here you need to enter the valid credentials so I am using Gmail so I will use my dummy account selling in 3 by Mukesh at the 8 gmail.com so this demo at the rate 1 2 3 4 5 so if you want to set who is sending this email you can set like email is coming from mukesh at the gmail.com subject is selenium test report from selenium add 2 to whom you want to send so I will give my 80 here okay so 28 the return as an automation calm and just press control s if you want to check you can put some sets out statement set just to check whether your code is working fine or not I will say here just started and finally will say email sent it's very easy code just you need to remember one class and that's all once a great object of simply mail you will get email object and then you can call these methods set hostname as in report get the credentials make SSL connect on I will say true set from subject message add ensign so let's right click and run as java application let's start it and it will take some time to connect it to SMTP server and it will send emails it will come like email sent or just completed email sent so I will get one email okay and you can see this is email which I got selenium test report is the subject that we have set and this is the body this is test email from selenium and this is the subject authority the person who want to send that report or email it's quite easy to use right so what I will do I can create a method for this and we can call this method whenever we want so to reuse this code what we will do we will put this so if simplest way is you can surround this piece of code and you can say right-click refactor as extract method yes you can see it is Eclipse feature if you want to create a method from certain piece of code you can surround that code right click and say extract as a method method name I will give send email I will make it public click on OK so you can see now automatically Eclipse has created this method for us send email or send email and I will put this as static so whenever I want I can directly start using it so this is my class ready class name is an email Java and method is send email now how to use with this engine so as of now I don't have testing you so I will quickly add this lesson G so I will go back to maven to post your comment I will search for test ng so this is Justin G the latest version is 6.11 I don't think it is quite stable so I will take 565 6.9 dot 10 and I will simply copy/paste this dependency and if I check the Maven dependency yes the stingy jar file added now you can start using testing G so let's say if I have created one test I will say create new others if you want like you can directly create I am using using test in G class name I can give my test one a one before class and after class click on finish so you can see this is the default structure I got from testing G so after test I want to call that method so again I will use same email dot Java dot send email it throws some exceptions so I can either use strike H or throws before class I can write anything if which I want so this is just a dummy program so I will just write anything like starting the test and in the test case I can write as of now let us say I have two numbers in to be equal to 100 and a sir dot assert equals a and B I'm just letting a dummy test so what will happen once this class or test will drain it is starting the test it will run the test and after the class it will send email right so here I will just write one statement like done so I click run as the Singhji test so you can see here is starting the test test then notice running this after class and here it will send one email again it takes some time to connect to the server and send email so that's fine you can wait and you will see it will send an email so right now it does not matter what is the condition every time it will send one email rate because here we have not given any condition so now I got one email again which is email sent successfully now we need to add certain condition if my test failed then only I have to check my emails or I should get one email so now if you go back to one of my post which I have written like capture the screenshot on failure okay so if you go back here we have used one after method and after method we have used one a small piece of code right which checks the result at runtime and if result is equal to failure it should take the screenshot in this example but in our case we'll send an email so just remove this unnecessary emails okay so you can use one after method of testing G which is this flexibility that you can use that status as a like parameter and based on the status or results you can trigger the either the email code or screenshot code so here you can use one eye test result so this is one of the interface from testing G and here you can check so whatever result will come all the result details will be in this result object so it can put if result status equal to equal to failure so I test result the result of the test case equal to failure than send email okay so this is what we have our condition as of now so now this time what I will do I will just first let me do in a happy flow this time my test will not get fail so this will not send an email so this will only then when the test fails so it will say test fail an email sent in case no err no failure it will not send email so right click run as test in G test and you will see this time it will not send email because test is passed it pass now this time let me do let me forcefully put wrong value I am comparing if 90 equal 200 definitely not then it will come and it will send one email if you want you can remove this before collapses of no use as of now it is created at Clouseau I have taken so you can make the changes based on your conditions and you can see it says test fail and email send now you have this dynamic condition if test filled then only it will trigger email because if you keep sending email for every test execution it is not good and you can see this is email which I got so it's quite easy to use this library I will suggest you to go through this API once again and very easy code they have written if you want to try from your ndele so you can try like if you want to attach an attachment then what will be the code HTML formatted email and different things okay so it is just another wrapper if you want to use the pure code still we are using the same Java email API which we have used previously but I found it's quite easy because they just have removed unnecessary code I will not say removed they have abstracted it so they have written everything in a separate jar in just have to use it so that's all I have for this video ok in the next video I will show you how you can attach an attachment and then send so I will take one complete selenium end-to-end test case and then we will see how we can integrate this API that's all I have for this video so if you liked this video kindly share and subscribe my channel in case if you've any doubt feel free to contact me at location at learn I hope not emotion not calm thank you so much have a nice day Popeye
Info
Channel: Mukesh otwani
Views: 62,495
Rating: undefined out of 5
Keywords: Selenium Webdriver Tutorial, Selenium, selenium tutorial for beginners, How to Send Email in Selenium Webdriver
Id: qGq9K85mGyA
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Tue May 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.