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. 在js里面比较大小必须先转换成number

    利用js里面的Number函数从对象转换成数值

  2. 洛谷P4165 [SCOI2007]组队(排序 堆)

    题意 题目链接 Sol 跟我一起大喊:n方过百万,暴力踩标算! 一个很显然的思路是枚举\(H, S\)的最小值算,复杂度\(O(n^3)\) 我们可以把式子整理一下,变成 \[A H_i + B S_ ...

  3. 跨域方法:JSONP、iframe

    同源策略:浏览器出于安全考虑,会限制文档或脚本中发起的跨域请求(但src请求不受此限)资源的加载.实际上通过抓包软件可以发现请求和响应都会成功,但是响应数据并不会被浏览器加载.不同源的客户端脚本(ja ...

  4. float浮动

    float是什么意思?float是浮动,翻译成中文也是浮动意思.进入对应css手册中float手册了解float基本信息. Float常跟属性值left.right.none Float:none 不 ...

  5. Architecture And Framework

    高屋建瓴 From Up to Down. Outside into inside. Interface-Oriented Framework with dynamic configuration. ...

  6. QT5.9 新特性与版本回顾

    原文链接: http://blog.qt.io/blog/2017/05/31/qt-5-9-released 翻译内容如下,采用的是第三方某在线翻译软件,所以有些地方不是太精确,纵然大吉做了一定的调 ...

  7. Centos7 下安装Apache2 + MySQL + PHP7

    Apache 1.安装Apache yum install httpd 2.设置服务器开机自动启动Apache systemctl enable httpd.service 若要验证是否自动启动可在重 ...

  8. 微信小程序开发7-JavaScript脚本

    1.小程序的主要开发语言是 JavaScript ,开发者使用 JavaScript 来开发业务逻辑以及调用小程序的 API 来完成业务需求. 2.ECMAScript 在大部分开发者看来,ECMAS ...

  9. VUE知识day3_vue插件总结

  10. [翻译] LazyFadeInView 渐入显示text文本

    LazyFadeInView 渐入显示text文本 https://github.com/itouch2/LazyFadeInView LazyFadeInView is a cool way to ...