Selenium Webdriver is a tool for automated testing of Web pages. It is available for a range of programming languages (Java, Python, etc.) and for the most popular Web browsers (Firefox, Chrome, Safari, Internet Explorer).
This deck will drive you though Selenium fundamentals and some basic and hands-on examples.
2. Web automatization
• Perform actions like a user would do
– Testing webpages
• Selenium:
• Selenium Webdriver: native browser
• Selenium Grid: tests in different servers
• Selenium IDE: recording of actions
3. Selenium Webdriver features
• Browsers:
– Firefox
– Chrome
– Internet Explorer
– Safari
– PhantomJS (headless browser)
• Languages:
– Java
– Python
– Javascript
– Ruby
– C#
• Customization of browser settings: addons, proxy, cookies…
5. Basics
• Import
from selenium import webdriver
• Open browser:
driver = webdriver.Firefox()
driver = webdriver.Chrome()
driver = webdriver.Safari()
• Page retrieving
driver.get("http://www.python.org")
This will stop the execution of the program until the webpage is
completely loaded
6. Finding elements
• Find element by id, name, class name,
tag name, xpath…
element = driver.find_element_by_id("id-search-field")
element = driver.find_element_by_class_name("search-field”)
element = driver.find_element_by_tag_name("input")
element = driver.find_element_by_xpath("//*[contains(text(),'ABC')]")
element = driver.find_element_by_name("q")
7. Forms (I)
• Textboxes
– element.send_keys("pycon")
• Select
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_tag_name("select"))
select.select_by_visible_text("Mercedes")
• Multiselect
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id("select_multiple"))
select.deselect_all()
select.select_by_visible_text("Audi")
select.select_by_visible_text("Ferrari")
8. Forms (II)
• Checkboxes
driver.find_element_by_id("cbox1").click()
• Button
driver.find_element_by_id("submit").click()
Radio button: driver.find_element_by_id("radio2").click()
• Get text
element = driver.find_element_by_id("counter")
element.text
• Keys
from selenium.webdriver.common.keys import Keys
element.send_keys(Keys.RETURN)
element.send_keys(Keys.COMMAND, 'f')
9. Advance features (I)
• Moving between windows and iframes
– HTML: <a href="somewhere.html" target="windowName">Click here to
open a new window</a>
– Python: driver.switch_to.window("windowName")
• Dialogs
– alert = driver.switch_to.alert
– alert.dismiss()
• Navigation
– driver.forward()
– driver.back()
10. Browser settings (I)
• Cookies:
driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})
driver.get_cookies() # Only first party cookies
driver.delete_cookie("CookieName")
driver.delete_all_cookies()
• User Agent
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "some UA string")
driver = webdriver.Firefox(profile)
13. Closing the browser
• There are two functions:
driver.quit()
driver.close()
• The difference is if the profile folder is deleted or not
– quit: deletes the folder
– close: doesn’t delete the folder
• Profile folder has a name starting with tmp and some
random characters
• Location of the folder
– Mac: /private/var and one random subfolder
– Linux: /tmp/
• You can also check the profile folder in about:support in
the Firefox of Selenium
14. Waits
• What happens when you click an element
and it is not present or hidden? =>
Exception!!
• To solve this problems, there are two kinds
of waits: implicit and explicit
– Implicit wait: maximum time to wait for elements
to be present in the DOM
– Explicit wait + expected condition: maximum
time to wait for elements to be present and
condition is true
16. Implicit and explicit waits
• Implicit wait:
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("http://some_url")
driver.find_element_by_id(”element").click() # Will wait for 10s until the element is present
• Explicit wait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get(”http://some_url")
element = wait.until(EC.element_to_be_clickable((By.ID,’some_element')))
17. No GUI
• When working without GUI is important to set the
window size/resolution, as webpages may not be
displayed properly
• PhantomJS
driver = webdriver.PhantomJS()
driver.set_window_size(1400,1000)
• Ubuntu
– Install xvfb: apt-get install xvfb
– Package for python: pip install pyvirtualdisplay
– File:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
…
display.stop()
19. Conclusion: advices using Selenium
• Launch always the same version of browser with
FirefoxBinary
• When finding elements, use WebDriverWait to check that
the element complies with the action you want to
perform
• Selection of ids: not choose random ids
• Working without GUI: set resolution as big as
possible
20. Conclusion: advices using Selenium
• Timers: sometimes webpages are not loaded
properly and the browser gets stuck. In order to
solve this, you can set preferences in Firefox:
driver.set_page_load_timeout(10)
driver.set_script_timeout(10)
profile.set_preference(“network.http.response.timeout”, 10)
profile.set_preference(“dom.max_script_run_time”, 10)
profile.set_preference("network.http.connection-retry-timeout", 10);