izotop
izotop

Reputation: 51

Convetion of test functions in Selenium with Python

I want to test an internal web application and I want the code (Selenium & Python) to be well-organized and clean. I have a question of what is the convetion of writing test functions, for example:

def test_access accesing the web portal and checking if the page loads def test_login acessing the web portal and checking if the page loads and then if login succeeds.

In the above example I have 2 functions and the test_login function is test_access function plus checking if login succeeds.

  1. In that case is it correct in test code to do something like this:
def test_access:
     # do sth 

def test login:
    test_access()
    #check if login succeeds
  1. Or should be every test function be written without invoking the previous test function? Or is it correct to repeat the code within test functions?

  2. What if the test funtion that I want to implement consists of accessing the web portal, logging in, then checking every attribute of the page after the login. In that case should be it one function doing all this actions or should it be chunked into more but smaller functions?

Upvotes: 2

Views: 97

Answers (2)

Christophe Brun
Christophe Brun

Reputation: 71

To have a clear view of what you are testing in a def test_* you must only have in this def test_* the code you want to test. The sequences you repeat such as des test_access must be fixtures. The Pytest test framework https://docs.pytest.org/en/latest/ has all the required features for that purpose. It integrates perfectly Selenium. Those fixtures are then passed as arguments of the def test_* and are not anymore in the def Any sequence ran multiple times are in fixtures. You can use pytest.mark.order(<order of fixture>) decorator to schedule them. Add an assert in fixture if there is something in you want to test. Because a test can then be empty and just a sequence of fixture. A example could be:

import pytest

@pytest.fixture()
def access():
     # do sth 

def test_access(access):
    pass

def test_login(access):
    #check if login succeeds

This is just a piece of advice based on my experience in web an mobile QA. It is hard to explain in a few lines. It might not be obvious in this simple example but Pytest is for sure the state of the art of software QA (in Python), it is really worth reading the documentation and learning it extensively.

Upvotes: 0

PDHide
PDHide

Reputation: 19989

https://www.selenium.dev/documentation/en/guidelines_and_recommendations/page_object_models/

Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT. The tests then use the methods of this page object class whenever they need to interact with the UI of that page. The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change. Subsequently all changes to support that new UI are located in one place.

The Page Object Design Pattern provides the following advantages:

There is a clean separation between test code and page specific code such as locators (or their use if you’re using a UI Map) and

layout. There is a single repository for the services or operations offered by the page rather than having these services scattered throughout the tests.

Right way to design testing is to use page objects

define a class for each page and add actions and elements specific to that page inside this class.

And call the object of these class inside tests

login.setpassword("something")
login.setusername("somethign")
login.submit()

Upvotes: 1

Related Questions