Hello everyone and welcome back to the Appium Pro intro workshop. In this module we're going to talk about interacting with elements. We're going to put together everything we've learned about finding elements and couple that with some stuff we can actually do with the elements and we'll get some actual useful automation going at last.
Okay, let's talk about the four basic element actions that we encounter most frequently in Appium. By far the most frequent action would be click. Now click is a command that you run on a web element instance, so this is not a command that we call on a driver object. Rather, it's a command that we call on an element object that we've received as a result to the call to driver.findElement. And you might be confused, why is this called click and not tap? It's called click because this API was first introduced with Selenium for supporting web browser automation, and of course for web browser automation on desktop computers, you use a mouse and not your finger typically, so it's called click. But in the world of Appium it really just means tap.
Once we have an element that we found, we can also send keys to it. Now we can't send keys to just any kind of element. It has to be a certain kind, especially a kind that can take key input. And when we say keys, what we mean is keystrokes on a keyboard. So, when we want to input text into a box, we use the sendKeys method and we pass in the string that we want it to type as a parameter. We can also use send keys on other types of form controls, not just text fields. For example, if we have a slider that goes from left to right, we could imagine that the left edge is represented by the number zero and the right edge is represented by the number one. We can send in a string of anywhere between zero and one. So, 0.0, 0.5, 0.8, 1.0 and what Appium will do is it will move the position of that slider to the relative amount that we have defined on the range between zero and one.
Sometimes we find ourselves in a position where we have some text in a box and want to clear it so we can actually find an element that has some text in it, and we can call element.clear() and that will clear the text out of the field for us.
Lastly, it's very common that we want to figure out what text is being displayed in a given element. And this is especially useful when we're trying to perform verifications that our app is behaving the way it should. We can find an element and get the text from it so that we can make assertions about the state of the application in our test code.
So basically, that's it. We call element.getText() and that will give us back the string that is being displayed currently in the element that we've found. So that's really it. These are the four actions that are going to comprise most of your Appium automation. It's really not too complicated, but let's see how we can put all these together along with finding elements to run through a basic login flow.
So, if I just load up my emulator and open up the test application, here's what we're going to try and implement. Basically first, we want to navigate to the login screen. Then we're going to enter a correct username, in this case, "alice", and a correct password. In this case, my password, and tap the login button and then verify that it says something appropriate like "you are logged in as alice". So, this would constitute an actual test of the functionality of this application. Of course, we would need other tests that tried with incorrect usernames and passwords to prove that we didn't get the same results even with incorrect data, but there we have it. So, let's see how we can do this with Appium itself.
Here is our project, a bunch of our Java files. We're looking at the moment at AndroidLoginTest.java. So as always, it extends our base class and uses TheApp's Android capabilities. Then our test logic itself is starting to look a little bit meatier than in previous examples. The first line is something we haven't seen before and we're going to look at it in more detail in the next module. So, we won't go over it right now. But basically, this manage timeouts business is just to make sure that when we find elements we're actually waiting appropriately to find them, and not just trying to find them before they have shown up on the screen. But just bracket that for the moment in your mind because we'll talk about it soon. And here's the main flow of the test. We are calling driver.findElement four times and each time we're calling it using an accessibility ID locator.
So first we're finding the login screen element and clicking it, I should say tapping it in order to move to the login screen. Then we are finding the username field and calling sendKeys on it. And we're sending in the string, "alice". Likewise with the password field, sending in the string, "mypassword". And finally, we're finding the login button and calling click on it. Our last command is attempting to prove that we are in a correct logged in state. So, for this, we're using an X path locator. And what this X path query means here is start anywhere in the X path document. The star means look at any type of element. So not just one class of element, but any type. And find an element whose text attribute is equal to the string you are logged in as Alice. So, what will happen is if my test finds this element, it will succeed. And if it doesn't find this element, then an exception will be thrown from this driver.findElement method, and the test will fail.
So, let's give this a run and see if Appium can basically mimic what I did by hand just a little bit ago. So, this is called AndroidLoginTest. So, I'll run ./gradlew test --tests AndroidLoginTest. Before I do that, I always check that Appium is up and running. It is. Here's my Appium server and we knew that our emulator was already ready to go. So now I will just sit here and watch until the app is loaded and starts going on the emulator. There it goes. So now we're actually doing something with Appium. And you can see that the actual test itself after a startup was quite quick, faster than I could navigate through this manually certainly.
So that is basically how we interact with elements using Appium once we found them. And Appium does get more complex than that, but really that's 80 to 90% of what you do with Appium. As long as you have a very basic generic UI control based application, you can get away with using just these few commands and automate pretty much all of your user flows. So that's it for this module. Stay tuned until the next episode where we discuss waiting for elements appropriately.