jeudi 8 août 2019

Need advice on Test Class structure (Python, Selenium)

I am currently working on QA testing system, and I am using Python and Selenium. The project structure I currently have is this:

locators.py - contains all locators on the page

EXAMPLE:

  class LoginPageLocators(object):
      """ Login Page Locators """

      USERNAME = (By.ID, 'username') #username text-box
      ....

basePage.py - base page class

EXAMPLE:

class Page(object):

    def __init__(self, driver, base_url):

        self.base_url = base_url
        self.driver = driver
        self.timeout = 30

    def find_element(self, *locator):
        """
        This method finds an element on the page with the appropriate locator

        :param locator:
        :return: element on page:
        """

        return self.driver.find_element(*locator)

pages.py - Here I have all my page classes, and here is a small example:

EXAMPLE:

class LoginPage(Page): """" Login Page Class"""

def __init__(self, driver):
    self.locator = LoginPageLocators
    super().__init__(driver)

def enter_username(self, user):
    """
    This function enters the username of the user about to log in 

    :param user:
    :return: fills the username textbox:
    """

    self.find_element(*self.locator.USERNAME).send_keys(users.users.get_user(user)["username"])

There is also a users.py file, but there is no need to show this one. I hope you can kind of get the structure I implemented in my code.

Now for my actual task/ testing. I need to implement a simple scenario:

  • User 1 logs in unsuccessfuly
  • User 2 logs in as admin and gives User 1 login rights
  • User 1 logs in successfully
  • User 2 logs in again and deletes User 1, so test can be repeated, and logs out

This is my current implementation, which works pretty great, but I am unhappy about it for some reason. Here is the code:

       """" This module contains the actual tests """

      # skipping imports ...


      class PageTests(unittest.TestCase):

          # efficient set up
          @classmethod
          def setUpClass(inst):

              inst.driver = webdriver.Chrome()
              inst.driver.get("http://localhost:9696")

           def test_scenario_01(self):

               # Setting up page objects
               login_page = LoginPage(self.driver)
               main_menu = MainMenu(self.driver)
               permissions_manager = PermissionsManager(self.driver)

               # Assert login of user is unsuccessful

               login_page.login("selen003")
               self.assertIn("http://localhost:9696/login?reason=incorrect", login_page.get_url(), "User already exists")

              # Admin log in and assign permission to User
               login_page.login("selen001")
               self.assertIn("http://localhost:9696/profile/", login_page.get_url(), "Unsuccessful Admin login")

               main_menu.click_pm()
               permissions_manager.add_user("selen003")

               time.sleep(3)

               permissions_manager.logout()

               # Attempt login of User again
               login_page.login("selen003")
               main_menu.logout()

               # Delete user, so the test can be re-done
               permissions_manager.delete_user("selen001", "selen003")

          @classmethod
          def tearDownClass(inst):
              inst.driver.quit()


      if __name__ == "__main__":
          unittest.main()

The code runs perfectly well for this scenario and succeeds in testing. However I am feeling unhappy for a couple of reasons and I need advice:

  • The test scenario is in a single test method, and I can't really put multiple asserts for each individual action (well I can, but I have heard it is really unpractical), but I really want to test each individual action and raise the appropriate errors (For example: when the user logs in, a message will be given in the html report "STEP 2: user XXX successfull login"

  • Perhaps, I should split the scenario in multiple functions (test_valid_login(), test_invalid_login(), etc...) and implement a
    TestSuite, which imports the individual tests and runs them. However I am not sure if this an efficient solution in this case

  • I wonder if there is a more efficient way of doing it (and allowing for easier future implementations of scenarios)

I just need advice on how should I approach my test structure, because I am unsure if this is a good solution (and something tells me it isn't) and I would appreciate it if you can give me some tips/ ideas.

Aucun commentaire:

Enregistrer un commentaire