简体   繁体   English

Python Selenium使用openpyxl将结果写入excel文件,但是只有大小写写入excel文件

[英]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. 我上周刚开始使用Python学习Selenium,我编写了一个包含4个步骤的测试用例,在第3和第4步,如果通过/失败,那么它将结果写入excel文件。 But when i run the test case, there is only one step can write result to file excel, any help? 但是当我运行测试用例时,只有一步可以将结果写入excel文件,有帮助吗? Btw, sorry for my bad english :P 顺便说一句,对不起,我的英语不好:P

My scenario 我的情况
Step 1: Enter username 步骤1:输入使用者名称
Step 2: Enter password 步骤2:输入密码
Step 3: Click login button 步骤3:按一下[登入]按钮
Step 4: Wait for Gmail's logo visible 步骤4:等待Gmail徽标可见

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-- 首先,使用以下命令安装openpyxl:“ pip install openpyxl”,然后创建一个新的文件python文件并编写以下代码-

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. 现在转到创建testScript的位置并导入python文件,请记住所有文件应为同一文件夹,包括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. test_Basic每次运行时都会覆盖该文件,这大概就是为什么它只有一个结果的原因。

Try writing your test initially just to use Python's logging module. 最初尝试编写测试只是为了使用Python的日志记录模块。 Once this is working fine you can replace the logging object with some code that writes Excel files. 一旦工作正常,就可以用一些写入Excel文件的代码替换日志记录对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM