Selenium Web Driver: some tricks using Python

After migrating to Web Driver it took time to get with the following things:

* Selecting element from drop down list (now not just select command), e.g.:

el = driver.find_element_by_id('id_line')
for option in el.find_elements_by_tag_name('option'):
    if option.text == "line to select":
        option.click()
        break

* If there are lot of element existence checks that wrapped in try-catch block it is useful to reduce implicit timeout to reduce overall test execution time, e.g. for Firefox driver:

def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(5)
    self.verificationErrors = []

* Try to avoid time.sleep(N) commands in tests it is better to wait for some action or change in system. So the best way to make construction:


try:
    if self.is_element_present(By.CSS_SELECTOR, "h3 > strong"):
        driver.find_element_by_link_text("169_convertedvideo_425").click()
        self.assertTrue(int(driver.find_elements_by_css_selector("#video_source span.black")[0].text) >= 0)
    else:
        raise Exception(self.id() + ".ErrorText!")
except Exception as e: self.verificationErrors.append(str(e))
finally:
    self.removeAllMonitoringSources(driver)

or wait by small ticks:


for i in range(60):
    try:
        if self.is_element_present(By.LINK_TEXT, "Exit"): break
    except: pass

    time.sleep(1)

else: self.fail("time out")

* Xpath is always slower in IE
* If some elements in DOM are hidden you can access them using JavaScript execution command driver.execute_script(script, *args)

SeleniumHQ: launching MS Visual Studio tests for different browsers

Sometimes you need to automate functional tests for application with Web UI. SeleniumHQ is a free tool that is good for this purposes which you can integrate with MS Visual Studio.

You can launch test in different browsers like this: Continue reading “SeleniumHQ: launching MS Visual Studio tests for different browsers”

IE: cache problems

When you are writing java scrips sometimes Internet Explorer cache problems occur. IE caches even ajax get responses.

To avoid it you need to change you javascript code in following way:

<br />
$(document).ready(function() {<br />
   /* you have to put the following line right after above line */<br />
   $.ajaxSetup({cache: false});<br />
   /* rest of the code */<br />
   ...<br />

Continue reading “IE: cache problems”