views:

49

answers:

1

I'm having some trouble figuring out how to take out what is not necessary in a selenium strip and package it in such a way that I can call it from another script.. I am having trouble understanding what is going on with this, as I don't get where the unit testint parts are coming from... ideally if I could just separate this into a function that I could call that would be idea, thanks for any advice.

(AND, yes i do need selenium, I kindly ask that you please don't suggest alternatives as I am going to be using selenium for a lot of things so I need to figure this out)

This is just a basic demo script:

from selenium import selenium

import unittest



class TestGoogle(unittest.TestCase):

    def setUp(self):

        self.selenium = selenium("localhost", \

            4444, "*firefox", "http://www.bing.com")

        self.selenium.start()



    def test_google(self):

        sel = self.selenium

        sel.open("http://www.google.com/webhp")

        sel.type("q", "hello world")

        sel.click("btnG")

        sel.wait_for_page_to_load(5000)

        self.assertEqual("hello world - Google Search", sel.get_title())



    def tearDown(self):

        self.selenium.stop()



if __name__ == "__main__":

    unittest.main()
+1  A: 

What I would recommend is to make functions in your other script that have as an argument a reference to the test case. That way, your functions could fail the test case if something does not go right. Like so (to search google for a string and check the title):

def search_s(utest, in_str):
  s = utest.selenium
  s.type('q', in_str)
  s.click('btnG')
  s.wait_for_page_to_load('30000')
  utest.assertEqual("%s - Google Search" % (in_str,), s.get_title())

Then, in your test case, call it like this:

def test_google(self):
  s.open('/')
  search_s(self, "hello world")

You can then make libraries of these types of methods, allowing you to mix-and-match pieces of your tests.

hb2pencil
thanks.. yeah thats basically what I started doing after I found that other info... thanks for posting this
Rick