开发者

How to input parameters into Selenium RC TestSuite?

I have to test few sites which sales the same things, but they have an another template.

So I want to run each MainTestClass with giving some input parameter, let's say :

java -jar SeleniumServerStandalone-2.0b2.jar -port 5555 (template_id=5)

Is it possible?

class MainTestCases(unittest.TestCase):
    def setUp(self):

        #self.template_id=template_id I want something like that
        self.verificationErrors = []

        self.selenium = selenium("localhost", 5555, "*chrome", "http://www.google.com/")
        time.sleep(5)
        self.selenium.start()


    def test_test1(self):
        if self.template_id==1:
        ...
        elif self.template_id==2:开发者_如何学Python
        ...
    def test_test2(self):
        if self.template_id==1:
        ...
        elif self.template_id==2:
        ...
    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

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


Try adding an init method to MainTestCases, like so:

class MainTestCases(unittest.TestCase):

    def __init__(self, methodName, template_id):
        super(MainTestCases, self).__init__(self, methodName)
        self.template_id = templateId

    def setUp(self):
        ... and so on...

Due to this customization, you will need to build your test suite manually because each test case has to be instantiated with the template_id, like so--

def suite(template_id):
    testcasenames = unittest.defaultTestLoader.loadTestsFromTestCase(MainTestCases)
    suite = []
    for case in testcasename:
        suite.append(MainTestCases(case, template_id)
    return suite

Then in main, instead of unittest.main(), do:

  1. Parse command-line arguments. You may want to consider the argparse (2.7+) or optparse (2.6 and earlier) modules. They are powerful, but easy to start with by looking at the examples.
  2. Create and run the suite: unittest.TextTestRunner().run(suite(template_id))


Now, I use this solution:

  1. Create suite test which runs testcases:

import unittest from Flights.FlightsTestCases import FlightsTestCases import sys from Flights.FlightTemplate import FlightTemplate

def suite():

    testSuite= unittest.TestSuite()
    testSuite.addTest(FlightsTestCases('test_test1'))


    FlightsTestCases.www_address='http://testpage.pl/'
    FlightsTestCases.flight_template=FlightTemplate.Test

    #FlightsTestCases.www_address='http://productionpage.pl/'
    #FlightsTestCases.flight_template=FlightTemplate.Production

    return testSuite


if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

change set_up to something like:

class FlightsTestCases(unittest.TestCase): www_address = None flight_template = None xml_report_generator = None

def setUp(self):
    self.verificationErrors = []
    if self.www_address == None:
        self.selenium = selenium("localhost", 5555, "*chrome", "http://testpage.pl/")
    else:
        self.selenium = selenium("localhost", 5555, "*chrome", self.www_address)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜