Thursday, 23 February 2017

Appium : How to perform Click() and Fill the EditText Boxes


Hello Guys,

In my last blogs, we discussed about appium setup and launching of an app and finding web elements using appium so that we can perform any operation using those web elements. Say we want to click any button like we did in our Calculator example where we performed 7 + 5 = 12 operation successfully.

So far we have only seen to perform click() operations. It’s pretty simple, just find the element like Buttons, checkbox, radio buttons etc., call the click() method with that web element.
For e.g.,
WebElement loginbtn = driver.findElementById("com.tinder:id/button_login_real"); 
loginbtn.click(); 
 
 It’s really very simple....isn’t it ?? Now, what if we want to enter some text say login id, password or 
some comments ? Well, for that we need to call “sendKeys()” method.  We pass the text which we 
want to enter to the respective web elements. It’s a good practice though, to save your contents in a 
variable and pass the variable to this method. In the following example we will see how to pass login ID 
and Password to an app. 

For e.g.,
public final String EMAIL = "myname@gmail.com", PASS = "MyPassword";
driver.findElement(By.xpath("//android.widget.EditText[@index='0']")).sendKeys(EMAIL);
driver.findElement(By.xpath("//android.widget.EditText[@index='1']")).sendKeys(PASS);

And once we have entered the credentials, we can click the login button. So, now we can easily signup, 
login to any app which require thses stuffs. Isn’t it Cool ?? Okay, So the concept says, Get that element 
which you want to perform action upon and just perform the action. This Click() method can be applied 
not only with Buttons, but Radio Button, CheckBoxes, or even Toggle Buttons. All it requires is the 
finding that element. That’s it !!  If you are confused with any of the things mentioned here in this blog. 
I recommend you to go through my previous blogs on appium where I have mentioned these things in 
details.






Thursday, 16 February 2017

Appium - Finding WebElements



Hello Guys,
In my last blog, I discussed about the launching of any of the pre-installed App through Appium. I hope you didn’t face any difficulty in that. In this blog, I will take you a step closer towards Appium. Okay ! So let’s try to understand how appium actually works. In my recent blog, we had seen the launching of Calculator App automatically and we had performed some actions as well. What do you think, how did we able to press the keys ?? or clicked the “=” button for the result ? Well, the answer of this question lies in the title of this blog. Yes, you got it Right ! It’s the “Web Elements”. Ohh !! great, What’s the "web-element" by the way ? Well, WebElements are used for the identification of Web Based elements using WebDriver. By saying web-based elements, I mean the buttons, editText boxes, checkboxes, radio buttons etc. with which we interact often.

If you use “uiautomatorviewer” you can easily recognize your web elements. I have not used anything else for this purpose as of now. 

 
In the above screenshot you can see that the numeric 9 is selected and on the right pannel every details regarding that key is been reflected such as index = 2, text = 9, etc. But unfortunetely, life is not that easy everytime. In some situations, you might not be able to get these data. For e.g., like in the above screenshot we couldn’t get the “content-desc” details. Similarly we might not get the details related to the feilds like...index, text, resource-id, etc. What to do in such situations then ? Well, we can use the combinations. Like (class_name + index), (class_name + text), (class_name + content-desc) or (class_name + bounds).

We can locate web elements in may ways, one of the way is by using “xpath”. I personally use xpath more like anything in case of lacking of the resource-id’s. If you are getting the resource-id, there is no need to do extra things. Simply use findElements(By.id(“”)).
for e.g.,
WebElement back_button;
back_button.findElement(By.id("resource-id of the back Button"));
In case, you are not able to find the resource-id but the class name and index. You can still locate the element by using “xpath[@index]

driver.findElement(By.xpath("element’s Class Name[@index='index value']"));
for e.g.,
WebElement search_editText;
search_editText = driver.findElement(By.xpath("//android.widget.EditText[@index='1']"));

In case, you want to access your elements using Bounds,
WebElement back_button = driver.findElement(By.xpath ("//className[@bounds='[10,58][110,126]']"));
[ use your element’s bound value. ]

I personally don’t recommend to locate elements via bound value, as it might vary upon the size of mobiles.




Thursday, 9 February 2017

Appium - launching an App

Hello Guys,

Hope you are doing well !. Today we will learn to launch an installed Android Application like calculator, phone book, etc. I believe you have done your appium setup already. Let's directly jump to code which will launch Calculator application on our mobile.  

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class OpenAppTest {
    AndroidDriver driver;
    @Before
    public void setUp() throws MalformedURLException {
        
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();        
// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "Intex Aqua_Ring");        
// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");        
// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "6.0.1");        
// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability("platformName", "Android");        
// Set android appPackage desired capability. It is        
// com.android.calculator2 for calculator application.        
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.calculator2");        
// Set android appActivity desired capability. It is        
// com.android.calculator2.Calculator for calculator application.        
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");        
// Created object of AndroidDriver will all set capabilities.        
// Set appium server address and port number in URL string.        
// It will launch calculator app in android device.
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
    @Test
    public void openCalculator() {
        

        driver.findElement(By.id("com.android.calculator2:id/digit_9")).click();
        driver.findElement(By.id("com.android.calculator2:id/op_add")).click();
        driver.findElement(By.id("com.android.calculator2:id/digit_7")).click();
        driver.findElement(By.id("com.android.calculator2:id/eq")).click();
      
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        }
@After
public void End() 
       {
          driver.quit();
             }
 } 
 
  


Copy and paste the above code with some relevant changes as per your device name and it's 
version number. Don't forget to include the appium and selenium JAR files (which you had 
downloaded in order to setup the appium) as a library to your IDE(In my case, it's Android Studio).
 In order to run this test case, you need to start your appium server as well. You can start appium 
server by just typing the “appium” at your terminal. I will not explain anything related to the code in 
this blog but in my upcoming blogs. I will share every details as per my knowledge. If you fail to
 launch the Calculator application on your mobile using this code. Please let me know about this 
by commenting below. 



 
 
 
 







 
 
 
 



 
 
 

 


Thursday, 2 February 2017

Appium Setup on Ubuntu


Appium Setup on Ubuntu 16.10

Hello Friends,

Today I am going to discuss about the installation and setup process of Appium on Ubuntu. Don’t worry much about the version of Ubuntu as It will remain same for older versions of Ubuntu as well (I hope So !!). I was personally using Android Studio but you can use other tools as well(like eclipse, IntelliJ IDEA editor etc.,). Before we start installing and setup of Appium, Let’s have look at the tools we need to download. Install JDK latest version and then,

Download -
  1. Android Studio (You can use any IDE)

Note: Try to get the latest versions of each of the tools. Otherwise, you may stuck somewhere either during the setup or may be after using it for few days.

Setup Appium

1. Install ruby: 
Paste the below command at terminal and hit enter
sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev 
libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
 
2.Install linux brew:
Paste the below command at terminal and hit enter 
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/
Homebrew/linuxbrew/go/install)"
 
3.set path for brew 
Type: gedit.bashrc at terminal and copy paste following into the .bashrc file 
export PATH="$HOME/.linuxbrew/bin:$PATH"
export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"
export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"
 
4.Install node:
Paste the below commands one by one at terminal and hit enter 
brew update
brew install node
brew link node
 
5.Install appium 
npm install -g appium
npm install wd 
To start appium: Paste the below command at terminal and hit enter 
appium


I hope it should work for all the versions of Ubuntu. I did it on Ubuntu(16.10). If you face any difficulty installing and setting up Appium, Leave a comment below. I would love to help you out.