简体   繁体   中英

Trouble getting python selenium tests to run

I am creating an automation framework from scratch using selenium with python and would really like some input and advice on the best way to do so.

So far I have the following but my test script will not run. Please help!

script.py

from selenium import webdriver


class WebDriver(object):

  def __init__(self, driver=None):
    """
    __init__ setup webdriver test script class
    """
    self.driver = driver


  def setup(self):
    self.driver = webdriver.Chrome()

  def teardown(self):
    self.driver.quit()

test.py

import script

class Test(script.WebDriver):

  def search(self):
    self.driver.get("www.google.com")
    self.driver.find_element_by_id("lst-ib").clear()

This simple example should get you started:

from selenium import webdriver
import unittest

class WebDriverTestCase(unittest.TestCase):

  def setUp(self):
    self.driver = webdriver.Chrome()

  def tearDown(self):
    self.driver.quit()


class MyTests(WebDriverTestCase):

  def test_search(self):
    self.driver.get("https://www.google.com")
    self.driver.find_element_by_id("lst-ib").clear()


if __name__ == '__main__':
    unittest.main()

I'm assuming you are trying to run this with python unittest . If so, your class should inherit from unittest.TestCase in order to mark that it contains test cases:

class WebDriver(unittest.TestCase)

...

class Test(script.WebDriver)

And second missing piece is "boiler plate code to run the test suite" (see explanation here ) in test.py :

if __name__ == "__main__":
   test.main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM