Selenium for-ops
Click here to load reader
-
Upload
lukasz-proszek -
Category
Technology
-
view
1.557 -
download
0
description
Transcript of Selenium for-ops
![Page 2: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/2.jpg)
Background
I You’re a BOFH at GlobalFskcup Ltd.
I Everyday you deal with numerous incidends.
I You have to seek approval and/or log every change made.
I The Head of IT has his favorite tool for that
I MacroHard PointShare form
I Rich editor filled form, with sparkling format choosers.
I Bells and whistles included
![Page 3: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/3.jpg)
The soft problem
I Filling the form takes 5-20 minutes each time
I No PFY available to do the job
I Keeping templates in a text document... sooooo 1990
I Browser plugins - does not recognize all of provided inputs
I curl - yeah right, like I have time for that
![Page 4: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/4.jpg)
Selenium - basics
1 from selenium import webdriver
2 driver = webdriver.Firefox ()
3 driver.get("http :// google.com")
4 driver.find_element_by_id("...")
![Page 5: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/5.jpg)
how to identify element → inspect element
![Page 6: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/6.jpg)
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 ’)
![Page 7: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/7.jpg)
badumm tsss
![Page 8: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/8.jpg)
Something more complicated
![Page 9: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/9.jpg)
Show me the code
![Page 10: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/10.jpg)
What the fsck?
![Page 11: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/11.jpg)
Problem? HTML injection
I the id is a hash of something... different everytime
I 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")
![Page 12: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/12.jpg)
Problem? HTML injection
I We can not use webdriver.send keys() on that... it’sinside the html code
I 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)
![Page 13: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/13.jpg)
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 ()
![Page 14: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/14.jpg)
Selecting a dropdown option
1 driver.find_element_by_css_selector(
2 "option[value =’{0}’]".format(value)
3 ).click ()
![Page 15: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/15.jpg)
Sending the form
1 driver.find_elements_by_css_selector(
2 "input[value=’Save ’][type=’button ’]"
3 ).click ()
![Page 16: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/16.jpg)
Many python, such win
I 208 lines of Python
I virtualenv
I selenium for browser automation (pip install selenium)
I click for easy commandline (pip install click)
I path.py just because (pip install path.py)
I YAML for templates (pip instal pyYaML)
I keepass for credentials (pip install keepass)
![Page 17: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/17.jpg)
outcome, further plans, other uses
Outcome
I It took 3hrs to write
I It eared it’s keep after one day
Further plans
I webdriver.PhantomJS() for headless operation
I ansible integration
Other uses
I e.g. testing how long it takes the user to perform some action
![Page 18: Selenium for-ops](https://reader038.fdocuments.us/reader038/viewer/2022100601/557dfe4bd8b42a07708b4bdf/html5/thumbnails/18.jpg)
Thank You
I wiki2beamer - latex-beamer made easy
I impressive - pdf presentations with sugar added
I http://github.com/frogu/presentations