Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

6.4. python

apt-get install python-pip
sudo pip install -U selenium
	
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.0b3.jar
java -jar selenium-server-standalone-2.0b3.jar

vim test.py

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.keys import Keys
from time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert browser.title == "Yahoo!"
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("selenium" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
	

6.4.1. python example

例 6.1. python testcase

			
from selenium import selenium
import unittest, time, re

class testcase(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://www.google.com/")
        self.selenium.start()

    def test_testcase(self):
        sel = self.selenium
        sel.open("/")
        sel.type("name=q", "netkiller")
        sel.click("name=btnG")
        self.assertEqual(u"Neo")

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

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