Selenium1:搭建自动化测试环境

这篇具有很好参考价值的文章主要介绍了Selenium1:搭建自动化测试环境。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Selenium IDE/我的第一个web自动化程序 2020/4/28 周二 64-66

0 Selenium: 驱动浏览器做自动化测试

官网:https://www.selenium.dev/

分三块:

Selenium IDE,是Firefox的扩展插件,支持用户录制和回放测试

Selenium WebDriver,提供了各种语言环境的API来支持更多控制权和编写符合标准软件开发实践的应用程序。

SeleniumGrid,分布式自动化工具,可以在多个测试环境中以并发的方式执行测试脚本,实现测试脚本的并发 执行,缩短大量的测试脚本的执行时间

Selenium WebDriver

Selenium Grid

1 Selenium IDE:(了解)

Selenium IDE 是一个Firefox/Chorme插件,用于记录和回放用户与浏览器的交互。

只支持Firefox、chrome。其中chrome必须翻墙,所以用Firefox来进行学习。

1.1 下载Selenium IDE for firefox:

Selenium 官网:https://www.selenium.dev/downloads/

选Selenium IDE for firefox: Will enter https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/ and click "Add to firefox" will download.

浏览器右上角有个Se小logo,就说明安装好了。

1.2 点击Se的logo,"Record a new test in a new project"/"Create a new project". Enter project name: SeleniumIDETest;Enter Base URL:https://www.baidu.com;Start recording:浏览器会自动打开百度。

1.3 开始操作浏览器。

1 输入腾讯课堂,点击“百度一下”

2 点击第一条记录,会新开一个tab,显示腾讯课堂首页。

回到IDE,右上角关闭录制,填test name:BaiduTest,OK,脚本就录好了。

1.4 脚本回放:

在页面点“Current run test”:会自动输“腾讯课堂”,查询,点了官网链接,但是报错:因为Firefox阻止了新窗口弹出。

->解决方案:Firefox->选项->隐私与安全->权限->拦截弹出式窗口:Uncheck.退出火狐浏览器,重新打开,进行脚本回放,ok。

1.5 导出脚本,为Java、Python、Ruby(必须Firefox版本比较高)

Testname右边有三个点,Export->Java->Export .java file

1.6 Selenium IDE:

Selenium IDE 是一个Firefox/Chorme插件,用于记录和回放用户与浏览器的交互。使用它来创建简单的脚本或者协助进行探索性测试。

缺点:只支持Firefox/Chorme;脚本不稳定;线性脚本,无法处理复杂的逻辑。

2 Selenium WebDriver(重点掌握):

用于Web应用程序测试的工具,Selenium是开源并且免费的,Selenium 测试直接在浏览器中运行,就像真实用户所 做的一样 覆盖IE、Chrome、FireFox、Safari等主流浏览器。通过在不同浏览器中运行测试,更容易发现浏览器的不兼容性。 支持Java、Python、Net、Perl等编程语言进行自动化测试脚本编写。

2.1 发展历程

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

2.2 Selenium WebDriver原理

驱动(翻译器):把客户端发送来的指令,翻译成浏览器能识别的语言。

不同浏览器提供不同driver驱动

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

3 Maven安装配置:

0 Maven作用

maven 作用:项目构建工具,是目前大部分企业在做项目时首选的项目构建,管理工具;也有一部分企业在用Ant,Aspira用的是Ivy

编译代码;项目打包;项目模块化管理;在线管理依赖;提供丰富的插件,方便项目持续集成。

在我们课程中最大的作用管理jar包[其他jar包管理工具:Ant,gradle。

第三方jar(工具包),如Excel、Json

Maven: 别人写好的jar包;git:管理自己的代码

本地下载maven,本地新建一个本地仓库文件夹repository,空的。远程有一个中心仓库,是由maven团队管理的。本地仓库可以直连中心仓库,来获取jar包。但因为中心仓库在美国,直接连很慢。加了阿里云的镜像,就像代理服务。阿里云会定时去同步,就像私服。配了镜像,就会去阿里云取jar,就会很快。

1 下载maven&环境配置:3.6.1

https://maven.apache.org/download.cgi

Maven 3.6.1:D:\developSoftware\apache-maven-3.6.1

1 新建环境变量:变量名MAVEN_HOME,变量值为解压后的文件夹:D:\developSoftware\apache-maven-3.6.1

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

2 修改PATH变量:

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

2 修改Maven全局配置文件:

将Maven仓库配置为自己创建的文件夹+配置mirror:修改这个文件D:\developSoftware\apache-maven-3.6.1\conf\settings.xml

创建maven仓库:自定义文件夹作为本地Maven仓库

如新建文件夹 D:\developSoftware\apache-maven-3.6.1\repository

配置文件路径 D:\developSoftware\apache-maven-3.6.1\conf\settings.xml ,

1 将Maven仓库配置为自己创建的文件夹:L55

<localRepository>D:\developSoftware\apache-maven-3.6.1\repository</localRepository>

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

2 配置阿里云镜像,指定下载jar包的地址,这样访问会比较快:L159-164

<mirror>

<id>nexus-aliyun</id>

<mirrorOf>central</mirrorOf>

<name>Nexus aliyun</name>

<url>http://maven.aliyun.com/nexus/content/groups/public</url>

</mirror>

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

3 Eclipes中配置maven

Window->Preference->Maven->Installation/User settings, apply.

Maven-Installation: 配置本地maven根目录:D:\developSoftware\apache-maven-3.6.1

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

Maven-User settings shall as below:

User setting: D:\developSoftware\apache-maven-3.6.1\conf\settings.xml

Local Repository: D:\developSoftware\apache-maven-3.6.1\repository

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

遇到的问题:

Eclipse->Preference:看不到maven [Solution: https://blog.csdn.net/u013033112/article/details/80618961]

Solution:安装插件

  Help --> Install New Software --> Add, 输入如下内容:

  m2e

  http://download.eclipse.org/technology/m2e/releases

一直Next到最后,提示重启eclipse即可。

4 搭建自动化测试环境

1 创建Maven项目

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档
selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

Group Id:分组,填组织名,如com.lemon

Artifact Id:项目包名(简单理解为jar包名)

Version:版本

Packaging:打包方式

pom.xml添加依赖:

<dependencies>

<dependency>

...

</dependency>

...

</dependencies>

1 maven项目结构

src/main/java:一般是开发人员写代码的目录

src/main/resources: 开发人员放项目资源文件的目录

src/test/java:放测试人员代码的目录

src/test/resources:放测试资源文件的目录

Maven Dependencies:通过maven拿下来的项目依赖包存放目录

target: 存放构建后文件的目录

2 Maven的几个常用构建命令

mvn package:打包

mvn install:安装到本地仓库

mvn test:执行单元测试代码

2 引入selenium框架:pom.xml里引入selenium依赖

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>3.141.59</version>

</dependency>

NOTE: Version 应该选比较新,并且usage人数比较多的:https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

进到版本页:然后把maven配置信息复制粘贴到eclipse里pom.xml的文件里。

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

保存pom.xml后会自动下载依赖包,然后在eclipse中maven dependency下面就能看到这些依赖包

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档

遇到问题:但是我的本地pom一直报错:

解决方法:

pom.xml:

<properties>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.source>1.8</maven.compiler.source>

<!-- 文件拷贝时的编码 -->

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- 编译时的编码 -->

<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<aspectj.version>1.8.10</aspectj.version>

</properties>

<dependencies>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>3.141.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.google.j2objc/j2objc-annotations -->

<dependency>

<groupId>com.google.j2objc</groupId>

<artifactId>j2objc-annotations</artifactId>

<version>1.3</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/animal-sniffer-annotations -->

<dependency>

<groupId>org.codehaus.mojo</groupId>

<artifactId>animal-sniffer-annotations</artifactId>

<version>1.17</version>

<scope>provided</scope>

</dependency>

</dependencies>

</project>

最終我的pom.xml(可用):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 " target="_blank">http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.twentythree</groupId>

<artifactId>webtest</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>com.webtest.auto</name>

<properties>

<maven.compiler.target>1.8</maven.compiler.target>

<maven.compiler.source>1.8</maven.compiler.source>

<!-- 文件拷贝时的编码 -->

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- 编译时的编码 -->

<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<aspectj.version>1.8.10</aspectj.version>

</properties>

<dependencies>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>3.141.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.google.j2objc/j2objc-annotations -->

<dependency>

<groupId>com.google.j2objc</groupId>

<artifactId>j2objc-annotations</artifactId>

<version>1.3</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/animal-sniffer-annotations -->

<dependency>

<groupId>org.codehaus.mojo</groupId>

<artifactId>animal-sniffer-annotations</artifactId>

<version>1.17</version>

<scope>provided</scope>

</dependency>

</dependencies>

</project>

3 写代码

0.1 下载老版本的Firefox-V56 &取消自动更新:

下载:http://ftp.mozilla.org/pub/firefox/releases/56.0/win64/zh-CN/

设置:

1 打开Firefox浏览器->选项->常规->选中:不检查更新。

2 找到Firefox安装路径:C:\Program Files\Mozilla Firefox,删掉下面的两个updater文件即可。

0.2 下载老版本的chrome-V68,且取消自动更新:

下载:https://www.chromedownloads.net/chrome64win/

设置:https://jingyan.baidu.com/article/76a7e409f2137afc3b6e15be.html

1 右键选中左下角,运行,msconfig,服务,选中:隐藏所有Microsoft服务,取消选中Google更新服务,确定即可。

1 Chrome打开百度首页(好了):

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档
1 代码:

public class OpenBrow

{

public static void main(String[] args)

{

// 相当于环境变量,让测试脚本认识ChromeDriver在哪里

System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\chromedriver.exe");

ChromeDriver driver = new ChromeDriver();

driver.get("https://www.baidu.com");

}

}文章来源地址https://www.toymoban.com/news/detail-780051.html

2 问题

Q1: The path to the driver executable must be set by the webdirver.chrome.driver system properity

S1:

1 下载ChromeDriver:http://npm.taobao.org/mirrors/chromedriver,下载什么版本呢?在chrome地址栏输入:chrome://version/ ,查看当前chrome浏览器版本68,查看:http://npm.taobao.org/mirrors/chromedriver/2.40/notes.txt,则对应的chromedriver 可以是V2.41,V2.40,V2.39.先试下V2.40.下载到本地,解压得到一个exe文件,把这个chromedriver.exe文件复制到eclipse-src/test/resources路径下。

2 配置属性:

System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");

代码:

public static void main(String[] args)

{

//相当于环境变量,让测试脚本认识ChromeDriver在哪里

System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");

ChromeDriver driver = new ChromeDriver();

driver.get("https://www.baidu.com");

}

2 FireFox 打开百度首页:

selenium自动化测试环境搭建,Selenium,selenium,firefox,测试工具,Powered by 金山文档
1 代码:

public class OpenBrow

{

public static void main(String[] args)

{

// 相当于环境变量,让测试脚本认识ChromeDriver在哪里

System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\geckodriver.exe");

FirefoxDriver driver = new FirefoxDriver();

driver.get("https://www.baidu.com");

}

}

2 问题:

Q1:Cannot find firefox binary in PATH.

Solution:

//如果firefox安装不是在默认的c盘,需要:设置系统属性:属性名:"webdriver.gecko.bin",属性值:firefox.exe安装的路径

//System.setProperty("webdriver.firefox.bin", value);

Q2: The path to the driver executable must be set by the webdriver.gecko.driver

Solution: 缺少火狐浏览器驱动包。

下载geckodriver驱动包?什么版本呢?根据firefox版本,找到合适的geckodriver版本。

因为firefox是V56,可以下载geckodriver是0.19或者0.21

geckodriver镜像地址:https://npm.taobao.org/mirrors/geckodriver/

选V0.20.0,下载,解压得到geckodriver.exe,把这个文件复制到eclipse,src/test/resources路径下面。

加一行代码:

System.setProperty("webdriver.gecko.driver", "src\\main\\resources\\geckodriver.exe");

3 IE打开百度首页:

Q1:缺少IE浏览器驱动包:The path to the driver executable must be set by the webdriver.ie.driver

Solution:下载IE浏览器驱动包,配置属性

注意:ChromeDriver、FirefoxDriver版本是根据chrome、firefox浏览器版本决定的,但是IEDriver的版本是根据selenium决定的。

因为selenium是3.141.0,所以IEDriver选择:3.141

IEDriver镜像下载:https://npm.taobao.org/mirrors/selenium/

IEDriverServer,64位,3.141.0

把解压的IEDriverServer.exe放到eclipse,src/test/resources路径下,加一行代码:

System.setProperty("webdriver.ie.driver", "src\\main\\resources\\IEDriverServer.exe");

Q2:Protected Mode Settings are not the same for all zones

Solution1: 不推荐

手动打开IE浏览器->工具->安全->所有选项的保护模式全部关闭

Solution2:推荐,代码处理

//忽略IE protected mode的设置

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

InternetExplorerDriver ieDriver = new InternetExplorerDriver(capabilities);

Q3: Unexpected error launching Internet Explorer. Browser zoom level was set to 150%. It should be set to 100%

Solution1:手动调整到100%,不推荐。

Solution2:代码调整,推荐:

capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

@SuppressWarnings("deprecation")

InternetExplorerDriver ieDriver = new InternetExplorerDriver(capabilities);

4 封装一个通用的方法来打开各浏览器Chrome、Firefox、IE:

public static void main(String[] args)

{

WebDriver driver = openBrowser("ie");

driver.get("https://www.baidu.com");

}

//多态:父类WebDriver接收子类返回对象:chromeDriver/firefoxDriver/ieDriver

public static WebDriver openBrowser(String browser) {

if(browser.equalsIgnoreCase("chrome")) {

System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");

ChromeDriver chromeDriver = new ChromeDriver();

return chromeDriver;

}else if (browser.equalsIgnoreCase("firefox")) {

System.setProperty("webdriver.gecko.driver", "src\\main\\resources\\geckodriver.exe");

FirefoxDriver firefoxDriver = new FirefoxDriver();

return firefoxDriver;

}else if (browser.equalsIgnoreCase("ie")) {

System.setProperty("webdriver.ie.driver", "src\\main\\resources\\IEDriverServer.exe");

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

@SuppressWarnings("deprecation")

InternetExplorerDriver ieDriver = new InternetExplorerDriver(capabilities);

return ieDriver;

}

return null;

}

}

到了这里,关于Selenium1:搭建自动化测试环境的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • selenium 自动化测试——环境搭建

    安装python,并且使用pip命令安装 selenium 然后尝试第一次使用selenium 完成一个简单的测试自动化脚本 执行过程中,你会发现执行到第4行时会报错,那是因为没有下载对应的浏览器驱动, 下载对应的驱动,我的是chrome浏览器,查看版本 然后找到对应的webdriver文件下载,完成后

    2024年02月10日
    浏览(43)
  • python+selenium自动化测试环境搭建步骤(selenium环境搭建)

     📢专注于分享软件测试干货内容,欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📢交流讨论:欢迎加入我们一起学习! 📢资源分享:耗时200+小时精选的「软件测试」资料包 📢 软件测试学习教程推荐:火遍全网的《软件测试》教程 ​ 1.自动化测试概念:       是

    2024年01月22日
    浏览(46)
  • Python + Selenium 自动化测试入门-环境搭建

    1、python 开发环境安装         python官网下载地址:https://www.python.org/downloads/         命令行查看安装python版本:python --version  python -V 2、pycharm 开发工具安装         pycharm官网下载地址:​​​​​​https://www.jetbrains.com/pycharm/download/ 3、selenium 工具包安装         命令行

    2023年04月14日
    浏览(51)
  • selenium+python自动化测试之环境搭建

    最近由于公司有一个向谷歌网站上传文件的需求,需要进行web的自动化测试,选择了selenium这个自动化测试框架,以前没有接触过这门技术,所以研究了一下,使用python来实现自动化脚本,从环境搭建到实现脚本运行。 selenium是一个用于Web应用程序测试的工具。Selenium测试直接

    2024年01月19日
    浏览(53)
  • UI自动化测试篇 :Selenium2(Webdriver)&TestNG自动化测试环境搭建

     🔥 交流讨论: 欢迎加入我们一起学习! 🔥 资源分享 : 耗时200+小时精选的「软件测试」资料包 🔥  教程推荐: 火遍全网的《软件测试》教程   📢 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!    最开始学习UI自动化,用的工具是QTP10,用起来确实比较容易上手

    2024年03月10日
    浏览(56)
  • python+selenium+pycharm自动化测试环境搭建

    1. 下载Python Python Release Python 3.8.0 | Python.org 本人电脑是64位的,下载相应文件。 进入cmd(windows命令提示符)下面输入\\\"Python\\\"命令。 (如果提示python不是内部或外部命令!别急,去配置一下环境变量吧) 修改我的电脑-属性-高级-环境变量-系统变量中的PATH为: 变量名:PATH 变量值:

    2023年04月08日
    浏览(49)
  • selenium+java自动化测试环境搭建mac

    一、官网下载selenium Downloads | Selenium    下载之后的文件如下 二、下载谷歌浏览器驱动,驱动版本和浏览器版本一致、下面是我的谷歌浏览器版本  谷歌浏览器下载地址 CNPM Binaries Mirror  下载完成如下:

    2023年04月08日
    浏览(41)
  • PyCharm 搭建 Selenium + Python 的自动化测试环境

    1、下载和安装 Python: 访问官方 Python 网站(https://www.python.org/downloads/)。 根据操作系统选择适合的 Python 版本,下载安装程序并按照向导完成安装。 2、下载和安装 PyCharm: 访问 JetBrains 官方网站(https://www.jetbrains.com/pycharm/)。 根据操作系统选择适合的版本,下载安装程序

    2024年02月04日
    浏览(48)
  • Python+Selenium3+Chrome自动化测试环境搭建

    写在最前面,因为各种原因,搭建该环境方法多种多样。在本教程中,展示环境搭建的详细步骤。在不同软硬件环境下安装报错,可找出原因,百度排错。 本博客的具体操作视频请移步B站: https://www.bilibili.com/video/BV1oe4y1w7yr/?spm_id_from=333.999.list.card_archive.clickvd_source=585bb8c205

    2024年02月02日
    浏览(48)
  • web自动化测试框架落地实施全过程-测试环境搭建 (Selenium+Python)

    Web自动化测试是指使用自动化工具模拟用户在Web浏览器中执行的操作,通过编写脚本来自动化执行测试用例,以验证Web应用程序的功能、性能和兼容性等方面的质量。其主要目的是降低测试成本和时间,并提高测试效率和准确性。 Web自动化测试通常包括以下步骤: 确定测试

    2024年02月09日
    浏览(77)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包