+ All Categories
Home > Software > Controlling the browser through python and selenium

Controlling the browser through python and selenium

Date post: 15-Jul-2015
Category:
Upload: pviafore
View: 156 times
Download: 4 times
Share this document with a friend
Popular Tags:
45
Transcript
Page 1: Controlling the browser through python and selenium
Page 2: Controlling the browser through python and selenium

• Pat Viafore

•ADTRAN

•Past work at TOA Systems

•Python at home

Page 3: Controlling the browser through python and selenium

• What is Selenium?

• How Do I Use It?

• Getting Information Off a Page

• Controlling Input

• Advanced Features

• Limitations

• Rejoicing

Page 4: Controlling the browser through python and selenium

And what can it do for me?

Page 5: Controlling the browser through python and selenium
Page 6: Controlling the browser through python and selenium

• Allows scripts to take control of a web browser

• Supports multiple language bindings

• We’ll focus on Python today

• Windows/Linux/MacOS Support

• $> pip install selenium

• Licensed under Apache 2.0

• Easy to use

Page 7: Controlling the browser through python and selenium

Selenium

Remote

Control

Selenium 2.0

Selenium IDE

Selenium

Grid

Page 8: Controlling the browser through python and selenium

Selenium

Remote

Control

Selenium 2.0

Selenium IDE

Selenium

Grid

Page 9: Controlling the browser through python and selenium
Page 10: Controlling the browser through python and selenium
Page 11: Controlling the browser through python and selenium
Page 12: Controlling the browser through python and selenium

• I currently work on an internal web-app

• I use Selenium to drive acceptance tests – making sure

the web page operates as we expect it to

• Database interactions

• Error conditions

• External service tests (e-mail, LDAP, etc.)

• I use Python to “glue” web browser interaction with

system events

Page 13: Controlling the browser through python and selenium
Page 14: Controlling the browser through python and selenium

• We’re not just limited to acceptance tests

• Automate common tasks you use a web browser for

Page 15: Controlling the browser through python and selenium

• Hook up Selenium to data feeds to control web pages

• Hulu

• Pandora

• YouTube

• Take care of things on a schedule

• Pay bills online

• Sign up for more Python Meetings

Page 16: Controlling the browser through python and selenium
Page 17: Controlling the browser through python and selenium

Enough with the blah, blah, blah….Let’s Code!

Page 18: Controlling the browser through python and selenium
Page 19: Controlling the browser through python and selenium
Page 20: Controlling the browser through python and selenium
Page 21: Controlling the browser through python and selenium
Page 22: Controlling the browser through python and selenium
Page 23: Controlling the browser through python and selenium
Page 24: Controlling the browser through python and selenium

• browser.find_element_by_css_selector

• browser.find_element_by_xpath

Page 25: Controlling the browser through python and selenium

Because just getting things is boring

Page 26: Controlling the browser through python and selenium
Page 27: Controlling the browser through python and selenium
Page 28: Controlling the browser through python and selenium
Page 29: Controlling the browser through python and selenium

• Go to YouTube

• Search for Python programming

• Sort by Recent Date

• Click on a random video

Page 30: Controlling the browser through python and selenium
Page 31: Controlling the browser through python and selenium

Bring on the cool stuff

Page 32: Controlling the browser through python and selenium
Page 33: Controlling the browser through python and selenium
Page 34: Controlling the browser through python and selenium
Page 35: Controlling the browser through python and selenium

• Screenshotting

• Execute arbitrary Javascript code

• browser.execute_script(jsCode)

• Also an async version

• Browser Capabilities

• Proxying

Page 36: Controlling the browser through python and selenium

• Navigate to Google Maps

• Move mouse to center of map

• Start scrolling north a bunch

• Move the mouse or press keys through chainable actions

• More options about ActionChains

http://selenium-python.readthedocs.org/en/latest/api.html

Page 37: Controlling the browser through python and selenium
Page 38: Controlling the browser through python and selenium

• We could use XVFB or something similar on certain

Linux distros

• Instead, we’ll use PhantomJS to create a headless

browser

• Separate JS installation

• We’ll create X threads that do a series of interactions and time

them

Page 39: Controlling the browser through python and selenium
Page 40: Controlling the browser through python and selenium

There is no such thing as a Silver Bullet

Page 41: Controlling the browser through python and selenium

• Why Use Selenium?

• Why Not Mocha?

• Why Not Jasmine?

• Why Not GUI Recorders?

Page 42: Controlling the browser through python and selenium

• RESTful Interactions

• Requests does a far better job

• User Interface (UI) testing

• Selenium does offer Location/Size/CSS, but you’ll

probably be rolling your own layer on top of Selenium

to handle UI testing

• Brittle UIs

• UIs change often (classes, ID changes, buttons

changes)

• Can mitigate by providing a wrapper around Selenium

to interact with your site – DRY (Don’t Repeat Yourself)

Principle

Page 43: Controlling the browser through python and selenium

Pizza, pizza, pizza!

Page 44: Controlling the browser through python and selenium
Page 45: Controlling the browser through python and selenium

Recommended