开发者

Selenium, with Python, how to simplify scripts so that I can run them from other python scripts?

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()


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜