#!/usr/bin/env python # -*- coding: utf-8 -*- from selenium import selenium import unittest import time import re import os from os import environ as e import random from collections import deque TIMEOUT = 2000 RCHOST = "192.168.1.10" RCPORT = "4444" ENVIRONMENT = "*firefox" BASEURL = "http://192.168.1.10/" class SeleniumTester(unittest.TestCase): def setUp(self): self.verificationErrors = [] self.rcHost = e.get('SELENIUM_RC_HOST', RCHOST) self.rcPort = e.get('SELENIUM_RC_PORT', RCPORT) self.environment = e.get('SELENIUM_ENVIRONMENT', ENVIRONMENT) self.baseURL = e.get('SELENIUM_BASE_URL', BASEURL) self.timeout = e.get('SELENIUM_TIMEOUT', TIMEOUT) self.selenium = selenium(self.rcHost, self.rcPort, self.environment, self.baseURL) self.selenium.start() def assert_equal_text_element(self, t1, v): self.wait_for_element_present(t1) try: self.assertEqual(self.selenium.get_text(t1), v) except AssertionError, e: self.verificationErrors.append(str(e)) def wait_for_text_present(self, text): for i in range(self.timeout/1000): try: if self.selenium.is_text_present(text): break except: pass time.sleep(1) else: self.fail("Time out waiting for text '%s'" % text) def wait_for_element_present(self, element_name): for i in range(self.timeout/1000): try: if self.selenium.is_element_present(element_name): break except: pass time.sleep(1) else: self.fail("Time out waiting for element %s" % element_name) def go_top(self): sel = self.selenium sel.open('/') sel.wait_for_page_to_load(self.timeout) sel.click("//div[@id='tabhead']/div[1]/a/img") sel.wait_for_page_to_load(self.timeout) def click_link(self, link): sel = self.selenium self.wait_for_text_present(link) sel.click("link=%s" % link) sel.wait_for_page_to_load(self.timeout) def click_buttonn(self, name): sel = self.selenium sel.click("//input[@value='%s']" % name) sel.wait_for_page_to_load(self.timeout) def login(self, username, password): sel = self.selenium self.go_top() self.click_link('Login') sel.type("id_username", username) sel.type("id_password", password) sel.click(u"//input[@value='Log in »']") sel.wait_for_page_to_load(self.timeout) def logout(self): self.click_link("Logout") def tearDown(self): raw_input("Press Enter to continue...") self.selenium.stop() self.assertEqual([], self.verificationErrors) class us1(SeleniumTester): def test_100(self): print 'login' self.login("root","root") self.click_link("GO GO TEST") self.wait_for_text_present("Android") self.go_top() name = 'WOW WOW' name_element = "//div[@id='content']/table/tbody/tr[td[div[span[text()='{name}']]]]".format(name=name) name_index = self.selenium.get_element_index(name_element) + 1 price_element = "//div[@id='content']/table/tbody/tr[{name_index}]/td[1]/div[@class='app_list']/span[@class='app_price']".format(name_index=name_index) price = "$1000.00" self.assert_equal_text_element(price_element, price) self.logout() if __name__ == "__main__": suite = unittest.TestLoader().loadTestsFromTestCase(us1) unittest.TextTestRunner(verbosity=0).run(suite)