Finding the center point of an element in Selenium

Sometimes you have to click or tap on a specific position of an element or an element’s container. This article will describe the way to find the center point of an element using Selenium.

Problem

Below is an image that contains 5 smaller images of stars. The image’s container can be found by locator type.
You need to click/tap on the star for rating 1-5.

Solution

To solve this problem, let see a smaller problem that finding the center point of an star element.

Center point (f) of a star element

From above image, there are some things that need to make clear:

  1. Coordinate:
    Values on Ox axis are incremented from-left-to-right.
    Values on Oy axis are incremented from-top-to-bottom.
  2. Star element is a rectangle (or square) with:
    Height and Width of the element are h & w corresponding.
    p(x, y) is the point at the TOP-LEFT position.
    f(x, y) is the center point of star element that need to find x-value and y-value.

If you know the parameter h & w of element and the TOP-LEFT point of element, then you can find the point we need.

Fortunately, Selenium provides the APIs to help us in this case.

Point p = element.getLocation();
Dimension d = element.getSize();
d.x = h;
d.y = w;

Now the point f(x, y) could be found and clicked as follows:

f.x = p.x + di.width / 2
f.y = p.y + di.height / 2
new TouchActions(driver).down(f.x, f.y).perform();

Now, turn to the initial problem that is more complicated with 5 stars.

We need to find the coordinates of f1, f2, f3, f4, f5.
We still have the point p and the width – W and height – H of stars container.

Since all star elements are similar, then we have

h = H;
w = W / 5;

Thus, now we have point p and the width & height of each star. From those, all center points of stars can be found as:

// Point f1
f1.x = p.x + w / 2
f2.y = p.y + h / 2

// Point f2
f2.x = p.x + w / 2 + w
f2.y = p.y + h / 2

// Point f3
f3.x = p.x + w / 2 + w * 2
f3.y = p.y + h / 2

// Point f4
f4.x = p.x + w / 2 + w * 3
f4.y = p.y + h / 2

// Point f5
f5.x = p.x + w / 2 + w * 4
f5.y = p.y + h / 2

Finally, the problem is solved and you can touch to the all stars.

The problem is solved based on location and size of element that is supported by 2 Selenium APIs:

element.getLocation();
element.getSize();

Besides, Selenium also provide an API that contain all above properties.

Rectangle rect = element.getRect();
Point p = rect.getPoint();
Dimension d = rect.getDimension();

Or

Rectangle rect = element.getRect();
int px = rect.getX();
int py = rect.getY();
int h = rect.getHeight();
int w = rect.getWidth();

Summary

The article presents the basic example of using Point and Dimension for finding the center position of an element. There are several methods of Selenium to support getting necessary properties of element.

Reference

Selenium javadoc: https://selenium.dev/selenium/docs/api/java/

What is API Test? Are there any special testing techniques applied for?

You might know API test for web service (REST, SOAP), but it’s just a type of API test. This article will tell you more information about API, types of API and API test.

What is API?

API stands for Application Programming Interface. API provides the interface or protocol to communicate between 2 modules or layers. It is a collection of methods for developing applications.

An API could be considered as a black box for developers using it. Thus, developers do not care about what occurs inside an API. They just care about what they should provide for API and what API response the result.

The information for an API is input parameters, and the response is output.

API could be applied for operating system, database, software library, hardware functions, and web applications.

There are some typical types of API familiar for testers, like:

  • Web service
  • Java API (library/dependency)

What is API test? Are there any special testing techniques applied?

An API is like a black box, so API test is to verify the output which is returned after passing input parameters to API. Tester plays the role as developer to use it and even more verify its working.

Testing techniques for API test are generally not much different compared with functional test. We still using basic techniques, such as:

  • Boundary values.
  • Equivalent partition.
  • Decision tables.
  • State transitions.

The special thing in API test is how to use and implement those techniques. Because API test is not GUI test, we should use properly tools and sometimes tester must writing code or script for testing.

An API is itself a completed unit of software applications, so API test could be a part of Unit testing. However, API test is only executed after the build or deployment ready.

For agile development, API test takes effects faster and more stable than GUI functional test. It is usually executed automatiically right after a build version ready. However, the costs and speed for implement the test cases (using tools or writing code) are concerned that need to be balance with application release plan.

Summary, API is the term of not only web service, software application but also hardware functions. API testing techniques are not separated with usual functional testing techniques, because one of the main purpose of API test is for checking function work well. The others approaches of API test could be to measure performance, security of API.

Reference:

https://www.vectorcast.com/testing-solutions/api-testing

First simple Java program

Previous article introduced fundamental information about Java programming language. Now let’s start create a simple application.

Prerequisite

Before starting this section, please ensure you have some software installed on your machine:

  • JRE or JDK
  • Setup JAVA_HOME for System environment variable. Please follow the instruction here.
  • Text editor (like Notepad++, Emacs, Vim) or IDE (like Eclipse, IntelliJ Idea)
    (for this site, I will use IntelliJ Idea Community Edition for creating program)

Let’s start

Create a java project

  • Launch IntelliJ Idea Community Edition > Create a new project
  • Select Java
  • Set project SDK: browse to JDK directory if the text is not loaded successfully.
  • Next
  • Next
  • Set project name and location

It’s done for creating project. IntelliJ Idea will show the project like below.

Now, It’s time for writing code. We need:

Create a package

  • Right-click on src > New > Package
  • Type package name. The name should be in lower case.

Create a java class and write some code

  • Right-click on package that you’ve just created.
  • Enter the name of class

Run the program

Finally, it’s time to write simple lines of code. Just copy and paste below code to your created class.

package firstProgram;

public class FirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello tandt53. This is the first java program created by you.");
    }
}

Let’s run this code by right-click on the code and select Run ‘FirstProgram.main()’ and check the result.

Conclusion

Phew!!!! It was a lot of works, right? Now, take 5 mins to review what you’ve done:

  • Created a brand new java project
  • Add a package and a java class inside
  • Write some code and execute it

Those are common steps that you need to do when you’re going to write a java program. However, there are some question inside you mind now.

  • Why do you need to follow all of them?
  • What are package and class in java?
  • What was exactly you just created in java class?

I will go details in the next post to help you clear all above questions. Now take you time to rest. đŸ™‚

Java Introduction and Installation

Applications

Java is a well-known programming language. There are so many applications created by using it.
– Web application
– Mobile application
– Desktop application
– Games
– Database connection
– Much more

Properties

  • Java is an independent platform language. It means that the application created by using Java can be run on any platform (Windows, Mac, Linux) thanks to Java Virtual Machine (JVM).
  • Object-Oriented Programming language. It is a based paradigm that Java supports. Every element in Java are objects, except primitive data types.
  • Interpreted and compiled code. The source code is written in .java files, then they are compiled into bytecode format in .class files. At run time, JVM reads the bytecode instructions and interpret to the native hardware platform which JVM was written.
  • Self memory management: Java allocates and de-allocate objects in memory automatically. JVM will delete the objects which do not have active pointer exists, it’s called Garbage Collector.

Installation

To run Java programs, you must have either Java Runtime Environment (JRE) or Java Development Kit (JDK). JDK contains everything JRE has and much more tools such as the compilers and debuggers necessary for developing applets and applications. Thus, it is recommended that you should install JDK for playing more with Java.

enter image description here

For JDK, there are some options that you can choose to develop various types of applications, such as Java Platform, Standard Edition, Enterprise Edition.

For the next articles, I would suggest to use basically JDK Standard Edition (SE) version 8, even the latest version is 12.

You can find the installation files for your OS in here.

If you got any trouble in installation, please leave your comment below. I would be happy to help you.

Next article, we will start creating a simple java application and more details about parts of a program.