barciewicz
barciewicz

Reputation: 3773

How do I test a Selenium automation without restarting the browser each time?

Suppose I have the following code:

from selenium import webdriver

if __name__ == '__main__':

    def open_YouTube():
        browser = webdriver.Firefox()
        browser.get("http://www.google.pl")
        fill_search(browser)

    def fill_search(browser):
        elements = browser.find_elements_by_tag_name('input')
        for element in elements:
            if 'what are you looking for' in element.get_attribute('innerHTML'):
                search_box = element
                search_box.get_attribute('value') == 'happiness'



    open_YouTube()

Now, supposing I made a mistake or want to change anything in the function fill_search, is there a way to test only that function, without running the code from the start and reopening the browser?

For example in my Internet Explorer automations via VBA I have a function that identifies currently open instance of the browser by name or URL. I can run this function and test only the code I want to correct, working on an already open instance of the browser.

Is something similar possible through Selenium?

Upvotes: 3

Views: 1139

Answers (1)

Aphid
Aphid

Reputation: 363

There is the easy (recommended) way, and the complex way.

Easy:

Just import pdb and experiment with functionality in your browser session. Once you've got everything working as desired, update your code and remove the pdb line.

def fill_search():
        import pdb; pdb.set_trace()  # Experiment in the browser session
        for element in elements:
            element = driver.find_element_by_tag('input')
            if 'what are you looking for' in element.get_attribute('innerHTML'):
                search_box = element
                search_box.get_attribute('value') == 'happiness'

Complex:

driver = webdriver.Firefox()
session_id = driver.session_id
executor_url = driver.command_executor._url
same_session_driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
same_session_driver.session_id = session_id
same_session_driver.continue_doing_stuff

Upvotes: 2

Related Questions