0

I have a selenium python automation test where i want to pass url of website as a parameter to function. I will be calling the function via command line.

my_python_code.py

class my_automation_class(unittest.TestCase)
    
    def setUp(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--disable-gpu")
        self.driver = webdriver.Chrome(ChromeDriverManager().install())
        parser = argparse.ArgumentParser()
        parser.add_argument("url_link", help="display url")
        self.args = parser.parse_args()

    def test_my_automation(self):
        
        driver = self.driver
        driver.maximize_window()

        driver.get(self.args.url_link)
        //performs some operation after this

if __name__ == "__main__":
unittest.main(testRunner=HTMLTestRunner(output='Reports'),exit=False) 

When I ran this via command line

python my_python_code.py "https://www.netflix.com/in/ "

I am getting

ERROR [0.000000s]: unittest.loader._FailedTest.https://www

AttributeError: module 'main' has no attribute 'https://www'

I am not sure why it's not working. Is there any other way i can pass arguments to selenium python test case ?

10
  • The parsing statements should be under the if name == 'main': statement or in the set-up function. Commented Feb 4, 2022 at 8:05
  • @DevangSanghani Thank you for your answer, i added it in the setup func, but i am still getting the same error. Commented Feb 7, 2022 at 4:23
  • Can you update the code here? Commented Feb 7, 2022 at 4:38
  • @DevangSanghani hey , I have updated the code Commented Feb 7, 2022 at 4:55
  • You should run it like this : python my_python_code.py --url_link="https://www.netflix.com/in/ " Commented Feb 7, 2022 at 5:03

1 Answer 1

1
class my_automation_class(unittest.TestCase)               
      def setUp(self):
          chrome_options = webdriver.ChromeOptions()
          chrome_options.add_argument("--headless")
          chrome_options.add_argument("--disable-gpu")
          self.driver = webdriver.Chrome(ChromeDriverManager().install())
          self.url_link = args.url_link
            
      def test_my_automation(self):
          driver = self.driver
          driver.maximize_window()
          driver.get(self.url_link)
          //performs some operation after this
                
if __name__ == "__main__":
     parser = argparse.ArgumentParser()
     parser.add_argument("url_link", help="display url")
     args = parser.parse_args()
     sys.argv.pop()            
     unittest.main(testRunner=HTMLTestRunner(output='Reports'),exit=False) 

Run code:

    python my_python_code  --url_link="https://www.netflix.com/in/"
Sign up to request clarification or add additional context in comments.

2 Comments

I have one more doubt , i want to run this using pytest and create a report of it in html, how does this work ? i tried "pytest -v -s --json-report --json-report-indent=4 --json-report-file=report/report.json --html=report/report.html conftest.py"
Pls try it and post a new question in case you need help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.