Home > Software engineering >  passing command line arguments to selenium python test case
passing command line arguments to selenium python test case

Time:02-07

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 ?

CodePudding user response:

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/"
  •  Tags:  
  • Related