简体   繁体   中英

Python selenium using openpyxl to write result to excel file but there is only case is written to excel file

I just start learning about Selenium using Python last week, i write a test case with 4 step, at step 3 and 4 if this pass/fail then it will be write result to a excel file. But when i run the test case, there is only one step can write result to file excel, any help? Btw, sorry for my bad english :P

My scenario
Step 1: Enter username
Step 2: Enter password
Step 3: Click login button
Step 4: Wait for Gmail's logo visible

Here's my code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
from openpyxl import Workbook

class BasicTest(unittest.TestCase):

def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.get("https://www.gmail.com")
    self.LoginXpath = "//input[@value='Sign in']"
    self.LogoXpath = "//a[contains(@href, '#inbox')]"
    self.gmailUserName = "username"
    self.gmailPassword = "password"
    self.emailFieldID = "Email"
    self.passFieldID = "Passwd"
    self.InboxXpath = "id(':4b')/x:div/x:div[1]/x:span/x:a"

def test_Basic(self):
    driver = self.driver
    driver.find_element_by_id(self.emailFieldID).send_keys(self.gmailUserName)
    driver.find_element_by_id(self.passFieldID).send_keys(self.gmailPassword)


    try:
        driver.find_element_by_xpath(self.LoginXpath).click()
        self.write_test_case_result('PASS', 'A1')
        print('Find login button: PASS')
    except:
        self.write_test_case_result('FAIL', 'A1')
        print('Find login button: FAIL')

    try:
        WebDriverWait(driver, 15).until(lambda driver: self.InboxXpath)
        driver.find_element_by_xpath(self.LogoXpath).click()
        self.write_test_case_result('PASS', 'A2')
        print('Find logo xpath: PASS')
    except:
        self.write_test_case_result('FAIL', 'A2')
        print('Find logo xpath: FAIL')

def write_test_case_result(self, result, location):
    wb = Workbook()
    ws1 = wb.worksheets[0]
    ws1.title = 'Test result'
    dest_filename = 'Test_Result.xlsx'

    while True:
        if result == "PASS":
            ws1.cell(location).value = "PASSED"
            break
        else:
            ws1.cell(location).value = "FAILED"
            break
        break
    # Save the file
    wb.save(filename = dest_filename)


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

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

First, install openpyxl using a command -- "pip install openpyxl" and now create a new file python file and write below code--

from openpyxl import load_workbook

def userName(row_Num, cell_Num, sheet):
    wb = load_workbook('./test.xlsx')
    sheet = wb.get_sheet_by_name(sheet)
    username = sheet.cell(row=row_Num, column=cell_Num).value
    if username is not None:
        return username
    else:
        return '' #Null Value pass 

Now go where you created testScript and import python file, remember all file should be same folder including excellSheet.

 driver.find_element_by_xpath("//input[@id='user']").send_keys(userName(7,1,'Sheet1'))
 #Row num, Col Num, Sheet Name

The formatting is off but I think you should not be embedding what is essentially logging code within a test method. test_Basic will overwrite the file every time it is run, which is presumably why it only ever has one result.

Try writing your test initially just to use Python's logging module. Once this is working fine you can replace the logging object with some code that writes Excel files.

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