Automation, particularly UI automation is a way to reduce the manual work done in any project. It also helps to reduce the time taken in completing the repetitive work. By showing the results of the automation, we can explain the functionality of the project to the client and stakeholders. However imagine a situation when the client is not available currently to see the demo or the developer is busy and cannot present the demo. Is there a way to somehow record video of automation output in Selenium and show it to the respective people later? The answer is Yes.
Selenium provides us with the feature to run the automation code/suite and record the output as a video and save it on your system. This video can then be shared with anyone who can see what the automation is doing.
Let’s see how we can achieve this feature. I will try to explain those parts of the code needed to record the video; rest parts of the code will remain same.
1. First of all, to enable video recording, we need to download some important jars. They are freely available to download from Google. Below are the jars to be downloaded. These are the latest version available, any other version can also be downloaded:
ATUReporter_Selenium_testNG_5.1.1 and ATUTestRecorder_2.1
2. Once downloaded, we need to import these jars into our project build path folder. Please see below how to do that:
Go to your project folder –> Right Click –> Click on Build Path –> Click Configure Build Path –> Go to Libraries tab –> Then Click Add External Jars button and add the two downloaded jars –> Click OK. Please refer below screen:
3. After this you need to import below modules in your program to get access to classes and objects of those two jars:
4. Next we should create a folder where we need to save the videos. Ideally the folder can be created anywhere on system but it’s better to save them in our project Folder. So create a folder with name ‘videos’. You can choose any other name also. Creating the folder in project directory helps in accessing the folder using user.dir feature.
5. Now we need to write the code to record video of automation output. The following code shall be written in the method with annotation @BeforeMethod. By doing this we do not need to write this code in every method/test method or test case.
First create an object of ATUTestRecorder class. This object needs to be created at the class level so that this object can be used in all the test methods.
6. In the next step, we will use the recorder object to create video and define a name for the video every time the method is executed. Since we might be running the method multiple times daily, it’s better to include date and time in the name of the video. Otherwise every time a new video will be generated, the previous video will be replaced.
To do this we will create an object for DateFormat and assign this format to the object of Date class. Please see below the code for achieving this:
In the above code, first two lines create object to get date format. Third line of the code uses the recorder object to generate video, and then get the project directory using ‘user.dir’ feature and access the folder named videos in ‘user.dir’. Video will be generated with name ‘myvideo_yy-mm-dd HH-mm-ss’ format.
recorder.start() will start video recording
7. Use this code to stop video recording probably in @AfterMethod method: recorder.stop()
8. When you run the code, the video will saved in the directory with .mov extension. You can play the video to see the automation output. This video can be sent to anyone in the project for demo purposes.
9. We can also choose to delete the existing videos to free up the folder using below code. However this is not a mandatory step, you should decide on your requirement in case you need existing videos later. Here we will use the videos directory and use the File class object to delete all the videos in that folder:
We can also create a method to delete older videos by placing the above code in the method. The path of the directory can be passed as a parameter when calling the function.