Home | Mirror | Search

2. 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()
		

2.1. python example

例 6.2. 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()
				
				

comments powered by Disqus