0

I am using Selenium 2 with Webdriver. I would like to pass in my desired environment as part of a set of test data. My code looks like this:

    capability = webdriver.DesiredCapabilities.FIREFOX

And works correctly. But I would like to pass the "FIREFOX" in from a variable, kind of like this:

    TestParameters['Environment']="FIREFOX"
    capability = webdriver.DesiredCapabilities.TestParameters['Environment']

But I get this error

AttributeError: type object 'DesiredCapabilities' has no attribute 'TestParameters'

What can I do to evaluate the variable contents as a method name?

Skip Huffman
  • 5,239
  • 11
  • 41
  • 51
  • possible duplicate of [Call class method from variable in Python](http://stackoverflow.com/questions/1855558/call-class-method-from-variable-in-python) – jtbandes Mar 13 '12 at 13:07
  • Actually... this one is a closer dupe: http://stackoverflow.com/questions/3061/calling-a-function-from-a-string-with-the-functions-name-in-python – jtbandes Mar 13 '12 at 13:07

1 Answers1

1

Your code tries to access the attribute by the variable name instead of the variable value:

capability = webdriver.DesiredCapabilities.TestParameters['Environment']

As the error message says, this evaluates the TestParameters attribute of webdriver.DesiredCapabilites. Which it doesn't have.

What you want is to evaluate the attribute whose name is the value of TestParameters['Environment']:

capabilityAttributeName = TestParameters['Environment']
capability = getattr(webdriver.DesiredCapabilities, capabilityAttributeName)
Deestan
  • 16,738
  • 4
  • 32
  • 48