Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新了README.md #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$ pip install -r requirements.txt
```

注:安装```requirements.txt```指定依赖库的版本,这是经过测试的,有时候新的版本可会有错。
注:安装指定依赖库的版本,这是经过测试的,有时候新的版本可会有错。

### 配置

Expand All @@ -39,24 +39,25 @@ class RunConfig:

### 运行

**不支持在编辑器(pycharm/ VS code ...)中运行,请在 cmd(windows)/终端(Linux)下执行。**
**可以在编辑器(pycharm/ VS code ...)中运行,推荐在 cmd(windows)/终端(Linux)下执行。**

```shell
$ python run_tests.py (回归模式,生成HTML报告)
$ python run_tests.py -m debug (调试模式)
$ 待补充调试模式的作用和区别
```

### 警告提示

`conftest.py` 文件有一行在 pycharm 中提示错误:
`conftest.py` 文件有一行在 vscode 中提示错误:

```python
from py.xml import html
```

* 原因
## 原因 ##

从源代码判断,py名称空间中的属性是动态创建的,这就是为什么静态分析工具pylint (pycharm)无法识别它们的原因:
从源代码判断,py名称空间中的属性是动态创建的,这就是为什么静态分析工具pylint (vscode)无法识别它们的原因:

```py
apipkg.initpkg(__name__, attr={'_apipkg': apipkg}, exportdefs={
Expand All @@ -73,4 +74,8 @@ apipkg.initpkg(__name__, attr={'_apipkg': apipkg}, exportdefs={
})
```

所以,是pycharm 不够智能!你可以忽略这个错误。
所以,是vscode 不够智能!你可以忽略这个错误。

但是,时迁尝试在vscode解决了这个问题:pip3 install py

或许只是py库老版本没有兼容呢?
8 changes: 5 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os
PRO_PATH = os.path.dirname(os.path.abspath(__file__))


#配置文件or参数需要放到一个class类里的属性
class RunConfig:
"""
运行测试配置
"""
# 运行测试用例的目录或文件
# 运行测试用例的目录文件
cases_path = os.path.join(PRO_PATH, "test_dir", "test_baidu.py")
# 运行测试用例的目录文件夹下的所有测试用例
cases_path = os.path.join(PRO_PATH, "test_dir")

# 配置浏览器驱动类型(chrome/firefox/chrome-headless/firefox-headless)。
# 配置浏览器驱动类型(chrome/chrome-headless/grid)。
driver_type = "chrome"

# 失败重跑次数
Expand Down
36 changes: 11 additions & 25 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
from selenium import webdriver
from selenium.webdriver import Remote
from selenium.webdriver.chrome.options import Options as CH_Options
from selenium.webdriver.firefox.options import Options as FF_Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from config import RunConfig

# 项目目录配置
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
REPORT_DIR = BASE_DIR + "/test_report/"
REPORT_DIR = BASE_DIR + "/测试报告/"


# 设置用例描述表头
def pytest_html_results_table_header(cells):
cells.insert(2, html.th('Description'))
cells.insert(2, html.th('用例描述'))
cells.pop()


Expand Down Expand Up @@ -90,11 +87,13 @@ def capture_screenshots(case_name):
"""
global driver
file_name = case_name.split("/")[-1]
if RunConfig.NEW_REPORT is None:
raise NameError('没有初始化测试报告目录')
else:
image_dir = os.path.join(RunConfig.NEW_REPORT, "image", file_name)
RunConfig.driver.save_screenshot(image_dir)
#if RunConfig.NEW_REPORT is None:
# raise NameError('没有初始化测试报告目录!')
#else:
# image_dir = os.path.join(RunConfig.NEW_REPORT, "image", file_name)
# RunConfig.driver.save_screenshot(image_dir)
image_dir = os.path.join(RunConfig.NEW_REPORT, "image", file_name)
RunConfig.driver.save_screenshot(image_dir)


# 启动浏览器
Expand All @@ -111,13 +110,8 @@ def browser():
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.maximize_window()

elif RunConfig.driver_type == "firefox":
# 本地firefox浏览器
driver = webdriver.Firefox()
driver.maximize_window()

elif RunConfig.driver_type == "chrome-headless":
# chrome headless模式
# chrome headless模式(无头模式),不打开浏览器界面,执行的UI自动化
chrome_options = CH_Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--disable-gpu')
Expand All @@ -126,14 +120,6 @@ def browser():
options=chrome_options,
service=ChromeService(ChromeDriverManager().install()))

elif RunConfig.driver_type == "firefox-headless":
# firefox headless模式
firefox_options = FF_Options()
firefox_options.headless = True
driver = webdriver.Firefox(
firefox_options=firefox_options,
service=FirefoxService(GeckoDriverManager().install()))

elif RunConfig.driver_type == "grid":
# 通过远程节点运行
driver = Remote(command_executor='http://localhost:4444/wd/hub',
Expand All @@ -159,4 +145,4 @@ def browser_close():


if __name__ == "__main__":
capture_screenshots("test_dir/test_baidu_search.test_search_python.png")
capture_screenshots("test_dir/test_baidu.py")
4 changes: 2 additions & 2 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def init_env(new_report):
初始化测试报告目录
"""
os.mkdir(new_report)
os.mkdir(new_report + "/image")
os.mkdir(new_report + "/失败截图")


@click.command()
Expand All @@ -35,7 +35,7 @@ def run(m):
now_time = time.strftime("%Y_%m_%d_%H_%M_%S")
RunConfig.NEW_REPORT = os.path.join(REPORT_DIR, now_time)
init_env(RunConfig.NEW_REPORT)
html_report = os.path.join(RunConfig.NEW_REPORT, "report.html")
html_report = os.path.join(RunConfig.NEW_REPORT, "测试报告可视化网页.html")
xml_report = os.path.join(RunConfig.NEW_REPORT, "junit-xml.xml")
pytest.main(["-s", "-v", RunConfig.cases_path,
"--html=" + html_report,
Expand Down
52 changes: 52 additions & 0 deletions test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#目的:构建一个能自主更新浏览器驱动的框架(只使用谷歌浏览器)

#导入对应的包
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as CH_options
from time import sleep
from tools.SliderVerificationTool import SliderTool

#参数实例化浏览器驱动
#options参数设置
chrome_options = CH_options()
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])#去除 “Chrome正受到自动化测试软件的控制”
#chrome_options.add_argument("--headless")
#chrome_options.add_argument('--disable-gpu')
# chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options,service=ChromeService(ChromeDriverManager().install()))
#driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.implicitly_wait(30)

#打开网页
driver.get("http://pos.meituan.com/web/rms-account#/login")
#切换iframe
frame_element = driver.find_element(By.CSS_SELECTOR,"#__root_wrapper_ > div > div.introduce-bg-3TjSR > div > div.ant-col.login-section-36Fr9 > iframe")
driver.switch_to.frame(frame_element)
#点击账号登录
driver.find_element(By.CSS_SELECTOR,"#app > div > div.header > a:nth-child(2)").click()
#输入账号
driver.find_element(By.CSS_SELECTOR,"#account").send_keys(17808231403)
#输入密码并回车登录
driver.find_element(By.CSS_SELECTOR,"#password").send_keys("meituan123",Keys.ENTER)

sleep(3)
slider = driver.find_element(By.CSS_SELECTOR,"#yodaBox")
background = driver.find_element(By.CSS_SELECTOR,"#yodaBoxWrapper > label")

SliderTool(driver,slider,background)

sleep(2000)


"""
待补充:
1、浏览器滑块验证已支持,但存在一个接口校验,导致自动化登录 无法完成,需优化
优化方案:
1、使用曾经用过的面具版selenium绕过异常检测
2、定制化针对该接口识别的参数处理(初步尝试失败)
"""
Binary file not shown.
172 changes: 0 additions & 172 deletions test_report/2020_07_19_23_01_06/junit-xml.xml

This file was deleted.

Loading