1

Is there an easy way to do this in Python? I have a pytest funcarg defined like so;

def pytest_funcarg__selenium(request):
    sel = selenium('10.3.10.154', 5555, '*firefox', 'http://10.3.10.180:8000')
    sel.start()

    return sel

Which obviously won't work, as I have no means of changing the specified browser in my tests. I mean, I could wrap a dummy class around selenium in this funcarg that loops through a list of browsers, but if a test fails, then it will fail for all browsers, which of course isn't very practical for finding failing browsers.

I also played with the idea of using a decorator on the test function, which would redefine the function once in the module for each browser under a different name, but same result - the funcarg has no way of knowing which browser to use.

3 Answers3

0

One technique I've seen used is to organize the tests in a class and use subclassing to switch class variables indicating which browser to test:

class TestFirefox:
    browser = '*firefox'

    def pytest_funcarg__selenium(self, request):
        sel = selenium('10.3.10.154', 5555, self.browser, 'http://10.3.10.180:8000')
        sel.start()
        return sel 

class TestChrome(TestFirefox):
    browser = '*opera'

Besides maximizing code reuse, this technique also lets you override and custom selected tests for various browsers.

In py.test, I believe you can use generative tests to the same effect.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • This is looking like the best solution so far. Also, I didn't know you could define funcargs as class level methods, which is cool. –  Nov 25 '11 at 07:07
0

By using selenium Grid you can run single test in multiple browsers. Is it what you intended to?

Karthi
  • 479
  • 2
  • 9
  • 21
  • What do I specify in the browser string for this to work, then? –  Nov 25 '11 at 07:06
  • W.r.t selenium grid, in testng.xml file specify the browser string as –  Nov 26 '11 at 00:39
  • I fail to see how that's going to automatically run one test in every browser. There's no browser string for 'all nodes'. –  Nov 28 '11 at 02:46
0

Use an environment variable to supply the browser string, and default it to something useful (e.g., *firefox) if not supplied. Then you can simply set the value before running the test class from the command line.

Ross Patterson
  • 9,527
  • 33
  • 48
  • This would be a great solution, except that our automated test system is a little odd, so something like this would be quite difficult. With a better testing system, I'd like this approach. –  Nov 27 '11 at 06:48