 Hey everyone, my name is Tomer Cohen and today I'm here to speak with you about low-level Android automation with ADB Before my like before I start I like to stop my talks and presentations with the with them with him stands for what's in it for me And I want to speak a little bit about what what I want you to get out of this presentation so My biggest goal here is to get you to better understanding How adb works how to use it some cool commands that you might Like maybe you don't know And I'm going to share with you And then in the end I'm going to show a little bit how I Did automation with adb and python? Okay, so let's dive into it You recognize this guy This is me Like 20 years ago My name is Tomer. I'm from Israel. I'm 23 years old I've been automating since 2014 almost five years now and right now. I'm Android automation team leader in my organization, which means everything that is related to Android products is going through my My automation infrastructure and More than that I help other teams in the organization achieve better automation if it's just choosing the right tool or with automation design and everything and In my free time, I like to write so I created a blog about Automation and the automation design software design in general You can join although 99% of the Of the content is in Hebrew my language Yeah So oh was flipped Weird never mind So I'll speak about my journey a little bit and I don't know what happened to the slide So you just look at me My journey started as I said almost five years ago I started as a manual tester in the Android QAT and and We tested all the Android products the only products in my organization that were pretty interesting They didn't have any graphic user interface Okay, there were just services running in the background Okay, and what does this like what these? Products that what they were doing was just running in the background interacting with the other processes with other Android the system services that are running and like taking information about the system and It was some kind of like Security products on kind of like an anti virus that you don't you don't see it running and you don't see its Graphic user interface, but you know it's working in the background and this product was very interesting How would you test a product like this? What would you do? to communicate with it So we have We have adb. Okay, we used adb for the communication with the product with everything That is to the device and with adb we were able to Know everything we wanted about What's going on? Under the hood what we don't see Inside a device. So where's familiar with adb? Okay, most of the So adb is the Android debug bridge Okay, it was developed by Google in order for testers and developers to have Some kind of it's some kind of a tool for communicate command-lane tool for communicating With Android devices. Okay so We used adb for this communication and I'm going to show you in in this talk I'm going to show you how we used it and how we performed Basic I'm going to perform basic automation script with it So we use it manually at the beginning and after a few months my team leader came to me and said listen Tomer We want to create ourselves. We have to create our serves our own automation infrastructure And I want you to find one. You know what what we need to test You know what how the product works and you need to find something for us or to create So I went to the internet and looked for Android the testing Android automated the testing tools And there was a very long list you can look like tens of tools and The list was very overwhelming and I didn't know what to choose, but I noticed an interesting fact that 99% of the of these tools are for the Higher level of the device like for the graphic user interface for interaction with the device as a regular user Like as as a user that taking the device and then pressing buttons and do do things with the device and There was nothing for or there were a little bit, but it was nothing Really special for for interaction with the operating system so What we needed is Some lower level or low level tool for testing the for testing the applications So I wanted to go briefly about what what do I mean by low level by low level automation because There are many You can look at it in many ways. So I'll start by saying what it's not low level automation is not something It's not it doesn't have anything to do with the program with the program in language It's not an assembly code that tests some kind of a product and it's not a product And it's not an automation that Some kind of automation code that tests product that is written in assembly or in another program in low level program in language It's just all about the abstraction layers Okay, almost every software we see out there these days have It's built like almost every is built with layers of abstraction Okay, we as users use almost always the highest level Okay, if it's the form we use the screen if it's the arm, I don't know we can use the voice we can use many many kinds of Interactions with the highest level and down the way until we get to the actual logic that is running behind the scenes there is a long way to go right and low level automation to my eyes is just addressing or interaction with the The lower level. Okay, just being closer to your logic being closer to what you really want to have Okay, and it has its power We use low level automation or we use the low level tool because we didn't have any choice because we didn't have any UI okay, we didn't have any graphic user interface good so What do we think about what do you think about the low level automation? We think it's good. We think You should have it shouldn't have it. You should go straight from the highest level and then go down someone Yeah, again Yeah, it depends on the requirement, right? I Say in many cases not always but it can help you with achieving more reliable code Okay, because when you use many layers of obstruction when you interact with many layers of obstruction It can it means you run more code and I see more code as more potential bugs and When you interact with the lower levels You don't always have to have all the application on all the system up and running You maybe can just interact with what you want so for this for this thing another thing this is Good example for a layered layered architecture automation layered architecture of The upper Android operating system Which we as users use the application framework and down the way there is even Linux kernel that is running under the hood and With a DB actually you can interact with different With different layers and you can interact with Android services and I'm going to show you how how we do it in a few moments So we chose the Android debug bridge And it's just some kind of command line tool Okay, that's what we used to make it an automation tool was just wrapping it with With almost any program in language you have we'll have a way to just send commands to the terminal to the command line and we just dropped and added ADB at the beginning of the command and I'm going to show you in a second and Now any command you send to the command line is starting with ADB and the command you want to send Okay, and this was the basic infrastructure and we expanded it as we were going so a Little about the architecture or how things are connected behind the scenes or how it works ADB has three main elements the first one is the client which is responsible for the for the for creating the connections for creating the The requests to the device Okay, and there is a server ADB server is some kind of a process that is running You can start it manually or it's just start running when When when you start using it and when you use a command or you send a command from the client it looks for a server that is running on port 5 037 and It looks for a for it try to communicate with the server and then the server creates Sends their the commands to the ADB demo Which is just a process that is running on the device listening to the commands and then our response To the server and then back to the client and don't worry if it was a little bit complicated because I created a diagram Okay, so I have here a host with two Android devices connected to it story for the for the cloud quality of the diagram not so good at this things We have these two devices and when I start the adb server when I start using adb the adb server start running on the computer and we're going to see it in a second and Then I can create a connection or just send a command from the client and it's going from the server to the adb and The server has many roles here, but I cool cool thing. It gives us is just the ability to Connect from other computers to devices that are connected to a host Just think about it if if you are in let's say in your office, okay And there are many computers you can just communicate from computer that is sent this sitting in another room to your devices in your room, okay And it gives you some kind like in my in my organization We use this to create some kind of an internal cloud of our organization to just connect to other computers It was pretty cool So with this architecture, you can just connect from the client to the server and specify the host of the Devices and specify which device you want to use and then the request is going to the device and back Okay So this is architecture so adb has many many commands, okay, and I don't know all of them and it's like Very they have big interface and many things you can do. I created here what I called adb cheat sheet with with What it is just I think these are the most used commands And we're going to see not all of them, but I'm going to show you a little bit of what you can do with with the adb On a device we'll start by doing it mentally and then I'm going to show you how we created automation with it so We have here in these commands. We have a way to To See the connected devices to create a connection to a specific device to just push files pull files install applications uninstall enter the device shell like run run Being on the device shell and run sell some kind of Linux commands to the device Input events like Like buttons and the screen and swipe and tapping taking screenshots looking at in the Services and and the information that that of the services that are running behind the scenes. What's going on inside the Operating system we have access to all of it with adb and We can even you see the low cut which is one of the most powerful tools for testers as I see it Which shows you all the log and you can just if you're a tester you can just go to your developer or the guy who develops the Your product and say listen, please write logs to the low cut and you can filter it and and and see everything there That everything is working as expected. Okay so there are many things and Let's start for with the live demo and I hope I didn't prepare any I haven't prepared any Any backup video, but let's start by by looking at what we can do with it. So here we have Here we have an Android the emulator and Here I have Some kind of a command line like the CMD and I use adb This is the command line tool and we have here many things we can do With it with the device We saw many people use port forwarding for adb one of the best tools And one of the best thing I use is the adb shell So with adb devices we can see actually the devices that are connected To my computer. Okay. I can kill the server and show you That when I start using the devices when I start using adb when the server is down It just starts automatically. You see it's starting or now at TCP 5037. Okay, and now I have it running So with adb, I can enter. Let's say the shell of the device Okay, and we can do many things now. We have for example Access to the file system. We can do whatever we want. I can go to let's say To a directory to see files there to remove file Okay, to do whatever I want with this and it gives you like really great debugging option for the for the device Another cool tool that we have inside the Inside of the adb shell is the dumpsys which is the Current state of the device of the look how long it is. It's just gives you the all information Available about That the operating system what's going on right now behind the scenes that you cannot necessarily see so it was a very long list of Output I Can look at the dumpsys minus l command which shows me only the services I have Okay, and this is interesting because if you have Like a mobile application that does many things behind the scenes that you are not necessarily aware of Here you can start Researching and understanding what's going on Like if I go and there are all the services here I can ask for a specific service for example I want to know about the battery so we have here the battery you see and I can just say dumpsys Battery and I have here all the battery options. Okay, I can go with the window to see what's going on on the screen I can though I can go I can do actually whatever I want what Anything that is going inside a device Almost anything I Can have access from it. Okay So it's pretty powerful tool and now I want to show you shortly how how we used it for Automation, you know, which was pretty cool my eyes so You have any questions until Okay, so We just took Python and you can do it really with almost any program and language out there Tight Python was the only language In my organization was the most common language that we used so we took Python and And just wrapped it with these three methods as the basic infrastructure the first one as you can see is the Subprocess get output which just opens a process of the command line terminal whatever it is and send the command to it and gives you the output and then We wrapped it with a method. We called adb command adb command It's just a local command with adb in the beginning as I said earlier And then we added the adb shell command which will help what will help us to Create to create a shell commands and an interaction with the devices shell so Here's the basic infrastructure. It took us five minutes to write it. You don't have to understand a lot In order to to use it It's it's very simple and then and really We took us a really short amount of time and We enjoyed it a lot. So Now I'm going to Just to show a Little like short script or a scenario that I created. I'm not going to implement it now It's already implemented But here we're going to with adb and Python we're going to install an application to open the application to see if the application was written like an activity of the application was written in the logs and Then we're going to take a screenshot and pull it to our computer Okay, and it's just a basic scenario just for you to see how easy it is So here's the device and here's the scenario and let's go briefly about how How we implemented it or how how it's done so You can see. Yeah Okay So here we have the local command I just added Like the logs to so you can see the output on the screen The adb the shell command and then here's the main scenario Okay, I just added slips everywhere so you can see it on the screen. So it won't be too fast I added here another method install up again for you to see how easy it is we just Wrapped the adb command with install you can see it here and Now we have a install up. We added I downloaded the Flashlight application for example, and we're installing the application. We open it We're opening it with with monkey. You can there are few ways to open an application, but we open it this way Again it can be wrapped with another method. It's very very simple Then we took the log. We check if flashlight is in the log if it is we print scenario succeeded Sleepless for a little and then we're taking the screenshot and again very easy interface Just say screen cop minus P and where I want it and then I pull it Okay, just by saying what I want to pull and to where and Then I run it. So let's see how it works So here I have the script And we're going to see first I want To do something I just want first to uninstall the application I can do it with the PM PM is another tool adb gives us when I I can do PM least packages for example and Then with be a wait a second. I can Okay, the only packages gives us all the applications that are older package names of the application that are installed I copy this and say PM uninstall For this application and it will be set with the same success if I try to do it again It throws me an exception that unknown package. Okay, we can see it here so Let's just run the scenario and See how it's working So it says installing The device installing the application open the application now it asked me to allow pictures I don't know but here it sends I want the locat and they check in the locat if it's exist and it is and it says scenario succeeded and then we're taking a screen capture of the of the device and Pull it and now we should be able to see Two files in the temp directory One of them is the logging file and the second is the screenshot demo Okay, so here's the logging file is just the locat of everything and it just looked in there for it and Here is the screenshot demo and we can see that we got this this message and This is actually how you Use how you simply use ADB and how you can wrap it for automation and Appium did it as well with the Appium ADB project, which is written in a node I recommend you taking a look at it. They have many implementations there and Actually, that's about about ADB To sum up I want you to do I want to to say just try not to kill Try not to take a cannon in order to kill a mosquito Because many times we take big tools that are not necessarily the correct tools for for us and we just should think If there's a smaller tool that can perform our task and maybe will fit us more as Here the automation the low-level automation tool that I do or I created. Thank you very much