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. Java 类 ThreadLocal 本地线程变量

    前言:工作中将要使用ThreadLocal,先学习总结一波.有不对的地方欢迎评论指出. 定义 ThreadLocal并不是一个Thread,而是Thread的局部变量.这些变量不同于它们的普通对应物, ...

  2. Luogu3804:[模板]后缀自动机

    题面 luogu Sol \(sam\)然后树形\(DP\) 当时还不会拓扑排序的我 # include <bits/stdc++.h> # define IL inline # defi ...

  3. 利用Metaweblog技术的API接口同步到多个博客网站(详细)

    很早就有这个想法:自己有时候会用到多个博客,有些博客在一个网站上写完之后,要同步到其他博客网站,自己只能复制粘贴,感觉特别没意思,复制粘贴的麻木了.一直在想有哪些技术能实现一次写博,多站同步.最近网上 ...

  4. 文件路径太长无法删除 robocopy

    方法一:新建空文件夹 D:\temp robocopy D:\temp D:\target\source\test  /purge 或者在同一目录下,建一个空文件夹, test 在cmd下切换进入到当 ...

  5. Remove Duplicates from Sorted List 去除链表中重复值节点

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. Linux扩展根目录下的空间

    自己通过root创建了一个新用户,然而当我使用这个新用户时发现,/home/my中的空间只有几十M,完全不能满足我的使用,所以通过下面的方法扩展根下的空间. 我的本次操作,参考于 http://www ...

  7. C语言二级指针(指向指针的指针)

    转载:http://c.biancheng.net/cpp/html/85.html 指针可以指向一份普通类型的数据,例如 int.double.char 等,也可以指向一份指针类型的数据,例如 in ...

  8. java indexOf 和 split的用法

    1.java 的 indexOf 方法 ,如果存在 则 指定的字符串的开始位置,如果不存在 则返回-1: 2.java 的 split的方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. ...

  9. ZOJ Problem Set – 2321 Filling Out the Team

    Time Limit: 2 Seconds      Memory Limit: 65536 KB Over the years, the people of the great city of Pi ...

  10. java笔记--增加虚拟机内存

    --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877243.html "谢谢-- 为避免大型应用程序因虚拟机内存不足而无法 ...