python写个脚本将rf report.html 中每个用例的过程提取出来
时间: 2024-03-17 13:47:00 浏览: 28
可以使用Python的BeautifulSoup库和正则表达式来完成这个任务。具体步骤如下:
1. 使用BeautifulSoup库读取report.html文件,将其转换为BeautifulSoup对象。
```python
from bs4 import BeautifulSoup
with open('report.html', 'r') as f:
soup = BeautifulSoup(f, 'html.parser')
```
2. 找到所有的测试用例标签,并遍历每个标签。
```python
testcases = soup.find_all('div', {'class': 'testcase'})
for testcase in testcases:
# 提取用例名称和过程
```
3. 对于每个测试用例标签,使用正则表达式提取用例名称和过程。
```python
import re
name = testcase.find('div', {'class': 'testcasename'}).text.strip()
steps = []
for step in testcase.find_all('li'):
step_text = step.text.strip()
if re.match(r'^\d+\.\d+\.\d+\s', step_text):
step_text = re.sub(r'^\d+\.\d+\.\d+\s', '', step_text)
steps.append(step_text)
```
4. 将用例名称和过程保存到文件中或进行其他处理。
```python
with open('output.txt', 'w') as f:
f.write('Test Case: {}\n'.format(name))
f.write('Steps:\n')
for step in steps:
f.write('- {}\n'.format(step))
```
完整代码如下:
```python
from bs4 import BeautifulSoup
import re
with open('report.html', 'r') as f:
soup = BeautifulSoup(f, 'html.parser')
testcases = soup.find_all('div', {'class': 'testcase'})
for testcase in testcases:
name = testcase.find('div', {'class': 'testcasename'}).text.strip()
steps = []
for step in testcase.find_all('li'):
step_text = step.text.strip()
if re.match(r'^\d+\.\d+\.\d+\s', step_text):
step_text = re.sub(r'^\d+\.\d+\.\d+\s', '', step_text)
steps.append(step_text)
with open('output.txt', 'a') as f:
f.write('Test Case: {}\n'.format(name))
f.write('Steps:\n')
for step in steps:
f.write('- {}\n'.format(step))
```