For people working in Automation Testing, you might have noticed that as you add more lines to your code the script maintenance becomes difficult.
A better approach to script maintenance is to create a separate class file which would find web elements, fill them or verify them. This class can be reused in all the scripts using that element. In future, if there is a change in the web element, we need to make the change in just 1 class file and not 10 different scripts.
This approach is called Page Object Model(POM). It helps make the code more readable, maintainable and reusable.
HOW TO IMPLEMENT POM:
In this post, I will be setting up a basic framework and will automate the login process of FLIPKART.
So as shown above, I have set up different packages that we would use for our robust and stable framework. Also, as seen in red block that `Pages` package will contain all the pages that are there in the website which needs to be automated and similarly ‘tests’ package will have corresponding test cases which needs to be automated in that page. So basically they have same name for quick and easy reference.
The above screenshot shows login_page file where I have put down the elements on which we will be working on to Login to flipkart. I have also put the action on each of the element like click, send_keys etc. So as you see here, we are just finding the elements and not performing any test hence if any change occurs in the elements afterwards you just need to change it in this file only and your tests will remain the same.
Now the above is our login test file. Here as you can see we have written a method to open Flipkart on firefox and then we are entering our username and password to login.
HOW TO RUN THE TEST:
You can run the test in Pycharm Terminal by using command (if you use the same layout of folder as I did):
py.test -s -v tests/home/login_tests.py
As you can see below the line in green shows that our test cases passed and we are able to login to Flipkart successfully.
CONCLUSION:
This is just the basic of POM and we can add and do a lot more stuff on it to make our code look more cleaner and readable. But as shown above as well, this approach looks much better than having all the elements as well as the test cases in the same class file.