標籤:python firefox 測試 selenium profile
1.建立FireFox的profile
1.1 dos切換到Firefox的安裝目錄,例如:C:\Program Files\Mozilla Firefox
1.2 dos中執行命令firefor -p,彈出建立視窗(如下:),新增一個profile。此處建立在c:\xxx下
1.3 建立完成後,點擊中的"啟動Firefox",在c:\xxx下可以看見產生一些檔案(最重要的貌似是cert8.db)
2.寫一個測試指令碼test.py,執行觀察結果
2.1指令碼如下:
#coding=utf-8
import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestFireFoxProfile(unittest.TestCase):
def setUp(self):
profile = webdriver.FirefoxProfile(r‘c:\xxx‘)
self._browser = webdriver.remote.webdriver.WebDriver(\
command_executor=‘http://127.0.0.1:4444/wd/hub‘,\
browser_profile=profile,\
desired_capabilities=DesiredCapabilities.FIREFOX)
self._browser.get("http://www.baidu.com")
def test_tile(self):
assert "百度一下,你就知道" in self._browser.title
def tearDown(self):
self._browser.quit()
if __name__ == "__main__":
unittest.main()
2.2 啟動Server ---主要是想看啟動過程以及Remote方法的使用
2.3 執行test.py指令碼,server顯示如下資訊:
22:18:40.495 INFO - Java: Sun Microsystems Inc. 11.0-b15
22:18:40.496 INFO - OS: Windows Vista 6.1 x86
22:18:40.507 INFO - v2.37.0, with Core v2.37.0. Built from revision a7c61cb
22:18:40.583 INFO - Default driver org.openqa.selenium.iphone.IPhoneDriver registration is skipped: registration capabilities Capabilities [{platform=MAC, browserName=iPhone, version=}] does not match with current platform: VISTA
22:18:40.585 INFO - Default driver org.openqa.selenium.iphone.IPhoneDriver registration is skipped: registration capabilities Capabilities [{platform=MAC, browserName=iPad, version=}] does not match with current platform: VISTA
22:18:40.632 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
22:18:40.634 INFO - Version Jetty/5.1.x
22:18:40.634 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
22:18:40.635 INFO - Started HttpContext[/selenium-server,/selenium-server]
22:18:40.635 INFO - Started HttpContext[/,/]
22:18:40.657 INFO - Started [email protected]
22:18:40.657 INFO - Started HttpContext[/wd,/wd]
22:18:40.660 INFO - Started SocketListener on 0.0.0.0:4444
22:18:40.660 INFO - Started [email protected]
22:18:55.135 INFO - Executing: [new session: {platform=ANY, firefox_profile=UEsDBBQAAAAIAKWurEYan7HfMwYAA..., javascriptEnabled=true, browserName=firefox, version=}] at URL: /session)
22:18:55.224 INFO - Creating a new session for Capabilities [{firefox_profile=UEsDBBQAAAAIAKWurEYan7HfMwYAAAAAAQAIAAAAY2VydDguZGLt3WtQVFUAB/Bz713WFURNEFB5
.................................省略....................
rEazUQ1nLgAAACwAAAAkAAAAAAAAAAAAAAC2gUAgAgBzYWZlYnJvd3NpbmcvdGVzdC1waGlzaC1z
aW1wbGUuY2FjaGVQSwECFAAUAAAACACkrqxGxNrTQgUAAAAQAAAAIwAAAAAAAAAAAAAAtoGwIAIA
c2FmZWJyb3dzaW5nL3Rlc3QtcGhpc2gtc2ltcGxlLnBzZXRQSwECFAAUAAAACACkrqxGOr4ZNE8A
AADoAAAAJgAAAAAAAAAAAAAAtoH2IAIAc2FmZWJyb3dzaW5nL3Rlc3QtcGhpc2gtc2ltcGxlLnNi
c3RvcmVQSwECFAAUAAAACAClrqxG6sGSGmxvDQBJig0AIgAAAAAAAAAAAAAAtoGJIQIAc3RhcnR1
cENhY2hlL3N0YXJ0dXBDYWNoZS40LmxpdHRsZVBLBQYAAAAAJAAkAFsJAAA1kQ8AAAA=
, platform=ANY, javascriptEnabled=true, browserName=firefox, version=}]
22:19:03.237 INFO - Done: /session
22:19:03.340 INFO - Executing: [get: http://www.baidu.com] at URL: /session/2a7bc3e0-d62c-465c-8dba-2e4f962af4d7/url)
22:19:06.251 INFO - Done: /session/2a7bc3e0-d62c-465c-8dba-2e4f962af4d7/url
22:19:06.255 INFO - Executing: [get title] at URL: /session/2a7bc3e0-d62c-465c-8dba-2e4f962af4d7/title)
22:19:06.299 INFO - Done: /session/2a7bc3e0-d62c-465c-8dba-2e4f962af4d7/title
22:19:06.328 INFO - Executing: [delete session: 2a7bc3e0-d62c-465c-8dba-2e4f962af4d7] at URL: /session/2a7bc3e0-d62c-465c-8dba-2e4f962af4d7)
22:19:06.528 INFO - Done: /session/2a7bc3e0-d62c-465c-8dba-2e4f962af4d7
Python+WebDriver 啟動Firefox時使用profile