When you write automation test case with python, you always need to test the functions in different web browsers such as Firefox, Chrome, Safari and IE. This example will tell you how to configure and start related web browser in python source code.
1. Run Firefox Browser In Python.
- Download related geckodriver from https://github.com/mozilla/geckodriver/releases.
- Unzip the download file to a local directory.
- Use below python code to start a Firefox web browser. If you meet any error, you can read article How To Resolve Webdriverexception Geckodriver Executable Needs To Be In Path
from selenium import webdriver import time browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver') browser.get('https://www.google.com') time.sleep(10) browser.quit()
2. Run Chrome Browser In Python.
- Download related chromedriver from https://sites.google.com/a/chromium.org/chromedriver/
- Unzip chromedriver executable file to a local folder.
- Run below code to start google chrome web browser in python.
import time from selenium import webdriver browser = webdriver.Chrome(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/chromedriver') browser.get('https://www.google.com') time.sleep(10) browser.quit()
3. Run Safari Browser In Python.
Safari driver is included with MacOS Sierra by default, it is saved in folder /usr/bin. You can run which safaridriver
in command line to get it’s saved folder path. So you do not need to download and install safari driver.
But before you can start Safari web browser in python code, you need do following settings, otherwise you may encounter below error message when run python code.
selenium.common.exceptions.SessionNotCreatedException: Message: Could not create a session: You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver.
3.1 Enable Allow Remote Automation Option In Safari Web Browser.
- Open a safari web browser, then click Safari —> Preferences… menu item in the top menu bar.
- Click Advanced icon in the popup dialog. Check Show Develop menu in menu bar checkbox. After this you can see a
- Then there will display a Develop menu item in the Safari web browser menu bar. Click Develop —> Allow Remote Automation menu item to select it.
- Now run below python code to start the Safari web browser.
from selenium import webdriver import time browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver') browser.get('https://www.google.com') time.sleep(10) browser.quit()
4. Run Selenium WebDriver In Python Unittest.
- Import python build-in unittest module.
- Create a python class which extends unittest.TestCase.
- Start the web browser driver in TestCase class setUpClass method.
- Use the web browser driver in TestCase class test functions.
- Close and quit the web browser driver in TestCase class tearDownClass method.
''' Created on Aug 31, 2018 @author: zhaosong ''' from selenium import webdriver import time import unittest class ChromeBrowserTest(unittest.TestCase): chrome_browser = None page_url = '' # Class setup method. @classmethod def setUpClass(cls): ChromeBrowserTest.chrome_browser = webdriver.Chrome(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/chromedriver') print('Safari browser start.') # Class teardown method @classmethod def tearDownClass(cls): if(ChromeBrowserTest.chrome_browser!=None): ChromeBrowserTest.chrome_browser.quit() print('Safari browser quit.') else: print('Safari browser is not started.') # This is the test function. def browse_page(self): if(self.chrome_browser!=None and len(self.page_url)>0): self.chrome_browser.get(self.page_url) print("Safari browser browse page.") time.sleep(10) else: print('Safari browser is not started or page_url is empty.') if __name__ == '__main__': ChromeBrowserTest.page_url = 'https://www.baidu.com' # Create a TestSuite object. test_suite = unittest.TestSuite() # Add test function in the suite. test_suite.addTest(ChromeBrowserTest('browse_page')) # Run test suite and get test result. testResult = unittest.TestResult() test_suite.run(testResult) print(testResult)
Reference
thank you for ur information. this documentation is so useful for me.