Python實現自動化測試入門指南
This is a quick introduction to Selenium WebDriver in Python on Ubuntu/Debian systems.
WebDriver (part of Selenium 2) is a library for automating browsers, and can be used from a variety of language bindings. It allows you to programmatically drive a browser and interact with web elements. It is most often used for test automation, but can be adapted to a variety of web scraping or automation tasks.
To use the WebDriver API in Python, you must first install the Selenium Python bindings. This will give you access to your browser from Python code. The easiest way to install the bindings is via pip.
On Ubuntu/Debian systems, this will install pip (and dependencies) and then install the Selenium Python bindings from PyPI:
$ sudo pip install seleniumAfter the installation, the following code should work:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')This should open a Firefox browser sessions and navigate to http://www.ubuntu.com/
Here is a simple functional test in Python, using Selenium WebDriver and the unittest framework:
import unittest
from selenium import webdriver
class TestUbuntuHomepage(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def testTitle(self):
self.browser.get('http://www.ubuntu.com/')
self.assertIn('Ubuntu', self.browser.title)
def tearDown(self):
self.browser.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)Output:
----------------------------------------------------------------------
Ran 1 test in 5.931s
OK
WebDriver (part of Selenium 2) is a library for automating browsers, and can be used from a variety of language bindings. It allows you to programmatically drive a browser and interact with web elements. It is most often used for test automation, but can be adapted to a variety of web scraping or automation tasks.
To use the WebDriver API in Python, you must first install the Selenium Python bindings. This will give you access to your browser from Python code. The easiest way to install the bindings is via pip.
On Ubuntu/Debian systems, this will install pip (and dependencies) and then install the Selenium Python bindings from PyPI:
CODE:
$ sudo apt-get install python-pip$ sudo pip install seleniumAfter the installation, the following code should work:
CODE:
#!/usr/bin/env pythonfrom selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')This should open a Firefox browser sessions and navigate to http://www.ubuntu.com/
Here is a simple functional test in Python, using Selenium WebDriver and the unittest framework:
CODE:
#!/usr/bin/env pythonimport unittest
from selenium import webdriver
class TestUbuntuHomepage(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def testTitle(self):
self.browser.get('http://www.ubuntu.com/')
self.assertIn('Ubuntu', self.browser.title)
def tearDown(self):
self.browser.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)Output:
CODE:
testTitle (__main__.TestUbuntuHomepage) ... ok----------------------------------------------------------------------
Ran 1 test in 5.931s
OK
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734437/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python自動化測試框架有哪些?Python入門!Python框架
- 【自動化測試入門】自動化測試思維
- 【必看】Python自動化測試框架,Python入門知識!Python框架
- 前端自動化測試入門前端
- 用python實現selenium 自動化測試Python
- Jest前端自動化測試入門前端
- 在 Postman 中實現自動化測試的全面指南Postman
- 【入門必備】超實用的五種python自動化測試框架!Python框架
- python自動化測試工具selenium使用指南Python
- 自動化測試框架指南框架
- Android自動化測試入門(四)單元測試Android
- airtest自動化測試工具快速入門AI
- 自動化測試與軟體測試有什麼區別?Python入門教程Python
- Python實現效能自動化測試竟然如此簡單Python
- python自動化測試Python
- UI自動化測試介紹及入門UI
- 如何實現高度自動化測試?
- Postman實現UI自動化測試PostmanUI
- API自動化測試平臺,高效實現對API的自動化測試API
- Appium+Python實現iOS自動化測試~環境搭建APPPythoniOS
- WebUI 自動化測試-PO 設計模式入門WebUI設計模式
- Python實時物件檢測入門指南Python物件
- Python 介面自動化測試Python
- postman實現介面的自動化測試Postman
- 使用 Postman 實現 API 自動化測試PostmanAPI
- 故障測試入門指南
- 試著使用 jmeter 實現介面自動化測試JMeter
- 新手入門Java自動化測試的利器:Selenium WebDriverJavaWeb
- PHP 開發入門自動化測試歷程(一)PHP
- PHP 開發入門自動化測試歷程(二)PHP
- PHP 開發入門自動化測試歷程(三)PHP
- Python 實現行為驅動開發 (BDD) 自動化測試詳解Python
- HamronyOS 自動化測試框架使用指南框架
- Python 自動化測試框架unittestPython框架
- selenium+python自動化測試Python
- Python自動化測試框架-pytestPython框架
- python自動化測試-原創Python
- 自動化測試如何實現全面覆蓋
- 自動化測試系列 —— UI自動化測試UI