0
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class CorrecaoEfetivaNota(unittest.TestCase):
  def setUp(self):
  self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver')

def teste_login_avaliador(self):
    driver = self.driver
    driver.get("")
    cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]')
    cpf.send_keys("")
    password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]')
    password.send_keys("")
    login = driver.find_element_by_tag_name('button')
    login.click()
    driver.implicitly_wait(3)

def teste_buscar_mais_um(self):
    driver = self.driver
    buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
    buscar.click()

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

I'm trying to write this tests in Python, the first function is ok, but the second one inside the class is not being executed in the tests. How can I organize this?

2 Answers2

0

You write that the first function is ok, which I assume must be the setUp() function you are referring to (provided that in your code you indented correctly).

As Andersson comments, your unittest methods needs to begin with "test_" not "teste_". Providing "test_" is your way of telling unittest that this method should be tested.

In your unittest you probably also want to test something such as self.assertEqual(1,1) otherwise your tests will pass no matter what.

Next time please provide us with a more thorough description of what is wrong. How did you make the call to unittest? What error is python giving you? What result did you expect? Etc. It makes solving your problem much faster.

I encourage you to make a simple test first and ensure that it runs:

import unittest

class TestingUnittest(unittest.TestCase):
  def setUp(self):
    print("SetUp called")

  def tearDown(self):
    print("tearDown called")

  def test_Method(self):
    print("Testing 1==1")
    self.assertEqual(1,1)

Call this from your terminal:

>>>python -m unittest "name-of-test.py"
Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57
0

While working with Python and unittest module with Selenium you have to consider a few facts as follows :

  • Indentation for class and test_method are different.
  • Instead of driver.close() always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
  • If you are using unittest module you have to call the __main__.
  • Here is your own code with the required minor modifications which will execute the first method teste_login_avaliador() as well as the second method teste_buscar_mais_um() within the Class CorrecaoEfetivaNota():

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    class CorrecaoEfetivaNota(unittest.TestCase):
    
        def setUp(self):
            self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
    
        def teste_login_avaliador(self):
            driver = self.driver
            driver.get("http://d3dyod5mwyu6xk.cloudfront.net/")
            cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]')
            cpf.send_keys("27922797885")
            password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]')
            password.send_keys("enccejaregular")
            login = driver.find_element_by_tag_name('button')
            login.click()
            driver.implicitly_wait(3)
    
        def teste_buscar_mais_um(self):
            driver = self.driver
            buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
            buscar.click()
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == "__main__":
        unittest.main()
    
  • Note: Though both the test_methods are being called still you will face the following exception:

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button"}
    
  • At the line:

    buscar = driver.find_element_by_xpath("//section[1]/div/div/section[2]/div/div/div[1]/div/div[2]/button")
    

    This exception can be solved easily following the actual Test Steps of your usecase and if required you can raise a new question/ticket.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352