Selenium Nose TypeError:__ init __()只取1个参数(给定2个)

时间:2016-02-17 09:35:47

标签: python-2.7 selenium selenium-webdriver nose

我在Python Selenium Webdriver中编写了一些自动化测试。我正在阅读Selenose(http://shiningpanda.com/introducing-selenose.html),因为我想尝试使用它来测试我的测试用例使用相同的1个Web浏览器实例,而不是为每个测试用例打开一个新的浏览器。

运行我的测试套件时收到错误:

TypeError: __init__() takes exactly 1 argument (2 given)

完整错误跟踪是:

    Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 5.0.3\helpers\pycharm\utrunner.py", line 121, in <module>
    modules = [loadSource(a[0])]
  File "C:\Program Files\JetBrains\PyCharm Community Edition 5.0.3\helpers\pycharm\utrunner.py", line 41, in loadSource
    module = imp.load_source(moduleName, fileName)
  File "C:\Webdriver\Selenose\ClearCore\TestSuite\suite2.py", line 98, in <module>
    mySuit=suite()
  File "C:\Webdriver\Selenose\ClearCore\TestSuite\suite2.py", line 84, in suite
    test_suite.addTest(unittest.makeSuite(TestCases.AdministrationPage_TestCase.AdministrationPage_TestCase))
  File "C:\Python27\lib\unittest\loader.py", line 312, in makeSuite
    return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
  File "C:\Python27\lib\unittest\loader.py", line 56, in loadTestsFromTestCase
    loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
TypeError: __init__() takes exactly 1 argument (2 given)

我在TestCases文件夹中有nose.cfg,它的配置是:

[nosetests]
with-xunit = true
with-selenium-driver = true
selenium-driver = sample

[selenium-driver:sample]
webdriver = ie

我的TestSuite是(它位于名为TestSuite的文件夹中:

import sys
import unittest
from HTMLTestRunner2 import HTMLTestRunner
from Utilities.HelperMethods import get_datetime_now
import os
import unittest
import TestCases.AdministrationPage_TestCase
import TestCases.DataMapsPage_TestCase
import TestCases.DataObjectsPage_TestCase
# Some more imports ...

class Test_Suite(unittest.TestCase):

    def test_main(self):


def suite():
    """
        Gather all the tests from this module in a test suite.
    """
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(TestCases.AdministrationPage_TestCase.AdministrationPage_TestCase))
    test_suite.addTest(unittest.makeSuite(TestCases.DataMapsPage_TestCase.DatamapsPage_TestCase))
    test_suite.addTest(unittest.makeSuite(TestCases.DataObjectsPage_TestCase.DataObjectsPage_TestCase))
    # --- some more test cases
    return test_suite

mySuit=suite()

# output to a file
test_result_dir = os.path.join(r"C:\\Webdriver\\ClearCore\\TestReport\\") # Test report goes here

# open the report file
outfile = file(test_result_dir + "\TestReport_" + get_datetime_now() + ".html", "wb")

# configure HTMLTestRunner options
runner = HTMLTestRunner.HTMLTestRunner(stream=outfile,
                                       title='Test Report',
                                       description='Selenium - LA Demo create a basic project automated test')

# run the suite using HTMLTestRunner
runner.run(mySuit)


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

我的AdministrationPage测试用例片段(它位于名为TestCases的文件夹中):

import nose
from selenose.cases import SeleniumTestCase
# some more imports...

class AdministrationPage_TestCase(SeleniumTestCase):

    def __init__(self):
        self.driver.get(Globals.URL_riaz_pc)
        self.login_page = login.LoginPage(self.driver)
        self.driver.implicitly_wait(120)
        self.driver.maximize_window()

    def test_add_Project(self):
        print "*** test_add_project ***"
        project_name = Globals.project_name + get_datetime_now()
        print "project_name_value = " + project_name
        write_project_Name_toFile2(project_name)
        self.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
        # more code ...

我删除了我的基本测试用例,该测试用例具有下面的setUpClass,代码段:

@classmethod
def setUpClass(cls):
    cls.driver = webdriver.Ie(Globals.IEdriver_path)
    cls.driver.get(Globals.URL_riaz_pc)
    cls.login_page = login.LoginPage(cls.driver)

我移动代码以从setUpClass浏览到URL login_page到AdministrationPage测试用例中的init方法。

我的设置不正确吗?我该如何解决这个问题,它只打开一个浏览器实例并运行测试用例?

------------我修改过的测试套件代码和AdministrationPage_TestCase -----

测试套件:

class Test_Suite(unittest.TestCase):

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCases.AdministrationPage_TestCase.AdministrationPage_TestCase))
suite.addTest(unittest.makeSuite(TestCases.DataMapsPage_TestCase.DatamapsPage_TestCase))
dateTimeStamp = time.strftime('%Y%m%d_%H_%M_%S')
buf = file(r"C:\\Webdriver\\ClearCore\\TestReport\\\TestReport_" + get_datetime_now() + ".html", "wb")
runner = HTMLTestRunner.HTMLTestRunner(
        stream=buf,
        title='Test the Report',
        description='Result of tests'
        )
runner.run(suite)

AdministrationPage_TestCase

from selenose.cases import SeleniumTestCase

class AdministrationPage_TestCase(SeleniumTestCase):

project_name_from_file = "" 

def test_a1_browse_to_url_and_login(self):
#def __init__(self):
    self.driver.get(Globals.URL_riaz_pc)
    self.login_page = login.LoginPage(self.driver)
    self.driver.implicitly_wait(120)
    self.driver.maximize_window()

def test_a2_add_Project(self):
    print "*** test_add_project ***"
    project_name = Globals.project_name_regression + get_datetime_now()
    print "project_name_value = " + project_name
    write_project_Name_toFile2(project_name)
    AdministrationPage_TestCase.project_name_from_file = project_name
    self.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
    menu_bar = MenuBarPage(self.driver)
    administration_page = menu_bar.select_menuBar_item("Administration")
    administration_page.click_add_project_button()
    time.sleep(15)

0 个答案:

没有答案