python软件测试方法_如何使用Python Unittest定义测试方法

1586010002-jmsa.png

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值