Home | 簡體中文 | 繁體中文 | 雜文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品與服務 | Email

第 6 章 Selenium - Web Browser Automation

目錄

6.1. Selenium RC
6.2. 支持瀏覽器
6.3. python
6.3.1. python example
6.4. ruby
6.4.1. ruby example
6.5. Node.js selenium-webdriver
6.5.1. 安裝測試環境
6.5.1.1. Selenium Server
6.5.1.2. 安裝 selenium-webdriver
6.5.1.3. 瀏覽器相關設置
6.5.2. 測試例子
6.5.3. Unit Test
6.6. PHP
6.6.1. 安裝 PHPUNIT 與 Selenium
6.6.2. Example

http://selenium.seleniumhq.org/

6.1. Selenium RC

http://seleniumhq.org/download/
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.19.0.jar
java -jar selenium-server-standalone-2.19.0.jar
		

預設連接埠是4444,可以使用-port 改變連接埠

java -jar selenium-server.jar -Dfile.encoding="Unicode" 指定編碼

from selenium import selenium
# This is the driver's import.  You'll use this class for instantiating a
# browser and making it do what you need.

import unittest, time, re
# This are the basic imports added by Selenium-IDE by default.
# You can remove the modules if they are not used in your script.

class NewTest(unittest.TestCase):
# We create our unittest test case

    def setUp(self):
        self.verificationErrors = []
        # This is an empty array where we will store any verification errors
        # we find in our tests

        self.selenium = selenium("192.168.1.3", 5555, "*firefox",
                "http://www.google.com/")
        self.selenium.start()
        # We instantiate and start the browser

    def test_new(self):
        # This is the test code.  Here you should put the actions you need
        # the browser to do during your test.

        sel = self.selenium
        # We assign the browser to the variable "sel" (just to save us from
        # typing "self.selenium" each time we want to call the browser).

        sel.open("/")
        sel.type("q", "selenium rc")
        sel.click("btnG")
        sel.wait_for_page_to_load("30000")
        self.failUnless(sel.is_text_present("Results * for selenium rc"))
        # These are the real test steps

    def tearDown(self):
        self.selenium.stop()
        # we close the browser (I'd recommend you to comment this line while
        # you are creating and debugging your tests)

        self.assertEqual([], self.verificationErrors)
        # And make the test fail if we found that any verification errors
        # were found

if __name__ == "__main__":
    unittest.main()
		

firefox

self.selenium = selenium("localhost", 4444, "*firefox", "http://www.google.com/")
		

iexplore

self.selenium = selenium("localhost", 4444, "*iexplore", "http://www.google.com/")
		

chrome

self.selenium = selenium("localhost", 4444, "*chrome", "http://google.com/")
		

通過絶對路徑指定瀏覽器

self.selenium = selenium(
    "localhost",
    4444,
    "c:\\program files\\internet explorer\\iexplore.exe",
    "http://google.com/"
)
		
comments powered by Disqus