如何重复执行selenium3 webdriver-webdriver测试脚本,并且不增加浏览器内存

关注51Testing
Selenium&Webdriver远程测试和多线程并发测试
发表于: 09:42 &作者:TestWorld & 来源:51Testing软件测试网采编
推荐标签:
   Webdriver自动化,初学者可以使用selenium ide录制脚本,然后生成程序导入eclipse中调试运行!当然录制出来的东西回放不一定能成功,还需要手工去修改;selenium自动化测试工具,但是特殊情况下也可以用来测试性能;先来介绍一下selenium 如何启动远程pc上的进行测试!  启动远程pc浏览器之前,需要下载selenium-server-standalone-2.40.0.jar,  1、主机端cmd下运行命令:  java -jar selenium-server-standalone-2.40.0.jar -role hub  2、远程pc机cmd下运行命令:  java -jar selenium-server-standalone-2.40.0.jar -Dwebdriver.firefox.bin="E:\Mozilla Firefox\firefox.exe" -role &webdriver -hub http://10.30.12.110:4444/grid/register -browser browserName=firefox -port 7777  (Dwebdriver.firefox.bin="E:\Mozilla Firefox\firefox.exe是远程pc机浏览器安装路径;http://10.30.12.110:4444是主机地址和hub端口;节点端口7777不能和主机端口重复)  实例代码如下:import java.io.Fimport java.net.URL;import java.text.SimpleDateFimport java.util.Dimport org.openqa.selenium.By;import org.openqa.selenium.OutputTimport org.openqa.selenium.Pimport org.openqa.selenium.TakesSimport org.openqa.selenium.WebDimport org.openqa.selenium.WebEimport org.openqa.selenium.firefox.FirefoxDimport org.openqa.selenium.ie.InternetExplorerDimport org.openqa.selenium.remote.DesiredCimport org.openqa.selenium.remote.RemoteWebDimport org.openqa.selenium.support.ui.ExpectedCimport org.openqa.selenium.support.ui.WebDriverWimport org.testng.annotations.Timport net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Mpublic class TestLogin &implements Runnable {public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");@Testpublic void run() {System.out.println(Thread.currentThread().getId()+sf.format(new Date()));DesiredCapabilities capability = DesiredCapabilities.firefox();// & & &capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);//设置用来匹配node中要使用的浏览器capability.setBrowserName("firefox");capability.setVersion("24");capability.setPlatform(Platform.WINDOWS);WebDriver driver =String baseUrl = "http://XX.XX.XX.XX:9080/cas/login";//设置本地驱动,如果你实例化Driver的时候是"WebDriver driver = new InternetExplorerDriver(capability)"这种方式,就必须设置//System.setProperty("webdriver.ie.driver","D:\\IEDriverServer.exe");try{//本地启动浏览器// & & & & & &driver = new FirefoxDriver(capability);//远程启动浏览器driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);System.out.println(Thread.currentThread().getId()+"访问网页开始时间:"+sf.format(new Date()));driver.get(baseUrl);//打开网页try {//等待页面打开,超时设置10秒WebElement loginAccount = new WebDriverWait(driver, 10).until(new ExpectedCondition&WebElement&() {public WebElement apply(WebDriver d) {return d.findElement(By.id("loginAccount"));}});if(null==loginAccount){System.out.println(Thread.currentThread().getId()+" Timeout !!!");driver.quit();Thread.currentThread().interrupt();}else{System.out.println(Thread.currentThread().getId()+"访问网页结束时间:"+sf.format(new Date()));loginAccount.clear();loginAccount.sendKeys("username");WebElement loginPassword = driver.findElement(By.id("loginPassword"));loginPassword.clear();loginPassword.sendKeys("password");WebElement area = driver.findElement(By.cssSelector("area"));System.out.println(Thread.currentThread().getId()+"登录开始时间:"+sf.format(new Date()));area.click();try {//等待登录成功,超时设置10秒WebElement quxiao = new WebDriverWait(driver, 10).until(new ExpectedCondition&WebElement&() {public WebElement apply(WebDriver d) {return &d.findElement(By.xpath(".//*[@class='x-btn-mc']/em/button[text()='取消']"));}});if(null==quxiao){System.out.println(Thread.currentThread().getId()+" Loign &Timeout !!!");driver.quit();Thread.currentThread().interrupt();}else{System.out.println(Thread.currentThread().getId()+"登录成功时间:"+sf.format(new Date()));System.out.println(Thread.currentThread().getId()+"点击取消时间:"+sf.format(new Date()));quxiao.click();}} catch (Exception e) {System.out.println(Thread.currentThread().getId()+" Loign Error !!!");e.printStackTrace();driver.quit();Thread.currentThread().interrupt();}}}catch (Exception e) {System.out.println(Thread.currentThread().getId()+" Visit Error !!!");e.printStackTrace();driver.quit();Thread.currentThread().interrupt();}}catch (Exception e) {e.printStackTrace();driver.quit();}finally{if(null!=driver){driver.quit();}}}  如果先要做远程多线程并发测试,将上面的代码new出了很多实例并且启动他们,启动selenium 也需要多加几个参数:  1、主机端cmd下运行命令:  java -jar selenium-server-standalone-2.40.0.jar -role hub -maxSession 40 -port 4444  (maxSession 设置最大连接数)  2、远程pc机cmd下运行命令:  java -jar selenium-server-standalone-2.40.0.jar -Dwebdriver.firefox.bin="E:\Mozilla Firefox\firefox.exe" -role node -hub http://127.0.0.1:4444/grid/register -maxSession 20 -browser "browserName=firefox,version=24,platform=WINDOWS,maxInstances=20" -port 5555  (maxInstances是同时运行浏览器的数量)  不过在我实际使用过程中火狐浏览器是无法同时实现并发的,但是IE浏览器就可以,所以需要把上面火狐的设置改成IE的就可以了!不到万不得已还是不建议使用这种方法进行!
搜索风云榜
51Testing官方微信
51Testing官方微博
测试知识全知道Selenium WebDriver的使用(三) - sammyzhang - 博客园
Selenium WebDriver支持驱动众多的浏览器,包括PhantomJS、HtmlUnitDriver等无界面浏览器,他们速度快,但在兼容性上还是有点问题,有时会遇到页面JS执行错误,在一些应用场合,页面内容提取也不够简洁方便。
对于常用的桌面浏览器,兼容性很好,但在执行性能上较无界面浏览器要差一些。一是由于需要开启实体的浏览器加载页面的所有资源并进行页面渲染,这将大量消耗CPU及内存资源,在做多线程并发时尤为明显;二是由于通过浏览器的API进行页面内容的查找提取(Selenium RC是通过JS注入的方式实现的),性能受浏览器制约。那么在使用桌面浏览器进行网页的自动化测试、内容抓取时,提供一些提高性能的方法和建议:
取消不必要的消耗资源的配置,尽可能少的打开关闭操作浏览器窗口,及时关闭不必要的窗口,并始终保持浏览器窗口的最小化。
webDriver对浏览器窗体可以实现指定屏幕坐标位置、窗体尺寸、最大化,但无法最小化浏览器窗口:
1 webDriver.manage().window().setPosition(new Point(0, 0));//指定窗口坐标
2 webDriver.manage().window().setSize(new Dimension());//指定窗口大小
3 webDriver.manage().window().maximize();//最大化窗口
如果使用的是Google Chrome,可通过添加command line flag实现:
1 options.addArguments("--window-position=0,0");//指定窗口坐标
2 options.addArguments("--window-size=");//指定窗口大小
3 options.addArguments("-start-maximized");//最大化窗口
4 options.addArguments("--start-fullscreen");//全屏
同样,该方式也不提供将窗口最小化的方法。(如果需要调整窗口,建议通过command line flag实现,在打开浏览器时一步到位了,webDriver.manage是在打开浏览器窗口后再执行调整的)
最小化浏览器窗口需要另行编程实现。一种通过AutoIt脚本的快速实现方法:
$aWinList = WinList("[REGEXPTITLE:(?i)(.*Google Chrome.*)]")
For $i = 1 To $aWinList[0][0]
If BitAND(WinGetState($aWinList[$i][1]), 2) Then
WinSetState($aWinList[$i][1], "", @SW_MINIMIZE)
WinSetState($aWinList[$i][1], "", @SW_HIDE)
监控并关闭弹窗,Google Chrome默认就开启了屏蔽,但不可靠,可以通过&--disable-popup-blocking&关闭屏蔽功能。
删除占用资源的遗留临时文件、进程等,代码略。
最小化浏览器窗口可以降低浏览器对CPU的占用,特别是在大型的含有大量动画元素的页面上。
一些使用了lazy load或异步加载内容的页面,为了可靠获取页面元素,可以在页面加载完成后通过action.clickAndHold()、js.executeScript()的途径,执行触发动作。
通过循环进行元素查找,要尽可能快的返回查找结果,减少对webElement的所有操作。遍历findElements()返回的webElements,使用foreach总是从最高节点开始查找,在逾期内部子节点更可能命中时,应该改为反向遍历。
优先使用cssSelector并尽可能缩小查找范围,使用xpath速度会较慢。(Google Chrome v49实测也没慢多少 !-_-)
如何获取Request和Response信息?
原创文章,转载请标明出处。自动化测试用selenium2,也就是webdriver。大家就问题进来讨论哦【软件测试吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:58,328贴子:
自动化测试用selenium2,也就是webdriver。大家就问题进来讨论哦收藏
我今年参加无锡达内的软件测试就业班,毕业前世界500强Infosys来达内招聘,达内美国上市公司,专注it培训15年,软件测试行业紧缺人才,来达内先就业后付款,推荐就业
这个需要一些C或java基础。
通过注入selenium包的eclipse,编译脚步。通过脚本控制浏览器,包括IE firefox等。
你的帖子怎么指定可
自动化测试的成本怎么样
楼主好,我最近想学习一下selenuim,请问有没有好的,新的资料分享一些呢?
登录百度帐号推荐应用Selenium + Webdriver 学习(二) 各种浏览器简单操作 - CSDN博客
Selenium + Webdriver 学习(二) 各种浏览器简单操作
打开一个测试浏览器
对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏览器进行操作。但要注意的是,因为Chrome Driver是项目自己支持和维护的,所以你必需另外下载安装Chrome Driver,详细介绍查下他们的。
以下是对FF\ IE(7.8.9.10)\chrome 浏览器的例子
Java代码:
&&&&&&package&com.annie.&&&&import&java.io.F&&&&import&org.openqa.selenium.WebD&&import&org.openqa.selenium.chrome.ChromeD&&import&org.openqa.selenium.firefox.FirefoxB&&import&org.openqa.selenium.firefox.FirefoxD&&import&org.openqa.selenium.ie.InternetExplorerD&&import&org.openqa.selenium.remote.DesiredC&&&&public&class&OpenBrowser&{&&&&&&&&&&&&&&&&public&static&void&main(String[]&args)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&WebDriver&diver&=&new&FirefoxDriver();&&&&&&&&&&&&System.out.println(&success&open&the&Firefox01&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.setProperty(&webdriver.firefox.bin&,&C:\\Program&Files&(x86)\\Mozilla&Firefox\\firefox.exe&);&&&&&&&&&&&&&WebDriver&dr&=&new&FirefoxDriver();&&&&&&&&&&&&System.out.println(&success&open&the&Firefox02&);&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&File&pathToFirefoxBinary&=&new&File(&C:\\Program&Files&(x86)\\Mozilla&Firefox\\firefox.exe&);&&&&&&&&&&&&&&FirefoxBinary&firefoxbin&=&new&FirefoxBinary(pathToFirefoxBinary);&&&&&&&&&&&&&&WebDriver&driver1&=&new&FirefoxDriver(firefoxbin,null);&&&&&&&&&&&&System.out.println(&success&open&the&Firefox03&);&&&&&&&&&&&&&&&&&&&&DesiredCapabilities&ieCapabilities&=&DesiredCapabilities.internetExplorer();&&&&&&&&&&ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);&&&&&&&&&&WebDriver&oWebDriver&=&new&InternetExplorerDriver(ieCapabilities);&&&&&&&&&System.setProperty(&webdriver.ie.driver&,&C:\\Windows\\IEDriverServer.exe&);&&&&&&&&&oWebDriver.get(&&);&&&&&&&&&System.out.println(&success&open&the&IE&);&&&&&&&&&&&&&&&&&&&System.setProperty(&webdriver.chrome.driver&,&C:\\Windows\\chromedriver.exe&);&&&&&&&&&&&&&&&&&&&&&&&WebDriver&chdr=new&ChromeDriver();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&}&&&&&/STRING&&&
/*** This class
open browser
* @author annie.wang
package com.annie.
import java.io.F
import org.openqa.selenium.WebD
import org.openqa.selenium.chrome.ChromeD
import org.openqa.selenium.firefox.FirefoxB
import org.openqa.selenium.firefox.FirefoxD
import org.openqa.selenium.ie.InternetExplorerD
import org.openqa.selenium.remote.DesiredC
public class OpenBrowser {
* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
/** default open firefox
WebDriver diver = new FirefoxDriver();
System.out.println(&success open the Firefox01&);
//打开指定路径的firefox,方法1
System.setProperty(&webdriver.firefox.bin&,&C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe&);
WebDriver dr = new FirefoxDriver();
System.out.println(&success open the Firefox02&);
//打开指定路径的firefox,方法2
File pathToFirefoxBinary = new File(&C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe&);
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver1 = new FirefoxDriver(firefoxbin,null);
System.out.println(&success open the Firefox03&);
/** open IE browser
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);//
Setting the IE browser security
System.setProperty(&webdriver.ie.driver&,&C:\\Windows\\IEDriverServer.exe&);
oWebDriver.get(&&);
System.out.println(&success open the IE&);
/**open chrome browser
System.setProperty(&webdriver.chrome.driver&,&C:\\Windows\\chromedriver.exe&);
// System.setProperty(&webdriver.chrome.bin&,&C:\\Windows\\chromedriver.exe&);
WebDriver chdr=new ChromeDriver();
/** close the browser
1.method :quit
* driver.quit();
* close the browser
2.method :close
* driver.close();
/**得到当前页面url
* 返回当前浏览器的窗口句柄 */
/* String currentUrl = driver.getCurrentUrl();
//输出title和currenturl
System.out.println(&页面title:&+title+&\n&+&当前URL&+currentUrl);
((JavascriptExecutor)driver).executeScript(&alert(\&hello,this is a alert!\&)&);
String windowHandle=driver.getWindowHandle() ;
System.out.println(&当前窗口句柄:&+windowHandle+&\n&+windowHandle);
//String windowHandles=driver.getWindowHandles(Set); // 返回所有浏览器的窗口句柄
// String pageSource=driver.getPageSource()
//返回当前页面的源码
// System.out.println(pageSource+&\n&+pageSource);
IE 中 尤其注意,可能会报以下异常:
这个异常是由于IE浏览器的安全权限和zoom造成的,以下3个步骤方可解决
1. 加入此代码片段:
DesiredCapabilities&ieCapabilities&=&DesiredCapabilities.internetExplorer();&&ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);&&WebDriver&oWebDriver&=&new&InternetExplorerDriver(ieCapabilities);&&
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);//Setting the IE browser security
2. 设置Internet Option Enable 选项 勾掉
3. 设置zoom 为100%
运行成功,注意IE9下有异常 。尚未有解决方案,但是不影响IE9 测试功能
从上面代码可以看出操作浏览器的主要方法都来自org.openqa.selenium.WebDriver这个接口中。看了一下源代码这些方法都 是在org.openqa.selenium.remote.RemoteWebDriver这个类中实现的,然后不同浏览的driver类继承 RemoteWebDriver。
本文已收录于以下专栏:
相关文章推荐
selenium运行自动化脚本(验证通过)
( 20:58:07)
分类:Java学习
项目顾问建议用...
在用selenium 1.X的时候常常会用到getEval()方法来执行一段js脚本来对页面进行处理,以处理一些遇到的问题。当然selenium webdriver也提供这样的一个方法:execute...
selenium-webdriver提供了强大的元素定位方法,支持以下三种方法。
单个对象的定位方法多个对象的定位方法层级定位              ...
在编写自动化测试的时候,可能会碰到这样的情况:某个文本框用来选择日期,并且该文本框是只读的,这时我们不太好对该时间控件进行原生selenium api的操作,那么就可以直接执行js,直接设置该文本框的...
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get(&ht...
Selenium+Webdriver 执行JS脚本
博客园 — 虫师
如何打开一个测试浏览器
做自动化测试一般情况下我们都需要首先打开测试浏览器,浏览器开启后我们方可&命令&浏览器去打开新页面,点击特定的链接,判断具体的逻辑等等。因此该操作为&万里长征的第一步&,必须...
测试工作中,对浏览器的操作无而非就是:
最大化窗口设置窗口大小在不同页面间前进和后退
这三种比较简单,直接上代码:
# 导入webdriver
from selenium import webd...
WebDriver简介selenium从2.0开始集成了webdriver的API,提供了更简单,更简洁的编程接口。selenium webdriver的目标是提供一个设计良好的面向对象的API,提供...
他的最新文章
讲师:宋宝华
讲师:何宇健
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Selenium+Webdriver学习(三) 执行JS脚本 - CSDN博客
Selenium+Webdriver学习(三) 执行JS脚本
在用selenium 1.X的时候常常会用到getEval()方法来执行一段js脚本来对页面进行处理,以处理一些遇到的问题。当然selenium webdriver也提供这样的一个方法:executeScript()
下面演示在打开51.com首页如何得到帐号输入框中显示的字符,并打印输出。
/*** This class is about Execute javascript
* @author annie.wang
package com.annie.
import org.openqa.selenium.JavascriptE
import org.openqa.selenium.WebD
import org.openqa.selenium.firefox.FirefoxD
public class Execu_JS {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver=new FirefoxDriver();
driver.get(&&);
/** Selenium 执行JS*/
String js = &var user_input = document.getElementById(\&passport_51_user\&).return user_&;
String input_title=(String)((JavascriptExecutor)driver).executeScript(js);
System.out.println(&---input title---&+input_title);
String title = driver.getTitle();
//得到当前页面url
String currentUrl = driver.getCurrentUrl();
//输出title和currenturl
System.out.println(&页面title:&+title+&\n&+&当前URL&+currentUrl);
((JavascriptExecutor)driver).executeScript(&alert(\&hello,this is a alert!\&)&);
String windowHandle=driver.getWindowHandle() ; //返回当前浏览器的窗口句柄
System.out.println(&当前窗口句柄:&+windowHandle+&\n&+windowHandle);
//String windowHandles=driver.getWindowHandles(Set); // 返回所有浏览器的窗口句柄
// String pageSource=driver.getPageSource()
//返回当前页面的源码
// System.out.println(pageSource+&\n&+pageSource);
结果如下:
本文已收录于以下专栏:
相关文章推荐
问题场景:
使用java 掉用js代码,发现 ScriptEngine不支持浏览器内置对象window,document的问题;
问题一:为什么要 用java掉用js代码?
    比如在 抓取...
JavascriptExecutor js = (JavascriptExecutor)
js.executeScript(String script, object... a...
webdriver UI自动化测试执行JS脚本
不管是selenium RC 还是 webdriver 其在浏览器端 最终还是通过JS来操作页面元素实现自动化测试的(可参见2者的实现原理),在做自动化...
接下来将通过几个具体的例子来使用操作上述功能,以下代码都是通过junit框架编写。
例子1:该例子主要是登录网易相册,创建一个私人相册,并进入相册进行评论,然后删除这个相册。在这个例子中,可以找到各种...
//指定WebDrive
File file = new File(System.getProperty(&user.dir&)+&\\WebDrive\\IEDriverServer.e...
webdriver的API为我们做web自动化测试提供了强大支持,以下是本人完成该环境下的第一个脚本,时间用了蛮久,过程中在书本和网上查了很多信息,但是都没有根据脚本比对前端页面代码的讲解,于是详细记...
我们在看JSCover(http://tntim96.github.io/JSCover/manual/manual.xml)介绍及使用说明的时候,往往被图形界面所吸引。这种方式比较适合手工操作,点击...
他的最新文章
讲师:宋宝华
讲师:何宇健
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 selenium webdriver包 的文章

 

随机推荐