Automated UI testing is basically using specific tools and frameworks to automate manual UI testing and verify functionality. It has been a great help in increasing the quality of validation process during development period. Automated process will run without human effort and will save time for the repeated scenarios for the testing period of applications.
Selenium is a well-known UI automation framework that is used by test engineers. Selenium and Cucumber frameworks are used together frequently for test automation. And here we used Behave which is a Python library of Cucumber framework.
Cucumber tool is naturally a BDD(Behaviour Driven Development) framework which is used to write acceptance test for the web applications. It allows us to automate functional validation in easily readable and understandable format since it uses plain English.
An example of Cucumber scenario and feature file:
Why Page Object Model?
We used POM(Page Object Model) for our project design pattern instead of BDD. This method is popularly used for test automation with Selenium projects but there is not much example of usage Selenium, Cucumber, and POM design pattern all together especially for Python projects.
The advantage of this model is that it reduces code duplications and improves maintenance substantially. Under this model, we should have a Page Class corresponding to each web page in the application. Each Page class will contain page methods and operations on WebElements that belong to the web page. For example, login is a frequently used action and we can implement this action under Login Page Class.
Most of the POM examples include WebElements of that web page within the corresponding Page Class but it is better to keep WebElements location on a separate Locators Page class. In this way, it will be easier to control and maintain element locations and we will have a more organized view for the project.
The project directory of our project looks like this:
We should have environment.py in the same directory with steps folder so that behave will know where to look for step definitions. Beside that, for example, we can use environment.py to call driver for each scenario or feature as we wish
and close driver after.
Accessing Tag Information In Python
Cucumber framework uses tags to specify features or scenarios and tags attached to a feature and scenario are available in the environment functions by “feature” or “scenario” objects passed to them.
There are environmental controls specific to tags and we can invoke an environment.py function by using tags before_scenario and after_scenario to call functions before and after each scenario like launch_browser and quit_browser functions.
Eventually, using Page Object Model is the best practice for UI automation projects. It is easier and faster to implement and improve. The structure we are using helps us use every advantage of selenium and cucumber frameworks during implementation. Furthermore, this structure gives you the freedom of working together without any conflict during implementation.