[Original] selenium+python+openpyxl realizes login automation test, automatically reads excel use case data, and automatically writes data results to excel

1  # -*- coding: utf-8 -*- 
2  from selenium import webdriver
 3  from openpyxl import load_workbook
 4  from time import sleep
 5  class mylogintest():
 6      ''' Open Baidu Test ''' 
7  
8      def openBD(self ):
 9          ''' Open Baidu ''' 
10          print ( " begin " )
 11          book = load_workbook( " mylogintest.xlsx ")  #Read and write by default, if necessary, you can specify write_only and read_only as True 
12          b = book.active
 13          datadict = {} #Create a dictionary variable 
14          maxC = b.max_column #Get the maximum column 
15          maxR = b.max_row #Get Maximum line 
16          for i in range(1, maxC + 1): # The range starts from 0 by default and ends at -1 of the following parameter, and openpyxl starts from the first line and the first column, so the parameter is 1, maxC +1; It means to traverse the first column to the last column, 
17              datadict.setdefault(b.cell(1, i).value) # Put all the columns in the first row, that is, the 2 title as the key, into the dictionary datadict 
18              # print(datadict) 
19          for i inrange(2, maxR + 1): #Remove the title of the first row, traverse from the second row to the last row 
20              for j in range(1, maxC + 1): # Traverse from the first column to the last column 
21                  datadict[b .cell(1, j).value] = b.cell(i, j).value # Put the content of the second line as the value corresponding to the key into the dictionary datadict 
22              try :
 23                  self.driver = webdriver.Firefox( )
 24                  self.driver.get(datadict[ " url " ]) # Take out the corresponding URL value from the corresponding key, the key here is the title of the first row of the table, so the title of EXCEL should be written according to the regulations 
25                  self.driver .find_element_by_xpath( " /html/body/div[1]/div/div[1]/div[3]/a[1]/img" ).click()
 26                  if datadict[ " username " ]!= None:
 27                      username = self.driver.find_element_by_name( " username " ) .send_keys (datadict[ " username " ]) #When username is not empty, Put the value in the username textbox 28 if datadict[ " password " ] != None:
 29                      password = self.driver.find_element_by_name( " password " ).send_keys(datadict[ " password " ).])#
                 When the password is not empty, put the value into the password text box, to judge otherwise it will make an error 
30                  self.driver.find_element_by_id( " loginbtn " ).click() #Click to log in 
31                  if datadict[ " checkmeg " ]!= None: #When the check value is not empty 
32                      msg = self.driver.find_element_by_xpath( " /html/body/div[8]/div " ).text #Compare check value 
33                      if msg == datadict[ ' checkmeg ' ]: #If the check value comparison is successful, write the check value to the result 
34                          b.cell(i, maxC, msg)
 35                      else:
 36                          b.cell(i, maxC, ' failure ' ) #Otherwise it will fail and write to the result 
37                  else : #When the check value is not empty 38 # print(self.driver.switch_to.alert.text ) 39                      cur_url = self.driver.current_url #Determine the current url 40 if cur_url == datadict[ ' checkurl ' ] : #If the current url is the verified url 41                          b.cell(i, maxC, ' login successful ' )   #will test the login A successful result is written to the table 42 else :

                 

                     

                     43                          b.cell(i, maxC, ' login failure ' )   #Write the test login failure result to the table 
44                  sleep(1 )
 45                  self.driver.quit()
 46  
47              except :
 48                  b.cell(i, maxC , ' error ' ) #When there is an error in the program , write the error result to the table 49 50          book.save( " mylogintest.xlsx " ) # Remember to close 51 52 print ( " end " )
 53 54
 

 
          
mylogintest().openBD()

General idea:

Divide the login into two cases: one is the case of successful login, the other is the case of failed login, if the checkmeg of successful login is empty, compare the checkurl, if the login fails, compare the checkmeg directly, and then put The execution result is written into the table;

It is enough to directly judge whether checkmeg is empty in the script. According to whether checkmeg is empty, compare the two situations in different ways, and finally get the result;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325429786&siteId=291194637