KEMBAR78
Selenium for-ops | PDF
Selenium for OPS
Admins Anonymous meetup #5
Lukasz Proszek
lukasz@proszek.info
2014-06-17
Background
You’re a BOFH at GlobalFskcup Ltd.
Everyday you deal with numerous incidends.
You have to seek approval and/or log every change made.
The Head of IT has his favorite tool for that
MacroHard PointShare form
Rich editor filled form, with sparkling format choosers.
Bells and whistles included
The soft problem
Filling the form takes 5-20 minutes each time
No PFY available to do the job
Keeping templates in a text document... sooooo 1990
Browser plugins - does not recognize all of provided inputs
curl - yeah right, like I have time for that
Selenium - basics
1 from selenium import webdriver
2 driver = webdriver.Firefox ()
3 driver.get("http :// google.com")
4 driver. find_element_by_id ("...")
how to identify element → inspect element
Selenium - interact with webelement
1 from selenium import webdriver
2 driver = webdriver.Firefox ()
3 driver.get("http :// google.com")
4 fld=driver. find_element_by_id (’gbqfq ’)
5 fld.send_keys(’Anonimowi Admini ’)
badumm tsss
Something more complicated
Show me the code
What the fsck?
Problem? HTML injection
the id is a hash of something... different everytime
Let’s get the RichEditor’s class
1 e = "table.ms -formtable > tbody >
2 tr:nth -of -type (7) > td.ms -formbody >
3 span[dir=’none ’] > div >
4 div:nth -of -type (2)"
5 c=driver. find_element_by_css_selector (e)
6 .get_attribute("class")
Problem? HTML injection
We can not use webdriver.send keys() on that... it’s
inside the html code
We have to inject HTML with javascript
1 JS="document. getElementsByClassName ({0})[{1}]
2 .innerHTML =’{2}’".format(
3 CLASS_NAME , INDEX ,
4 "<marquee > WTF </marquee >"
5 )
6 driver.execute_script(JS)
Clicking a radio button
1 driver. find_element_by_css_selector (
2 "input[type=’radio ’][ value =’{0}’]".format(
3 "Approved by the CEO"
4 )
5 ). click ()
Selecting a dropdown option
1 driver. find_element_by_css_selector (
2 "option[value =’{0}’]".format(value)
3 ). click ()
Sending the form
1 driver. find_elements_by_css_selector (
2 "input[value=’Save ’][type=’button ’]"
3 ). click ()
Many python, such win
208 lines of Python
virtualenv
selenium for browser automation (pip install selenium)
click for easy commandline (pip install click)
path.py just because (pip install path.py)
YAML for templates (pip instal pyYaML)
keepass for credentials (pip install keepass)
outcome, further plans, other uses
Outcome
It took 3hrs to write
It eared it’s keep after one day
Further plans
webdriver.PhantomJS() for headless operation
ansible integration
Other uses
e.g. testing how long it takes the user to perform some action
Thank You
wiki2beamer - latex-beamer made easy
impressive - pdf presentations with sugar added
http://github.com/frogu/presentations

Selenium for-ops

  • 1.
    Selenium for OPS AdminsAnonymous meetup #5 Lukasz Proszek lukasz@proszek.info 2014-06-17
  • 2.
    Background You’re a BOFHat GlobalFskcup Ltd. Everyday you deal with numerous incidends. You have to seek approval and/or log every change made. The Head of IT has his favorite tool for that MacroHard PointShare form Rich editor filled form, with sparkling format choosers. Bells and whistles included
  • 3.
    The soft problem Fillingthe form takes 5-20 minutes each time No PFY available to do the job Keeping templates in a text document... sooooo 1990 Browser plugins - does not recognize all of provided inputs curl - yeah right, like I have time for that
  • 4.
    Selenium - basics 1from selenium import webdriver 2 driver = webdriver.Firefox () 3 driver.get("http :// google.com") 4 driver. find_element_by_id ("...")
  • 5.
    how to identifyelement → inspect element
  • 6.
    Selenium - interactwith webelement 1 from selenium import webdriver 2 driver = webdriver.Firefox () 3 driver.get("http :// google.com") 4 fld=driver. find_element_by_id (’gbqfq ’) 5 fld.send_keys(’Anonimowi Admini ’)
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    Problem? HTML injection theid is a hash of something... different everytime Let’s get the RichEditor’s class 1 e = "table.ms -formtable > tbody > 2 tr:nth -of -type (7) > td.ms -formbody > 3 span[dir=’none ’] > div > 4 div:nth -of -type (2)" 5 c=driver. find_element_by_css_selector (e) 6 .get_attribute("class")
  • 12.
    Problem? HTML injection Wecan not use webdriver.send keys() on that... it’s inside the html code We have to inject HTML with javascript 1 JS="document. getElementsByClassName ({0})[{1}] 2 .innerHTML =’{2}’".format( 3 CLASS_NAME , INDEX , 4 "<marquee > WTF </marquee >" 5 ) 6 driver.execute_script(JS)
  • 13.
    Clicking a radiobutton 1 driver. find_element_by_css_selector ( 2 "input[type=’radio ’][ value =’{0}’]".format( 3 "Approved by the CEO" 4 ) 5 ). click ()
  • 14.
    Selecting a dropdownoption 1 driver. find_element_by_css_selector ( 2 "option[value =’{0}’]".format(value) 3 ). click ()
  • 15.
    Sending the form 1driver. find_elements_by_css_selector ( 2 "input[value=’Save ’][type=’button ’]" 3 ). click ()
  • 16.
    Many python, suchwin 208 lines of Python virtualenv selenium for browser automation (pip install selenium) click for easy commandline (pip install click) path.py just because (pip install path.py) YAML for templates (pip instal pyYaML) keepass for credentials (pip install keepass)
  • 17.
    outcome, further plans,other uses Outcome It took 3hrs to write It eared it’s keep after one day Further plans webdriver.PhantomJS() for headless operation ansible integration Other uses e.g. testing how long it takes the user to perform some action
  • 18.
    Thank You wiki2beamer -latex-beamer made easy impressive - pdf presentations with sugar added http://github.com/frogu/presentations