Selenium & Webdriver 远程测试和多线程并发测试

  Selenium Webdriver自动化测试,初学者可以使用selenium ide录制脚本,然后生成java程序导入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.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main;

public class TestLogin  implements Runnable {  
    public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
@Test
    public 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 = null;
        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 server也需要多加几个参数:

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的就可以了!不到万不得已还是不建议使用这种方法进行性能测试!

Selenium & Webdriver 远程测试和多线程并发测试的更多相关文章

  1. Java接口多线程并发测试 (一)

    本文为作者原创,禁止转载,违者必究法律责任!!! 本文为作者原创,禁止转载,违者必究法律责任!!! Java接口多线程并发测试 一,首先写一个接口post 请求代码: import org.apach ...

  2. 【总结】selenium webdriver 远程连接firefox和IE 环境搭建

    参考链接:http://code.google.com/p/selenium/wiki/Grid2 本地环境为:win7,eclipse,jdk 1.7,本机ip为192.168.0.30 1.下载所 ...

  3. Java接口多线程并发测试 (二)

    原文地址http://www.cnblogs.com/yezhenhan/archive/2012/01/09/2317636.html 这是一篇很不错的文章,感谢原博主的分享! JAVA多线程实现和 ...

  4. 利用Testng注释实现多线程并发测试

    Testng 是一款非常优秀的测试框架,真正从测试角度出发,为测试所想.在测试过程中我们经常会遇到对某一个场景做并发请求,主要想了解该程序在并发时是否会有异常或者没考虑到的其他情况,这时往往不是要做性 ...

  5. 多线程并发测试(apache ad)

    1.配置 ThreadPoolTaskExecutor bean   <?xml version="1.0" encoding="UTF-8"?> ...

  6. 使用ab测试工具 进行并发测试

    ab.exe -n1000 -c100 http://localhost:8067/api/todo/555e95feb301baa678141148 http://www.cnblogs.com/y ...

  7. selenium从入门到应用 - 8,selenium+testNG实现多线程的并发测试

    本系列所有代码 https://github.com/zhangting85/simpleWebtest本文将介绍一个Java+TestNG+Maven+Selenium的web自动化测试脚本环境下s ...

  8. testng多线程并行执行测试

    testng多线程并行执行测试 testng多线程并行执行测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者子组件的能力.TestNG允许我们以 ...

  9. 白盒测试中如何实现真正意义上并发测试(Java)

    在这个话题开始之前,首先我们来弄清楚为什么要做并发测试? 一般并发测试,是指模拟并发访问,测试多用户并发访问同一个应用.模块.数据时是否产生隐藏的并发问题,如内存泄漏.线程锁.资源争用问题. 站在性能 ...

随机推荐

  1. [转]web使用Quartz.NET实现作业调度

    转自 https://www.cnblogs.com/best/p/7658573.html 一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的作业调度框架,是 OpenSym ...

  2. 02.CSS选择器-->:focus

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. MySQL数据库(8)----表的创建、删除、索引和更改

    MySQL允许使用 CREATE TABLE 语句和 DROP TABLE 语句来创建.删除表,使用 ALTER TABLE 语句更改其结构.CREATE INDEX 语句和 DROP INDEX 语 ...

  4. VS2013中使用Git建立源代码管理

    原文:http://blog.csdn.net/bodybo/article/details/38976549/ 第一次在VS2013中使用Git,也是第一次使用git,各种不熟悉.百度各种使用经验, ...

  5. C++创建一个新的进程

    原文:http://blog.csdn.net/hk627989388/article/details/53309865 STARTUPINFO用于指定新进程的主窗口特性的一个结构. PROCESS_ ...

  6. 然之协同系统6.4.1 SQL注入之exp编写

    前言 前面已经说明了 漏洞成因,这里介绍一下 exp 的编写. 正文 为了 getshell 或者是 任意文件下载, 我们需要修改 数据库中的 前缀sys_file 表, 所以我们的利用方式如下 使用 ...

  7. python 待关注库

    Python待关注库 GUI 图形 Tkinter/wxPython/PyGTK/PyQt/PySide Web框架 django/web2py/flask/bottle/tornadoweb/web ...

  8. maven(16)-灵活的环境构建

     多个环境 一个项目,在家的时候可能会在本机上运行,在公司可能在内网测试环境运行,上线后会在生产环境运行,在不同的环境中会有一些配置是不一样的,至少数据库就不一样.如果每换一个环境就去改所有配置太 ...

  9. CSS transitions深入理解

    到底css transition是什么,我们来看w3c的解释: CSS Transitions allow property changes in CSS values to occur smooth ...

  10. html5\CSS3有哪些新特性、移除了哪些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分HTML和HTML5?

    (1)HTML5现在已经不是SGML的子集,主要是关于图像,位置,存储,地理定位等功能的增加. 绘画canvas元素: 用于媒介回放的video和audio元素: 本地离线存储localStorage ...