2016 09-09 - selenium webdriver - fundamentals + hands-on!

21
Selenium Webdriver Fundamentals + Hands- on! by Miriam Marciel September 2016

Upload: alfonso-peletier

Post on 19-Jan-2017

96 views

Category:

Software


1 download

TRANSCRIPT

Page 1: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Selenium Webdriver Fundamentals + Hands-

on!

by Miriam MarcielSeptember 2016

Page 2: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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

Page 3: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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…

Page 4: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Selenium installation• Java– Download *.jar/Maven

• Pythonpip install selenium

• Javascriptnpm install selenium-webdriver

• Rubygem install selenium-webdriver

Page 5: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Basics• Import

from selenium import webdriver

• Open browser:driver = webdriver.Firefox()driver = webdriver.Chrome()driver = webdriver.Safari()

• Page retrievingdriver.get("http://www.python.org")This will stop the execution of the program until the webpage is completely loaded

Page 6: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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")

Page 7: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Forms (I)• Textboxes

– element.send_keys("pycon")

• Selectfrom selenium.webdriver.support.ui import Selectselect = Select(driver.find_element_by_tag_name("select"))select.select_by_visible_text("Mercedes")

• Multiselectfrom selenium.webdriver.support.ui import Selectselect = Select(driver.find_element_by_id("select_multiple"))select.deselect_all()select.select_by_visible_text("Audi")select.select_by_visible_text("Ferrari")

Page 8: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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 textelement = driver.find_element_by_id("counter")element.text

• Keys from selenium.webdriver.common.keys import Keyselement.send_keys(Keys.RETURN)element.send_keys(Keys.COMMAND, 'f')

Page 9: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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()

Page 10: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Browser settings (I)• Cookies:

driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})driver.get_cookies() # Only first party cookiesdriver.delete_cookie("CookieName")driver.delete_all_cookies()

• User Agentprofile = webdriver.FirefoxProfile() profile.set_preference("general.useragent.override", "some UA string") driver = webdriver.Firefox(profile)

Page 11: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Browser settings (II)• Proxy

proxy = Proxy({'httpProxy': myProxy}) driver = webdriver.Firefox(proxy=proxy)

• Firefox Binarybinary = FirefoxBinary("firefox-executable");driver = webdriver.Firefox(firefox_binary=binary)

Page 12: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Browser settings (III)• Plugins/Addons

profile = webdriver.FirefoxProfile()profile.add_extension(extension=’path-to-xpi/ublock.xpi')driver_addon = webdriver.Firefox(firefox_profile=profile)

• about:config options of Firefoxprofile = webdriver.FirefoxProfile()profile.set_preference("javascript.enabled", False);driver = webdriver.Firefox(firefox_profile=profile)

Page 13: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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

Page 14: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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

Page 15: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

Explicit wait conditions• title_is• title_contains• presence_of_element_located• visibility_of_element_located• visibility_of• presence_of_all_elements_located• text_to_be_present_in_element• text_to_be_present_in_element_value• frame_to_be_available_and_switch_to_it• invisibility_of_element_located• element_to_be_clickable • staleness_of• element_to_be_selected• element_located_to_be_selected• element_selection_state_to_be• element_located_selection_state_to_be• alert_is_present

Page 16: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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 waitdriver = webdriver.Firefox()wait = WebDriverWait(driver, 10)driver.get(”http://some_url")element = wait.until(EC.element_to_be_clickable((By.ID,’some_element')))

Page 17: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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 Displaydisplay = Display(visible=0, size=(800, 600))display.start()…display.stop()

Page 18: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

DEMO

Page 19: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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

Page 20: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

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);

Page 21: 2016 09-09 - Selenium Webdriver - Fundamentals + Hands-on!

21

SELENIUM WEBDRIVER: FUNDAMENTALS + HANDS-ON!

Miriam Marciel09/09/2016