Reputation: 41
In the below code how can I call function open_url()
from outside the class or from another file in python ?
from selenium import selenium
import unittest, time, re
from selenium import webdriver
seed_url="http://www.google.com"
class Test(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "https://www.google.com/")
self.selenium.start()
def test_urlfunctions(self):
sel = self.selenium
sel = webdriver.Firefox()
def open_url(url):
sel.get(url)
open_url(seed_url)
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Upvotes: 0
Views: 3663
Reputation: 15306
In general, were this a "normal" class and a "normal" method, you could instantiate the object, and simply call it.
i.e.
foo = Test()
foo.open_url()
However, this class is not intended to be used that way, as it would only have a default constructor which, while it would create an instance of the object, would be essentially empty, although I suppose you could explicitly call the other functions yourself.
Rather, this class appears like it's meant to be used by a test framework (perhaps nose), which will automatically call the setUp
and tearDown
methods when appropriate, and then automatically run the test methods.
Further, this particular method, open_url
, if your indentation is correct, is scoped to the test_urlfunctions
function, and so you couldn't call it from outside the class, unless you were to go through some contortions such as having the test_urlfunctions
method return the function which could then be used elsewhere. However, that would defeat the point of having it scoped locally and having this setup to be used with a test framework.
For example, here's a smaller example of this. Given this code:
class Foo:
def bar(self):
print "in bar"
def inner_bar():
print "in inner_bar"
inner_bar()
if __name__ == "__main__":
foo = Foo()
foo.bar()
foo.inner_bar()
This results in the following output:
in bar in inner_bar Traceback (most recent call last): File "so_class.py", line 14, in <module> foo.inner_bar() AttributeError: Foo instance has no attribute 'inner_bar'
This shows that inner_bar()
can't be called via the object itself from the outside as-is.
What are you trying to accomplish? Why do you want to do this here?
Edit in response to comment from OP:
If you want to run Selenium outside of the context of a test framework, you could decouple it from that by not inheriting from unittest
and reshuffling the functions and how you call them (since they would no longer be automatically called by the test framework).
However, you say you want to make a web crawler with selenium webdriver test framework, which doesn't really make sense. Selenium was meant for automating web browsers, which is generally (although not always) used for testing. You may actually want to look into something like scrapy, which was specifically designed for screen-scraping and web crawling.
Upvotes: 1