Search code examples
pythonandroidseleniumpython-behavetestrail

How can I use BDD for writing test cases in testrail if I used Selenium with Python for my scripts?


I am trying to learn BDD (Behave) for a Android mobile app automation done in PyCharm using Selenium with Python (so is not pure Python; and I never understood the difference, even though I tried to find info about that). I'm a beginner, so I need to create Test Cases in TestRail using my Selenium with Python scripts (this is my goal). I've heard of Cucumber with Gherkin but I am so confused. Do I need to learn pure Python? Can I write test cases in PyCharm? Can anyone help me with some advice? From where to start? Thanks in advance.

PS: I have some links with docs but I am still confused: https://behave.readthedocs.io/


Solution

  • I would try to make something like this Page-Object Pattern test for webdriver:
    for pages:

    class LoginPage(BasePage):
    
    def __init__(self, context):
        BasePage.__init__(self,
                          context.browser,
                          )
        self.context = context
        self.base_url = URL.TWITTER
        self.visit(url=self.base_url)
    
    locator_dictionary = LoginLocators.__dict__
    
    def login(self, username='', password=''):
        self.username.send_keys(username)
        self.password.send_keys(password)
        self.submit_btn.click()
        return TwitterTimeline(self.context)
    

    and for base_page:

    class BasePage(object):
    """
    Really the only method used here is '__getattr__' now
    """
    
    def __init__(self, browser, base_url='', **kwargs):
        self.browser = browser
        self.base_url = base_url
        self.timeout = 10
    
    @contextlib.contextmanager
    def wait_for_page_load(self, timeout=10):
        old_page = self.find_element_by_tage_name('html')
        yield
        WebDriverWait(self, timeout).until(staleness_of(old_page))
    
    def find_element(self, *loc):
        return self.browser.find_element(*loc)
    
    def find_elements(self, *loc):
        return self.browser.find_elements(*loc)
    
    def visit(self, url='', route=''):
        if not url:
            url = self.base_url
        self.browser.get(url + route)
    
    def hover(self, element):
        ActionChains(self.browser).move_to_element(element).perform()
        # I don't like this but hover is sensitive and needs some sleep time
        time.sleep(5)
    
    def __getattr__(self, what):
        try:
            if what in self.locator_dictionary.keys():
                try:
                    element = WebDriverWait(self.browser, self.timeout).until(
                        EC.presence_of_element_located(self.locator_dictionary[what])
                    )
                except(TimeoutException, StaleElementReferenceException):
                    traceback.print_exc()
    
                try:
                    element = WebDriverWait(self.browser, self.timeout).until(
                        EC.visibility_of_element_located(self.locator_dictionary[what])
                    )
                except(TimeoutException, StaleElementReferenceException):
                    traceback.print_exc()
                # I could have returned element, however because of lazy loading, I am seeking the element before return
                return self.find_element(*self.locator_dictionary[what])
        except AttributeError:
            super(BasePage, self).__getattribute__("method_missing")(what)
    
    def method_missing(self, what):
        print "No %s here!" % what
    

    Whole article is here:
    http://blerdeyeview.com/testing-with-behave-framework/
    And here's another one:
    https://www.testrisk.com/2015/03/page-object-patter-for-selenium-test.html

    I'm beginner too and it really helped me. I'm testing website, not Android, but it should suit You just for the idea.
    Works great for writing steps for Gherkin
    Good luck!

    P.S. I'm using locator dictionary on every subpage class, because it works good. Unfortunately there's no autocomplete with that method, but i believe You don't need dictionary for Android app. It should work like it is writen in those links.
    Edited for more code input