全局名称没有得到定义 - python

时间:2013-11-15 17:27:24

标签: python selenium

我正试图运行这个:

import unittest
import wd.parallel
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException
import copy
class Selenium2OnSauce(unittest.TestCase):
    def setUp(self):
        desired_capabilities = []

        browser = copy.copy(webdriver.DesiredCapabilities.FIREFOX)
        browser['version'] = '10'
        browser['platform'] = 'XP'
        browser['name'] = 'Python at Sauce'
        browser['tags'] = "Parallel"
        desired_capabilities += [browser]

        self.drivers = wd.parallel.Remote(
            desired_capabilities=desired_capabilities,
            command_executor="http://user-string:key-string@ondemand.saucelabs.com:80/wd/hub"
        )

    def ajax_complete(driver):
        try:
            return 0 == driver.execute_script("return JQuery.active")
        except WebDriverException:
            pass    

    @wd.parallel.multiply
    def test_sauce(self):
        self.driver.get('http://url.com')
        username = self.driver.find_element_by_id('id_username')
        print "Found Username"
        username.send_keys('i1@example.com')
        print "Username Entered"

        password = self.driver.find_element_by_id('id_password')
        print "Found Password"
        password.send_keys('testuser')        
        print "Password Entered"

        password.submit()
        print "Logged in"

        self.driver.find_elements_by_link_text('Notification')[0].click()
        print "Going to Notification"

        WebDriverWait(self.driver, 10).until(
            ajax_complete, "Timeout waiting for page to load"
        )

        print "ajax loaded string" in self.drive.page_source

    @wd.parallel.multiply
    def tearDown(self):
        print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
        self.driver.quit()
        print "Quit"

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

当我这样做时,我得到了这个:

Found Username
Entered Username
Found Password
Entered Password
Logged in
Going to Cal
Process Process-1:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "build/bdist.macosx-10.8-intel/egg/wd/parallel.py", line 88, in thread_func
    f(SubTest(driver))
  File "functionalTest.py", line 57, in test_sauce
    ajax_complete, "Timeout waiting for page to load"
NameError: global name 'ajax_complete' is not defined
Link to your job: https://saucelabs.com/jobs/06022dc1f54548d98a75f59bd211848d
Quit

我想我无法访问var call ajax_complete,但我不知道为什么!

在一天结束时,我想要做的就是等待所有的ajax完成,然后再进行下一次测试......

注意:我是一个蟒蛇新手,所以我很抱歉这个问题真的很傻。

1 个答案:

答案 0 :(得分:1)

Python中的方法可通过self访问:

WebDriverWait(self.driver, 10).until(
    self.ajax_complete, "Timeout waiting for page to load"
)

请注意,ajax_complete方法本身也需要将self作为第一个参数。