自动化测试-selenium启动浏览器
在自动化测试过程中,通过selenium启动浏览器时,可能需要加载插件(如测试用的firebug、或产品中要求必须添加某插件等)、读取用户数据(自己浏览器的配置文件/别人直接给的浏览器配置文件)、设置浏览器(不加载图片等)。
由于我们通过selenium启动的浏览器页面,是完全干净的页面,如果想要让该页面带上我们需要的信息,则需要自己设置。
下面讲一下Firefox和Chrome浏览器的各种启动方式:
一:Firefox
1.启动浏览器,使用浏览器上保存的所有用户数据。
用户数据是从Firefox的配置文件中读取的,首先看下自己电脑的配置文件,在cmd上进入Firefox的安装路径下,运行firefox.exe -ProfileManger命令,会弹出自己电脑火狐浏览器的所有配置文件。如下图。
//创建profileIni对象
ProfilesIni ini = new ProfilesIni();
//通过名字来获取到相应的配置文件,如上图所示,也可以写“profile2”
FirefoxProfile profile = ini.getProfile("default");
//创建浏览器驱动,并将profile传入,此时启动的时候,就会读取我们default配置文件来调用相应的火狐浏览器了
WebDriver driver = new FirefoxDriver(profile); driver.get("https://www.cnblogs.com/");
如果是别人传过来的一个配置文件,你自己保存在了地址XXX下。(即使是读取自己本地浏览器配置文件,也可使用下面的方法,只要将路径写对就可以)
//创建文件对象
File file = new File("C:\\your\\path\\yourProfileName");
FirefoxProfile profile = new FirefoxProfile(file); WebDriver driver = new FirefoxDriver(profile); driver.get("https://www.cnblogs.com/");
2.启动浏览器,设置代理
FirefoxProfile profile = new FirefoxProfile();
String proxyIp="192.168.2.111";
int proxyPort = 6666;
//开启代理模式
profile.setPreference("network.proxy.type", 1);
//设置代理服务器IP
profile.setPreference("network.proxy.http", proxyIp);
//设置代理服务器的端口
profile.setPreference("network.proxy.http_port", proxyPort); WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
如果还想了解其他的设置,可以在火狐浏览器搜索就可以了。
3.启动浏览器,安装插件
//设置自己所下载的插件的位置
File file = new File("D:\\your\\xxx.xpi\\path");
//创建火狐浏览器的profile对象
FirefoxProfile profile = new FirefoxProfile(file);
//调用addExtension方法,该方法将file路径中的插件名字提取出来,并将名字和new FileExtension 的对象添加到extensions中
//private Map<String, Extension> extensions = Maps.newHashMap();
profile.addExtension(file); WebDriver driver = new FirefoxDriver(profile);
driver.get("https://www.cnblogs.com/");
二:Chrome
关于Chrome的比较简单,其API上写的都非常清楚。可以自行参考下:https://sites.google.com/a/chromium.org/chromedriver/capabilities。里面的例子都写的很明了。
下面简单举几个例子。
1.添加启动参数(下面是比较常用的字体设置、加载用户数据、模拟手机模式)---addArguments
ChromeOptions options = new ChromeOptions();
//读取用户参数,XX代表当前用户
options.addArguments("--user-data-dir=C:\\Users\\XX\\AppData\\Local\\Google\\Chrome\\User Data"); //设置语言
options.addArguments("--lang=zh_CN.UTF-8");
//模拟手机模式
options.addArguments("--user-agent=iphone 7"); WebDriver driver = new ChromeDriver(options); driver.get("http://www.baidu.com");
关于设置手机模式的,--user-agent对应的值应该怎么填写,在这说明下:
打开浏览器,按F12,手动设置成手机模式,之后:
关于Chrome浏览器启动参数,我们测试中常用的大概还有:
--disable-images(控制是否加载图片)
--start-maximized (启动时最大化)
download.default_directory": download_dir (设置下载保存路径)
2.修改设置----setExperimentalOption
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
//禁止加载图片
prefs.put("profile.default_content_setting_values.images", 2);
options.setExperimentalOption("prefs", prefs); WebDriver driver= new ChromeDriver(options);
driver.get("http://www.baidu.com");
3.安装插件---addExtensions
/*
* 安装插件
*/
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("you/path/XXX")); WebDriver driver = new ChromeDriver(options);
driver.get("http://www.baidu.com");
4.设置代理---DesiredCapabilities
//设置一个空的能力对象
DesiredCapabilities caps = new DesiredCapabilities();
//设置将要代理的IP和端口号
String proxyIpPort="192.168.2.122:8088";
//设置一个空的协议对象
Proxy proxy=new Proxy();
//设置对象支持http、ftp、ssl协议
proxy.setHttpProxy(proxyIpPort).setFtpProxy(proxyIpPort).setSslProxy(proxyIpPort); //设置能力对象,将proxy对象设置为值
caps.setCapability(CapabilityType.PROXY, proxy); WebDriver driver = new ChromeDriver(caps);
driver.get("https://sites.google.com/a/chromium.org/chromedriver/capabilities");
关于Chrome的用户数据,可以到C:\Users\用户名\AppData\Local\Google\Chrome\User Data\Default\preferences文件中查看。想要设置的,里面都可以查到。
自动化测试-selenium启动浏览器的更多相关文章
- python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep
在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...
- python selenium启动浏览器打开百度搜索
python selenium打开百度搜索 #!usr/bin/python from selenium import webdriver import time browser = webdrive ...
- Python安装selenium启动浏览器
1:在Python运行火狐或谷歌的浏览器是需要下载相对应的驱动 例如:你想在Python中使用代码命令打开firefox的网页 如果没有安装驱动,直接运行的话会出下面的错误 所以我们要安装相对应的浏览 ...
- selenium是如何启动浏览器的
前几天有同学问到selenium是怎么样启动浏览器的(selenium启动浏览器的原理),当时稍微讲解了一下,不过自我感觉不够具体,现在特地把启动原理通过代码和一系列操作给串联起来,希望可以帮助大家更 ...
- Selenium 设置浏览器下载 Firefox 和Chrome
当我们在使用Selenium运行自动化测试时,偶尔需要用到下载功能,但浏览器的下载可能会弹出下载窗口,或者下载路径不是我们想要保存的位置,所以在通过Selenium启动浏览器时需要做相关的设置,将使这 ...
- 使用Python + Selenium打造浏览器爬虫
Selenium 是一款强大的基于浏览器的开源自动化测试工具,最初由 Jason Huggins 于 2004 年在 ThoughtWorks 发起,它提供了一套简单易用的 API,模拟浏览器的各种操 ...
- Python selenium + Firefox启动浏览器
Python selenium 的运用 from selenium import webdriver # from selenium.webdriver.firefox.firefox_profile ...
- 使用Selenium通过浏览器对网站进行自动化测试和相关问题
使用Selenium通过浏览器对网站进行自动化测试 自动化测试概念: 一般是指软件测试的自动化,软件测试就是在预设条件下运行系统或应用程序,评估运行结果,预先条件应包括正常条件和异常条件. 广义上来讲 ...
- 自动化小应用系列----利用selenium启动多个独立的浏览器
在我们测试的时候对于同一个系统,我们往往需要登陆多个不同的账号,由于cookie的原因,我们只能退出一个账号在登陆另外一颗账号,非常麻烦.我们可以使用selenium来启动浏览器,这样每个浏览器的属性 ...
随机推荐
- Yii2 指定log存放的文件
在main.php 文件里添加 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => ...
- 在idea配置jetty和创建(包、文件)javaWeb以及Servlet简单实现
在创建之前要安装好jetty jetty官网链接:https://jettylife.com/ 现在进行创建项目: 需要按照好jdk 现在进行添加jetty 现在进行配置 完成后ok ok 下面警告的 ...
- 【洛谷P3818】小A和uim之大逃离 II
小A和uim之大逃离 II 题目链接 比较裸的搜索,vis[i][j]再加一层[0/1]表示是否使用过魔液 转移时也将是否使用过魔液记录下来,广搜即可 #include<iostream> ...
- 【luogu P2071 座位安排】 题解
题目链接:https://www.luogu.org/problemnew/show/P2071#sub 邻接表 + 匈牙利 把之前的邻接矩阵匈牙利变成邻接表 要不然存不下... code: #inc ...
- js关于密码框强弱度的提示
三种密码强度的正则表达式: 较弱:全是数字或全是字母 6-16个字符:/^[0-9]{6,16}$|^[a-zA-Z]{6,16}$/; 中级:数字.26个英文字母 6-16个字符: /^[A-Za- ...
- 指纹获取 Fingerprint2
指纹插件 Fingerprint2 import Fingerprint2 from 'fingerprintjs2' new Fingerprint2().get(function(result, ...
- repo配置与连接
repo是远程访问android源码的工具,和git一起使用. repo的远程安装经常被屏蔽,你懂得. sudo apt-get install curl 244 sudo apt-get - ...
- oracle 分组函数、视图
组函数 分组函数作用于一组数据,对每一组返回一个值 组函数类型: 1.计数 count(列名 或 表达式) 对满足的行数进行统计 2.求和 sum(列名 或 表达式 ...
- oracle官网下载教程
1.百度搜索oracle 也可以直接点击进入 oracle官网 或直接进入 下载页面 2.选择中文,看的更容易些 3.拉到最下面,选择所有下载和试用 4.选择数据库下载 5.点击下载对 ...
- Angularjs基础(二)
AngularJS 表达式 AngularJS 表达式写在双大括号内:{{expression}} AngularJS 表达式把数据绑定到HTML,这与ng-bind 指令有异曲同工之妙 Angula ...