Automation Enthusiast
Automation Enthusiast

Reputation: 21

advice on building selenium/python framework?

    import sys
    import os
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
    from selenium import web driver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.support.ui import WebDriverWait

    class WebDriver():

      def setup(self):
         self.driver = WebDriver()
         self.driver = webdriver.Firefox()
         self.base_url = "www.google.com"
         self.driver.get(self.base_url)
         self.driver.delete_all_cookies()
         self.driver.implicitly_wait(30)
         self.verificationErrors = []
         self.accept_next_alert = True
         self.driver.maximize_window()

      def teardown(self):
         self.driver.quit()

I'd like to use this as my base file in my automation framework but it does not seem to work. Please help!

Also, aware of the indentations, not the issues running into here. I want to be able to import this and have it run set up and tear down for each script I add to the framework.

Any help with understanding how to build the framework would be so appreciated! Thanks

Upvotes: 2

Views: 3589

Answers (1)

willnx
willnx

Reputation: 1283

What testing framework are you thinking of using? That will completely change the syntax you use to have this logic ran before/after a test (or the whole test suite).

Additional considerations:

What type of tests are you making?

  • "unit test"? (quotas b/c you can't* have unittests in python for a webpage)
  • integration tests
  • stress/performance tests?

If you're thinking of Selenium as a way to unit test your web app's UI, you might want to checkout some JavaScript testing frameworks. Definitely checkout a JavaScript framework if you're using any JavaScript in your UI. Using JavaScript to manipulate the DOM, and trying to use Selenium to also manipulate the DOM is one giant race condition over the DOM.

What are you planning to test by using Selenium?

  • Sanity checking that the buttons your UI are hooked up correctly?
  • Business rules for your webapp?

I would strongly encourage you to use Selenium to validate the happy path (i.e. can I click this button) in your web app, and to not test your business rules; hammer the API to exercise those business rules. The API is way less likely to change than the UI, and changes to the UI regularly cause false positives in your Selenium tests (test breaks that generate a failure, not a real failure in the app).

Please don't be discouraged by any of this! It's awesome that you're writing tests!

Selenium is a good tool when used correctly, it's just easy to overload it and end up with inconsistent tests (depending on the amount of JS, and the JS framework).

Pointer about your code specifically:

make the class something you can instantiate and bind as needed - makes using the code across frameworks easier, and debugging easier b/c you can just open the python interpreter and use it.

# file named my_webdriver.py

import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait

class MyDriver():

  def __init__(self):
     self.driver = webdriver.Firefox()
     self.base_url = "www.google.com"
     self.driver.implicitly_wait(30)
     #self.verificationErrors = []  # delete this line, and deal with errors in the framework
     self.accept_next_alert = True
     self.driver.maximize_window()
     self.reset()

  def reset(self):
      """So I can be lazy and reset to a know starting point before each test case"""
      self.driver.get(self.base_url)
      self.driver.delete_all_cookies()

  def teardown(self):
     self.driver.quit()

Using it:

from my_webdriver import MyDriver

driver = MyDriver()
driver.get('http://my-awesome-app.org')
element = driver.find_element_by_id('some-id')
element.click()

Binding it within the unittest framework:

import unittest

from my_webdriver import MyDriver

class AwesomeTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        """Runs once per suite"""
        cls.driver = MyDriver()

    def setUp(self):
        """Runs before each test case"""
        self.driver.reset()

    @classmethod
    def tearDownClass(cls):
        cls.driver.teardown()

    def test_stuff(self):
        """a test case!"""
        # stuff
        pass

Good luck! hope this is helpful/ useful.

*I saw some stuff at a PyCon about using Python to manipulate the DOM, but I don't think anyone is doing that in production.

Upvotes: 1

Related Questions