Posting messages to Slack using Java (Test Automation)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys today we are gonna learn how to post messages to slack using test automation framework so as a part of this video what i'm gonna show you is how to post misses using a simple java project and the same code can then be integrated to your automation framework also in order to post messages in the form of automation status execution report and other message also like you know log application logs automation logs and all so to start with uh we are going to proceed this particular session in two parts basically in first part i'm going to show you how to configure slack account uh that's gonna take like maybe five to seven minutes i'll just take you through all the steps because that's the basic prerequisite thing which we have to do and it's really important and after that i'll take you to the java a simple java project where you can just integrate the code and then you know run it to post the messages so let's start all right so i need to navigate to a few urls to do that i have made a note of it to make it quick so this is a slack app url which you have to go and navigate the first step here is okay simulation problem yeah so first thing what you have to do is you have to set up a workspace and to do that you need to click here create a new workspace like this and then it will ask you to log in you know using your account so if you do not have any account created you can use a gmail address directly i'm using my personal gmail address here you can see i have logged in now you can see the user account and then a button is enabled for me to create a workspace so i'm gonna click here it's gonna trigger me to create the workspace it'll ask for a few basic inputs to be provided so first of all i have to provide a name of the workspace i'll put it as automation demo workspace right it's going to ask you the basic information about how many members are working along with you in your team you must put any number this is where it will ask you to provide the email address or account of people who want to access this workspace for now i'm going to skip this step it's loading now okay so here we can see yeah so now you can see the workspace is created you can see the workspace name here all right it comes with an url so if you copy this url launch it it's going to directly take you to the workspace okay the next step towards this is to create an app and to do that we'll be navigating to the app api of slack the url is this so it says api.com so here you will see an option to create an app you have to click here create an app create a new app from scratch provide any new app here so i'm going to keep the same naming standard automo app and then it'll ask you to add the workspace also so if you remember we have created a workspace here in the first step this is my workspace name automation gmo workspace so i'm gonna use that name here it's gonna show in the first list first option in the list here let's just select it and then create app all right the app is created the next step which we have to do is we have to create a channel this is the channel where messages will be posted in slack so you have to go back to the workspace url and then beside channel you'll see a plus icon plus button here click and then create a channel you can't put any name here so what i'm going to do is automation demo channel just following the same name standard which i have used there for workspace and app okay so i'm gonna just create it now it also will ask you to provide the email address who wants to access this but i'm gonna skip this for now okay so you can see here the new channel is created which is the name of the channel all right so uh we have to enable few things inside api.slack.com the first thing which we have to enable is you know incoming webhooks so you can see incoming webhooks here you need to click here you will see a slider you just need to tap here now it's enabled you can see a few things listed here in the form of curl so this is a this is basically the curl command but we have to get a webhooks url for that we have to add this new webhook to the workspace so right now we just created the web book we haven't added to the workspace now with this tape we will be adding it to the automation demo channel which is ultimately you know associated to the workspace which we have created and then we just need to click allow button here all right so now you can see here we'll get a webhooks url we need to copy this because we'll we'll leave this in our automation test i'm gonna just save this webhook url sorry for the typo right the next thing which we have to do is we have to enable oauth and permission for that we need to go here to our permissions since you navigate to this particular screen you will see part user auth token you need to copy this and store this as well so [Music] next thing which we have to do is we have to enable fuel scope for bot token scopes so we just need to add two methods here file read and file right right provided to method you can see here files read and files right that's all we need to do here in the setup of the apis for slack in order to load this all this configuration which we have done in this app we just need to reinstall app by clicking it here we'll ask you to select the channel once again we just need to select the channel click allow that's that's all so now you can see we have got a success message it means the if the apis which we have enabled for the this particular channel is now you know active all right so the next thing which okay one last thing which we have to make a note of is automation channel sorry slack channel which we created we would need this as well for our you know coding i'm gonna copy it from here so now let's go to uh the second part of the video where we'll learn how to set it up in the automation project or maybe a simple job project so i'm gonna use intellij for this i'm gonna show you by creating a simple java project ah this is existing one which i have used maybe i'll close it up and then i'll create a new project i'm creating a maven project because because i'll be using ramen dependencies slack integration for demo let me finish it up a new project is created here we need to provide the dependencies and in dependencies we have to provide dependency for three things first thing is uh jslac the second thing is http client and then http name so i'm gonna just create the sample here i'll pull out those dependencies from google from maven depository central repository i'll say jslac maven so this is one which we have to add in just like is added and then we have http client apache http client this is the one which we have to add as well this is for api interaction another one is http meme so i have added these three dependencies only for now because this one i just want to run you through basic coding for this particular work i've just resolved the dependencies everything is fine now what i'm going to do is i'll create a simple class file here select [Music] test okay i'll create a void method main method basically let's check everything is fine in this project okay so now you can see the project is created we are able to print it i'm gonna create a method which should do all the necessary work for us okay so public static void send message to slack i'll parameterize it with string variable where i'll pass in message before this i'll have few private variables created private static string first one is webhooks url second variable is or token third variable is your slack channel okay if remember i have noted all these things when i was setting it up in and slack app so i'll just copy these values and put it here this is your web books url this is odd token this is channel name i'll just verify once again the oauth one because when you when you load it it changes sometimes so we'll make sure that i'm putting up the right thing here this is copy right we're done with the variable creation now let's write the code here to invoke this using api i'll first create string builder and name it as message builder next step is is about you know appending the message which we have created as a variable as a parameter to this so message builder plot append we will be using it further in our code then we are going to create will create the payload you know instance and this payload is basically just going to have the data that you want to push to slack okay so payload dot enter and we will invoke the channel here channel name is variable name which we have put in here slack channel text and then message builder is yeah this is builder the one which we created in line number 15. we'll convert this to string to make it proper dot build this is done another piece is where we will create the webcast response web webhook response and this is is going to basically send uh the the messages to slack you know communicator so slack.instance dot send what it's gonna say it will be sending your payload which you have created just before this and this payload will be sent to the url which we have stored here all right so we will just add this exception handling also there here so that we will not get any unexpected errors all right and so we have we are released the code now let's try to execute and see how it works so i'm going to call this same message to slack will first message to snap after using automation let's from this and see how it goes on building the project right now okay so this execution is done we will not check in slack if the message is posted all right so you can see now here clearly the message which we entered there once the once we when we executed it it's getting posted successfully here okay now let's try to post one more message and see if it is working fine okay and i'm just gonna add a gain word after this we'll run it okay you can now see the second message also posted successfully so now now we can see here that the utility which we have created is working fine here you can pass payload in the form of json as well okay you can pass and you can create any message in the form of string and pass it to this particular method in your automation you can use it in your listeners also in test listeners or in consumer listeners or enjoy unit listeners and you know post these messages in after suite or you know in hooks commands to the slack so that uh you know it's gonna be easy for anyone to just go and check check the status of automation execution uh in communicator rather going to any particular location okay so this makes it uh you know even easier for anyone to access this uh status of automation execution directly in communicate rather referring to any email or any location so that's all about today you know video maybe you have you would have liked it let me know if you have any questions around it you can post it in the comment we'll try to answer it as soon as possible thanks everyone you
Info
Channel: Jiten Automation Lab
Views: 570
Rating: undefined out of 5
Keywords:
Id: L1v-lSVisW4
Channel Id: undefined
Length: 20min 50sec (1250 seconds)
Published: Tue Sep 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.