Python webdriver:driver.get(URL)无法在unittest中打开 - 通过控制台工作

时间:2011-11-08 00:54:48

标签: python selenium webdriver

正如标题所说,我有一份Selenium   - 从svn repo

安装

当我通过控制台导入和运行时,一切正常 当我尝试运行单元测试时,即使是直接从Selenium IDE导出的单元测试

它将打开浏览器并挂起而不尝试打开URL

其他详细信息

  • 操作系统:Ubuntu 11.04
  • Python:2.7
  • 浏览器:Firefox 7.0.1
    • 尝试使用HTTPUNIT虽然我一直收到非法字符错误

代码

import unittest
#import time
import re

from selenium import webdriver
#from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

#from local_config import BASE_URL, BASE_FOLDER, DRIVER
BASE_URL = 'http://localhost'
BASE_FOLDER = '/dir/to/site/'
DRIVER = 'FIREFOX'


class CheckSitemap(unittest.TestCase):

    def setUp(self):
        global BASE_URL
        global DRIVER
        if DRIVER == 'FIREFOX':
            self.driver = webdriver.Firefox()
        elif DRIVER == 'HTTPUNIT':
            self.driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
        else:
            raise Exception('DRIVER not set')
        self.driver.implicitly_wait(30)
        self.base_url = BASE_URL
#        print self.base_url
        self.verificationErrors = []

    def getSitemap(self):
        global BASE_FOLDER
        global DRIVER
#        print BASE_FOLDER
        driver = self.driver
        op1 = '%ssitemap.xml' % BASE_FOLDER
        if DRIVER == 'HTTPUNIT':
            op1 = '%s%s' % (self.base_url, op1[1:len(op1)])
#        print op1
        driver.get(op1.replace('//', '/'))
#        print 'HELLO WORLD!!'
        urls = []
#        print driver.page_source
        r = re.compile(r'<a href="(.*?)"')
        for match in r.finditer(driver.page_source):
            urls.append(match.group(1))
        if len(urls) == 0:
            r = re.compile(r'<loc>(.*?)</loc>', re.DOTALL)
            for match in r.finditer(driver.page_source):
                urls.append(match.group(1).strip())
        return urls

    def test_check_sitemap(self):
        global DRIVER
        driver = self.driver
        urls = self.getSitemap()
#        print urls
        for url in urls:
            if DRIVER == 'HTTPUNIT':
                driver.get(url)
            else:
                driver.get(url.replace(self.base_url, ''))

            if re.search(r"\.php on line [0-9]+", driver.page_source):
                self.verificationErrors.append('php error on %s' % url)

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException:
            return False
        return True

    def tearDown(self):
        self.driver.quit()
#        print self.verificationErrors
        self.assertEqual([], self.verificationErrors)

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

任何想法?

1 个答案:

答案 0 :(得分:2)

通过与IRC中的AutomatedTester聊天找到我的解决方案

目前python webdriver实现忽略了self.base_url值

所以当你.get()一个URL时,你需要使用完整的URL

已提交错误报告,并可能在不久的将来更新

Webdriver版本2.11.1