PortadaAutomatingAppSelTest01

Nowadays Automation has grown so much, that even working in an environment of just ‘Manual Testing‘, it’s really important to ensure and improve the testing process to include a percentage of ‘Automation‘.

It’s important to mention that there’s impossible to cover 100 % of Automated Test Cases because automation has its limits like the abilities and knowledge about coding in a language programming of the Testers, the most used are Java, Python, and JavaScript, which not all actual Testers have.

Another important key is that not all the types of testing are able to automate like look and feel test.

So to keep it simple, if you want to automate a test you have to take the following points at least to start automating:

1. There are Regression Test Cases that you need to run always before a release.

2. There are flows that are just too large but also repetitive.

3. The scope of the Company is implement Continuous Integration (CI).

There are a lot of automation tools for different platforms like Android, IOS, and Web, but also for different scopes like Selenium, Mocha, Jazmine and Espresso.

Let’s start with Mobile (Android & IOS) using Selenium and TestNG with Java.

Create a Maven Project in Eclipse

For the next example, we’ll be using Eclipse as IDE and Maven of libraries management.

1. Create a Maven Project, it’ll look something like this.

2. In Folder “src/main/java“, create the packages “example.enviroment“, “example.pages” “example.test.base” and “example.pages.base“. This packages will contain our code.

3. Now let’s create the class “Setup” for the package “example.environment“, the class “BasePage” for the package “example.pages.base” and the class “BaseTest” for the package “example.test.base”.

4. It’s necessary to add de dependencies to the “pom.xml” file.

5. In Setup class we create de AppiumDriver that will interact with the device and our test.

To Appium to been able to connect your device is necessary to define the next capabilities:
  1. udid = The ID of the device.
  2. device_Name = Generic name of the device usually just “My Phone”.
  3. appPackage = Is the name of the application’s package.
  4. appActivity = Is the name of the main activity (First Page) that the app loads when the app it’s launch.
  5. noReset = It’s an extra capability to avoid the information of the app got wiped out each time we run a test.
For the initialization of the driver, the URL passed to the driver it composes of the address where Appium is running (our machine the localhost) and the port Appium is running.

6. Let’s use ADB to obtain the previous capabilities.

By executing “adb devices” we obtain the UDID of the devices and emulator connected to our computer, in this case, is just one device “7MLNW18B29001109”.
To be able to see the device we must enable de “Developer Mode” and the “USB DEBUGGER”.
For the “appPackage” and “appActivity“, let’s type “adb shell” and then “dumpsys window windows | grep -E ‘mCurrentFocus‘”. It’s important to mention that the app to automate needs to be open on the device when we type the last command.
com.huawei.android.FMRadio“: is the “appPackage” of the app.
com.huawei.android.FMRadio.FMRadioMainActivity“: is the “appActivity” of the app.

Create our First Test using TestNg

1. In class “BaseTest” we are going to call our driver (Setup class) each time a test is executed that’s why it is going to be called in BaseTest is going to be base for all the tests. With the tag @BeforeMethod, we assure that the Setup class and its method are always called before our tests. Also with the tag @AfterMethod we are going to set the close of the objects open, each time a test finishes. The tag @Parameters is going to help us to send the values from the xml file of TestNG.

2. Now let’s run Appium to inspect the elements of our app.

The host is our computer it can be assigned as 0.0.0.0 or 127.0.0.1 is the local host.
The port can be changed if it’s occupied or we want to run tests in parallel (I recommend separate the ports at least 10 units to avoid conflicts).
Now we have Appium Service running, we can use this console to debbug or use the Eclipse console.

3. Let’s click “Start inspection Session“.

4. Insert the next capabilities, click the button “Start Session“, and then we’ll see the app main activity, in this example the “Radio app“.

5. This main activity it’s going to be the “Principal Page“.

We are going to use “Page object Model” (POM) and “Page Factory” for the implementation of the test.
POM is a framework to organize the elements of an application in a way that it’s easier to understand and to maintain via “Pages”, in each page (Class) it’s defined the elements of only that page, in this example, we are only going to have two pages, the “Principal Page” and the “Radio Channels Page“.

6. Let’s inspect and get the locators of the elements of “Principal Page“.

The most common locators are “ID” and “XPath“.
The ID locator is a unique locator that avoids having issues like duplicate elements if it is available de ID locator is much better to use it always.
The XPath locator depends on the DOM of the app source, so it’s not recommended to use it because it can change and also affects the performance because it has to go through all the DOM to find an element.
There are other locators like “name“, “class” and “text“, this locator it’s better to use them as a complement of each other because it’s possible that two or more elements have the same name, class, and text.
In this example:
The previous buttons ID is: “com.huawei.android.FMRadio:id/iv_prev“.
The next button ID is: “
com.huawei.android.FMRadio:id/iv_next“.
The Power button ID is: “com.huawei.android.FMRadio:id/btnPower”.
The stations button menu ID is: “com.huawei.android.FMRadio:id/action_stations“.

7. Now let’s create our methods in the class “PrincipalPage“, create a package called “example.pages.java” and the class “PrincipalPage“.

The first part of the PrincipalPage consists of the following:
  • This class extends BasePage that will have the methods used by all the pages.
  • In the Class’s Constructor it’s defined the “driver” that we create in Setup.
  • PageFactory.initElements: Initialize the elements of the page, this is the main function of Page Factory, at the moment it’s instantiated the class PrincipalPage all the elements of the page are initialized.
  • The structure to create these page elements are by the tag @FindBy and the type of locator to use like id, XPath, class, name etcetera; and the value of the locator.
    Finally, it only needs the name of the element in our case a WebElement.
The last part is the definition of the methods aka actions of the elements.
In these examples the actions of each element are just clicks, this “Click” method/action is defined by Selenium as other many actions like sendKeys, clear, getText, getLocation, and many others.
The failedMethod and staticWait are not declared on this page because it comes from BasePage.

8. Let’s define the actions in BasePage.

In this class is defined the methods “failedMethod” that prints the exceptions encountered on each method of the pages, and the method “staticWait” it converts from miliseconds to seconds, is a static wait to see the execution of the test, without it it’s not possible to see the actions.

9. Let’s create the last package where our Test Suites will be called as package “example.test.java” and class “FirstTest“.

10. The main test script resides in FirstTest.

This is the script of the test, where are just called the methods to make the actions.

11. Finally, let’s create the “PossitiveTests.xml” that TestNG will use to execute the tests and also is where the input parameters are defined.

The parameters are the input data of the test in Setup.
The class name is composed: package_name.class_name.
The methods include name is composed : method_name_of_FirstTest_class

12. Lets just run it!

Right click on “PossitiveTests.xml“, select “Run As” and then “TestNG Suite”.
If there’s a problem finding this option be sure to have installed TestNG on the IDE, for more information about the installation of TestNG refers to https://testng.org/doc/download.html.
These are the result displayed in TestNG.

Links of interest:

Speed up Android Testing with TestProject Agent

Upload a File With HTML5
The Agile Team Approach
What Scrum Master Certification to Choose?

These are some recommended books to learn more:

Any help is welcome to help us keeping this effort alive! PayPal Account BTC (Bech32): bc1qx6f8nczr5ram6d57svlnsfmk5mhu6lsr9q7mxw BTC: 1DDcWbphm1bKMvWruotNKLSM8ypVaHg5Nv ETH: 0x58D137fb142D946bCD815f0ded0fa3b3fE5AB3BF

Twitter
Visit Us
Follow Me
YouTube
YouTube
LinkedIn
Share
Follow by Email
RSS