Reputation: 23
I am new to python, selenium. I got following error during execution. I've used Python 3.5.0
How can i use argument in this problem? Is there any alternative method?
Error:
C:\>python test.py
Enter your username: dg
Enter your password: bn
Address: rty
Traceback (most recent call last):
File "test.py", line 5, in <module>
class TryTestCase(unittest.TestCase):
File "test.py", line 24, in TryTestCase
test_something(username, password, target)
TypeError: test_something() missing 1 required positional argument: 'target'
Code:
import unittest
from selenium import webdriver
class TryTestCase(unittest.TestCase):
username = input('Enter your username: ')
password = input('Enter your password: ')
target = input('Address: ')
def setUpClass(self):
self.driver = webdriver.Firefox()
self.driver.get('https://abcd.com/')
self.driver.implicitly_wait(5)
def test_something(self, username, password, target):
self.driver.find_element_by_xpath("xyz").send_keys(username)
self.driver.find_element_by_xpath("xyz").send_keys(password)
self.driver.find_element_by_xpath("xyz").send_keys(target)
self.driver.implicitly_wait(1)
def tearDownClass(self):
self.driver.close()
test_something(username, password, target)
if __name__ == '__main__':
unittest.main()
Upvotes: 1
Views: 4152
Reputation: 4326
In Python classes a parameter is passed with the scope of this class. This is the self parameter you have in all your methods. Because these methods are in a class you can't just call them (unless they are static). If you want to call them you must first create an instance of your class. After that you can call the the method through this instance and the self parameter gets passed automatically for you.
As others have commented on your question there is no need to call this method yourself as unittest does this for you. That does not mean it isn't an important thing to know why you are getting this error. Therefore I am going to give you some basic examples.
class Boat:
def sail(self, distance):
print("I am sailing for {0} meters").format(distance)
sail(5)
This example will give you the error you are getting. This is because you are not calling the method through an instance of your class (you can't call a method from inside a class at all unless the call is made from another method).
If you make the call from another method inside this class it is possible by using the self scope.
class Boat:
def sail(self, distance):
print("I am sailing for {0} meters").format(distance)
def do_stuff(self):
self.sail(5)
Now you are calling this method through an instance of the class (self). Meaning it will work fine.
If you want to call the method from outside the class you first need to create an instance of the class.
class Boat:
def sail(self, distance):
print("I am sailing for {0} meters").format(distance)
boat = Boat()
boat.sail(5)
This will work fine because you have first made an instance of the Boat class. When calling the sail method this instance automatically gets passed as a parameter (self).
Because in your example it is not called through an instance of this class it is expecting the self parameter to be passed manually. This is why the error says it is missing 1 argument. The one that's missing is the self argument.
Upvotes: 2