The python data driving operation Excel + ddt (Method B)

A, Mail163 the following data:

 

 

Two, Excel + ddt code is as follows:

to xlrd Import 
Import the unittest
from the webdriver Selenium Import
from selenium.webdriver.common.by By Import
from Import DDT DDT, Data, the unpack
Import Time

# installation: pip install xlrd
data stored in the read list # Table
DEF readExcels ():
Table xlrd.open_workbook = ( 'Mail163.xlsx', 'R & lt')
Sheet = table.sheet_by_index (0)
nrow = [] # define empty list
for row in range (1, sheet.nrows ): # 1-N from the read line taken through the data
# sheet.row_values (1,0,2) to the data read from the list of columns 0-2
nrow.append (sheet.row_values (row, start_colx = 0, = end_colx sheet.ncols))
return nrow
# Print ( readExcels ())
# [[ '', '', 'Please enter the account number'], [ 'admin', '', ' Please input password'], [ '', ' admin', ' Please enter the account number'], [ '^^^^', '', 'account malformed']]

@ddt
class Mail_163(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)
self.driver.get("https://mail.163.com/")

def tearDown(self) -> None:
self.driver.quit()

def login_163(self,username,password):
#验证登录163邮箱N中情况
self.driver.find_element(By.ID,"switchAccountLogin").click()
iframe = self.driver.find_element(By.TAG_NAME,'iframe')
self.driver.switch_to_frame(iframe)
self.driver.find_element(By.NAME,'email').send_keys(username)
self.driver.find_element(By.NAME,'password').send_keys(password)
the time.sleep (. 1)
self.driver.find_element (By.ID, "doLogin") the Click ().

DEF Assert_Text (Self):
# asserted: text asserts
the try:
divtext = self.driver.find_element (By.CSS_SELECTOR, ' . div.ferrorhead ') text
return divtext
the except Exception AS MSG:
Print ( "assertion failures} {" the format (MSG.))
self.driver.switch_to_default_content ()

@data (* readExcels ())
@unpack
DEF test_username_password_null (Self, username, password, Result):
'' 'verify: Log mailbox 163 in the case where the error message prompts N' ''
self.login_163 (username, password)
self.assertEqual (self.Assert_Text (),result)

# def test_username_null(self):
# '' 'Authentication: user name blank password is not empty error message prompts' ''
# self.login_163 (readusername (2), readpassword (2))
# self.assertEqual (self.Assert_Text (), ReadResult (2 ))
#
# DEF test_passwd_null (Self):
# '' 'Authentication: user name is not empty blank password prompt an error message' ''
# self.login_163 (readusername (3), readpassword (3))
# self.assertEqual (self.Assert_Text (), ReadResult (3))
#
# DEF test_username_input_format (Self):
# '' 'verify: enter the username illegal character error message prompts' ''
# self.login_163 (readusername (4), readpassword ( . 4))
# self.assertEqual (self.Assert_Text (), ReadResult (. 4))


IF the __name__ == '__main__':
the unittest.main (verbosity = 2) # log details

Guess you like

Origin www.cnblogs.com/Teachertao/p/11707768.html