搭建自动化测试环境

自动化测试环境:
Python3.7+Selenium3.141+谷歌浏览器76.0/火狐浏览器

1、安装Python并配置环境变量。

  1. 下载并安装:https://www.python.org/downloads/
  2. 配置环境变量:C:Python37;C:Python37Scripts;

2、安装Pycharm开发工具。

下载地址:http://www.jetbrains.com/pycharm/download/#section=windows
注意下载:Community社区版

3、安装Selenium

安装方式一(在线安装):

  • 安装Seleinum:pip install -U selenium
  • 查看Seleinum:pip show selenium
  • 卸载Seleinum:pip uninstall selenium

安装方式二:
Selenium下载地址:https://pypi.org/project/selenium/

  • 安装python包,选择全部组件
  • 解压selenium-3.13.0.tar.gz,然后cmd进入解压目录
  • 使用命令 Python setup.py install 安装Selenium

4、安装浏览器:Chrome和Firefox的其中之一。

5、浏览器驱动:下载Chrome浏览器驱动或者是Firefox浏览器驱动。

注意版本需要和对应的浏览器兼容。
下载后解压将exe的文件放到python的目录下:如:D:Python37

6、配置webdriver

配置方式一:
1)把下载好的chromedriver.exe程序放置到python的安装路径下

2)把seleinum加入到pycharm的项目中。
Pycharm->File->Setting->Project:项目名->Project Interpreter->+->搜索selenium->install Package->等10秒

3)在python中代码编写如下即可:

from selenium import webdriver
#打开浏览器
driver = webdriver.Chrome()  #Firefox、le、Edge等

4)右击运行,能打开浏览器说明自动化测试环境搭建完成。

配置方式二:
1)把下载好的chromedriver.exe程序放置到python项目中(其它路径也可)
2)在python中代码编写如下即可:

chromePath = chromedriver.exe  #路径
os.environ[ 'webdriver.chrome.driver' ] = chromePath  #gecko 、ie 等
driver = webdriver.Chrome(executable_path=chromePath) #Firefox、Ie等

如下分别是实现打开谷歌和火狐浏览器,并打开百度网址的代码。

#chrome
chromePath = os.getcwd()+'/../'+'webdriver/chromedriver.exe'
os.environ['webdriver.chrome.driver']=chromePath
driver=webdriver.Chrome(executable_path=chromePath)
driver.get('http://www.baidu.com')

#firefox
firefoxPath=os.getcwd()+'/../'+'webdriver/geckodriver.exe'
os.environ['webdriver.gecko.driver']=firefoxPath
driver=webdriver.Firefox(executable_path=firefoxPath)
driver.get('https://www.baidu.com')
原文地址:https://www.cnblogs.com/TD1900/p/11913462.html