乙醚 large avatar
测试学习小组  ›  python selenium

脚本执行没问题,但有报错ResourceWarning: unclosed file <_io.TextIOWrapper name='login_data.csv' mode='r' encoding='utf8'> testMethod()

By 乙醚 at 9 个月前 , 4207 次浏览
#!user/bin/python3
#coding:utf8


from Pages.login_page import LoginPage
from setting.driver_setting import TheDriver
from selenium import webdriver
import unittest
import csv
import time

class LoginCase(unittest.TestCase):

def setUp(self):
self.driver=TheDriver().the_driver()
# self.driver = webdriver.Chrome()
pass

def assert_page_url(self,url):
print('断言页面跳转')
try:
self.assertTrue(url in self.driver.current_url)
except AssertionError as msg:
print('没有返回首页地址',msg)
else:
print('登陆成功后返回首页地址')

def test_login_success_company(self):
data = csv.reader(open('login_data.csv','r',encoding='utf8'))

for login_datas in data:
username = login_datas[0]
password = login_datas[1]
loginpage = LoginPage(self.driver,'/view/login.shtml')
homepage = loginpage.login(username,password)

user_name_text = homepage.login_user_name().text
self.assert_page_url('view/index.shtml')
try:
print('公司用户名:',user_name_text)
self.assertEqual(u'天航科技',user_name_text)
except AssertionError as msg:
print(u'公司账号登陆失败',msg)
else:
print(u'公司账号登陆成功')
# # homepage.login_out()
# time.sleep(5)

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

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


'''
执行结果如下:
打开浏览器:谷歌
断言页面跳转
登陆成功后返回首页地址
公司用户名: 天航科技
公司账号登陆成功
```
F:\Program Files\Python36\lib\unittest\case.py:605: ResourceWarning: unclosed file <_io.TextIOWrapper name='login_data.csv' mode='r' encoding='utf8'>
testMethod()

Ran 1 test in 10.183s

OK
sys:1: ResourceWarning: unclosed <socket.socket [closed] fd=464, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>

Process finished with exit code 0
```

报以下信息 看不明白
```
F:\Program Files\Python36\lib\unittest\case.py:605: ResourceWarning: unclosed file <_io.TextIOWrapper name='login_data.csv' mode='r' encoding='utf8'>
testMethod()

sys:1: ResourceWarning: unclosed <socket.socket [closed] fd=464, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>
```

4 回复
乙醚 medium avatar
#1 - 9 个月前
乙醚

每次发完贴 自己就把问题解决了 加了一个文件关闭的方法 就解决了

小倔强 medium avatar
#2 - 5 个月前
小倔强

我也遇到了这个问题....乙醚兄,怎么加文件关闭的方法呀

虫师 medium avatar
#3 - 4 个月前
虫师

问题帮你做了修改,请学学markdown 再提问。

参考代码:

 with codecs.open('baidu_data.csv', 'r', 'utf_8_sig') as f:
data = csv.reader(f)
for line in islice(data, 1, None):
print(line)

sumacjg medium avatar
#4 - 3 个月前
sumacjg

如果是.txt文件,怎么解决啊,求助

测试学习小组 — 软件测试学习讨论区
现在注册
已注册用户请 登入
内容分享

欢迎关注我们的公众号: 测试圈TC


小贴士

你的问题在测试教程网中能找到答案