Selenium
In this tutorial, we'll get Selenium, Google Chrome, and ChromeDriver installed.
Selenium is a portable software testing framework for web applications.
Selenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including Java, C#, Groovy, Perl, PHP, Python and Ruby.
The tests can then be run against most modern web browsers. Selenium deploys on Windows, Linux, and Macintosh platforms. It is open-source software, released under the Apache 2.0 license, and can be downloaded and used without charge.
...
Selenium WebDriver is the successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenese, or via a Client API) and sends them to a browser. This is implemented through a browser-specific browser driver, which sends commands to a browser, and retrieves results.
We'll use virtualenv with Python3:
$ virtualenv -p python3 venv3 $ source venv3/bin/activate (venv3)$ (venv3)$ pip install selenium
Check the Selenium version from Python shell:
>>> from selenium import webdriver >>> from selenium import selenium >>> print ("Selenium webdriver Version: %s" % (webdriver.__version__)) Selenium webdriver Version: 2.53.6
$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb $ sudo dpkg -i --force-depends google-chrome-stable_current_amd64.deb
$ LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE) $ wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip $ unzip chromedriver_linux64.zip && sudo ln -s $PWD/chromedriver /usr/local/bin/chromedriver
(venv3)$ python Python 3.4.3 >>> from selenium import webdriver >>> d = webdriver.Chrome() >>> d.get("//www.google.com/") >>> d.title 'Google' >>>
We can access the classes like this (WebDriver API):
webdriver.Firefox webdriver.FirefoxProfile webdriver.Chrome webdriver.ChromeOptions webdriver.Ie webdriver.Opera webdriver.PhantomJS webdriver.Remote webdriver.DesiredCapabilities webdriver.ActionChains webdriver.TouchActions webdriver.Proxy
The following example (selenium 3.0.0b1) does:
- open a new Firefox browser
- load the Yahoo homepage
- search for "selenium"
- close the browser
from selenium import webdriver from selenium.webdriver.common.keys import Keys d = webdriver.Chrome() d.get('http://www.yahoo.com') assert 'Yahoo' in d.title elem = d.find_element_by_name('p') # Find the search box elem.send_keys('selenium' + Keys.RETURN) d.quit()
Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example uisng Python's standard unittest library (https://pypi.python.org/pypi/selenium):
import unittest from selenium import webdriver class GoogleTestCase(unittest.TestCase): def setUp(self): self.browser = webdriver.Chrome() self.addCleanup(self.browser.quit) def testPageTitle(self): self.browser.get('//www.google.com') self.assertIn('Google', self.browser.title) if __name__ == '__main__': unittest.main(verbosity=2)
Output:
python unit.py testPageTitle (__main__.GoogleTestCase) ... ok ---------------------------------------------------------------------- Ran 1 test in 12.203s OK
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization