`
shijunjuan
  • 浏览: 46721 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

搭建Selenium Grid压力测试环境

阅读更多
  1. In one machine, run registry file MaxUserPort.reg
  2. Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters]
    "EnableICMPRedirect"=dword:00000000
    "TcpTimedWaitDelay"=dword:0000001e
    "MaxUserPort"=dword:0000ea60
    "DisableIPSourceRouting"=dword:00000002
    "EnableDeadGWDetect"=dword:00000000
    "KeepAliveTime"=dword:000493e0
    "TcpMaxDataRetransmissions"=dword:00000004
     
         
/**
 * @author junjshi, created on Sep 3, 2013
 * 
 */


import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.testng.annotations.Test;

import com.ebay.maui.controller.TestPlan;
@Test
public class GridTest extends TestPlan{
	private static String browser="firefox";
	
	private static final int threadPoolSize = 200;
	private static int count = 0;
	private static String profilePath = "C:\\grid\\profile\\customProfileDirCUSTFF";
	private static String url = "http://gear2-hub-8915.phx-os1.stratus.dev.ebay.com:8080/wd/hub";
	private static final int interval = 2;//minutes
	
	public static void main(String[] args) {
		ExecutorService exec = Executors.newFixedThreadPool(threadPoolSize);
		CommonTest.setUrl(url);
		CommonTest.setProfilePath(profilePath);
		CommonTest.setInterval(interval);
		Runnable task = new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						
						System.out.println("Clean "+System.getProperty("java.io.tmpdir"));
						File tempDir = new File(System.getProperty("java.io.tmpdir"));
						CommonTest.deleteDirectory(tempDir);
						System.out.println("Executing thread #"+ count++);
						new CommonTest().testGrid(browser);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		};

		for (int i = 0; i < threadPoolSize; i++) {
			exec.execute(task);
		}
	}

	

}
 
 CommonTest.java 
import java.io.File;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.openqa.selenium.WebDriver;

import com.ebay.maui.driver.web.WebUXDriver;

/**
 * @author junjshi, created on Dec 9, 2013
 * 
 */

public class CommonTest {
	private static String profilePath = "C:\\grid\\profile\\customProfileDirCUSTFF";
	private static String url = "http://gear2-hub-8915.phx-os1.stratus.dev.ebay.com:8080/wd/hub";
	private static int interval = 2;//minutes
	
	
	
	public static void setInterval(int interval) {
		CommonTest.interval = interval;
	}

	public static String getProfilePath() {
		return profilePath;
	}

	public static void setProfilePath(String profilePath) {
		CommonTest.profilePath = profilePath;
	}

	public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		CommonTest.url = url;
	}

	public static int getInterval() {
		return interval;
	}

	public void runTest(String url) throws MalformedURLException{
		WebUXDriver.getWebUXDriver().setMode("ExistingGrid");
		WebUXDriver.getWebUXDriver().setHubUrl(url);
		WebUXDriver.getWebUXDriver().setFfProfilePath(profilePath);
		WebDriver driver = WebUXDriver.getWebDriver(true);
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		System.out.println(driver.getTitle());
		try{
			driver.quit();
		}catch(Exception ex){}
	}
	
	public void runChromeTest(String url) throws MalformedURLException{

		WebUXDriver.getWebUXDriver().setBrowser("chrome");
		WebUXDriver.getWebUXDriver().setMode("ExistingGrid");
		WebUXDriver.getWebUXDriver().setHubUrl(url);
		WebDriver driver = WebUXDriver.getWebDriver(true);
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		driver.get("http://www.qa.ebay.com");
		System.out.println(driver.getTitle());
		try{
			driver.quit();
		}catch(Exception ex){}
	}
	
	public void runIETest(String url) throws MalformedURLException{

		WebUXDriver.getWebUXDriver().setBrowser("iexplore");
		WebUXDriver.getWebUXDriver().setMode("ExistingGrid");
		WebUXDriver.getWebUXDriver().setHubUrl(url);
		WebDriver driver = WebUXDriver.getWebDriver(true);
		driver.get("http://www.qa.ebay.com");
		System.out.println(driver.getTitle());
		try{
			driver.quit();
		}catch(Exception ex){}
	}
	
	public void testGrid(String browser) throws MalformedURLException, InterruptedException{
		ExecutorService exec = Executors.newCachedThreadPool();    
        int timeout =  60 * 30;
        final String browserName = browser;
        Callable<Boolean> task = new Callable<Boolean>(){

			@Override
			public Boolean call() throws Exception {
				if(browserName.equalsIgnoreCase("chrome")){
					runChromeTest(url);
					return Boolean.TRUE;  
				}else if(browserName.equalsIgnoreCase("firefox")){
					runTest(url);
					return Boolean.TRUE;
				}else
				{
					runIETest(url);
					return Boolean.TRUE;	
				}
			}
	    };
	    Calendar start = Calendar.getInstance();
	    start.setTime(new Date());
        Future<Boolean> future = exec.submit(task);    
        Boolean taskResult = null;    
        String failReason = null;    
        try {    
            taskResult = future.get(timeout, TimeUnit.SECONDS);    
        } catch (InterruptedException e) {    
            failReason = "Main thread was interrupted when waiting for computing result";    
        } catch (ExecutionException e) {    
            failReason = "Main thread is wating for result, but excpetion throw.";  
            e.printStackTrace();
        } catch (TimeoutException e) {    
            failReason = "Main thread is wating for result, but time out.";    
            exec.shutdownNow();    
        }   
        System.out.println(taskResult+";"+failReason);
        Calendar end = Calendar.getInstance();
	    end.setTime(new Date());
	    long duration = (end.getTimeInMillis()-start.getTimeInMillis())/1000/60;
	    System.out.println("duration is "+duration);
        if(duration<interval){
        	System.out.println("Wating ..."+ String.valueOf(interval-duration)+" minutes");
        	Thread.sleep(1000*(interval-duration)*60);
        }
	}	
	
	public static boolean deleteDirectory(File path) {
		if (path.exists()) {
			File[] files = path.listFiles();
			if(files==null)return true;
			
			for (int i = 0; i < files.length; i++) {
				if(files[i].lastModified()> Calendar.getInstance().getTimeInMillis()-1000*60*60)
					continue;
				if (files[i].isDirectory()) {
					
					deleteDirectory(files[i]);
				} else {
					files[i].delete();
				}
			}
		}
		return path.delete();
	}
}
 
with MAUI dependency,(eBay开发的自动测试框架,读者可以用WebDriver开发) Run as JAVA application with VM parameter: -Xmx1024M
  1. Set the thread count and hub url and FirefoxProfile path as you need.
  2. Need to pay attention to folder C:\Users\stack\AppData\Local\Temp, it might increase very fast, clean it before testing on node machine.
    Add task to clean up the temp folder.
    cleanup.ps1

    Set-StrictMode -Version Latest
    # Function to remove all empty directories under the given path. If -DeletePathIfEmpty is provided the given Path directory will also be deleted if it is empty.
    function Remove-EmptyDirectories([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty)
    {
        Get-ChildItem -Path $Path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
        # If we should delete the given path when it is empty, and it is a directory, and it is empty, then delete it.
        if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null) { Remove-Item -Path $Path -Force }
    }
    # Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind.
    function Remove-FilesCreatedBeforeDate([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$true)][DateTime] $DateTime, [switch] $DeletePathIfEmpty)
    {
        Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DateTime } | Remove-Item -Force
        Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty
    }
    # Function to remove all files in the given Path that have not been modified after the given date, as well as any empty directories that may be left behind.
    function Remove-FilesNotModifiedAfterDate([parameter(Mandatory=$true)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory=$true)][DateTime] $DateTime, [switch] $DeletePathIfEmpty)
    {
        Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $DateTime } | Remove-Item -Force
        Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty
    }
    # Delete all files that have not been updated in 1 hour.
    Remove-FilesNotModifiedAfterDate -Path "C:\Users\stack\AppData\Local\Temp" -DateTime ((Get-Date).AddHours(-1))

    cleanup.bat

    cd c:\grid
    powershell .\cleanup.ps1

    Add windows task CleanUpTempFolder:



  3. Because stress testing brought a big burden to node machine, so it might occupied too much CPU & Memory in node machine, make node machine can't create driver successfully.
    So change the c:\grid\service\watchdog\watchdog.config file, change run_time to 5, default is 15. And delete watchdog.log file.

    process_list=chrome.exe,iexplore.exe,firefox.exe,chromedriver.exe,IEDriverServer.exe
    run_time=3
    poll_time=1
  4. Monitoring tool:
    1. jvisualvm

    2. netstat -na (check connection situation)
  5. Problems we might found:
    org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Could not get document from window handle

    chrome crash
    Solution: kill all related processes, clean the temp folder, restart selenium node service

 

分享到:
评论

相关推荐

    Playwright 结合 Selenium Grid - windows 环境使用教程.pdf

    本资源提供了 Playwright 结合 Selenium Grid 在 Windows 环境中的使用教程,介绍了如何将 Playwright 连接到 Selenium Grid Hub,启动 Google Chrome 或 Microsoft Edge 浏览器,並在远程机器上执行自动化测试。...

    Mac OS搭建Python+selenium自动化测试环境

    Mac OS搭建Python+selenium自动化测试环境 1、官网下载安装python 访问Python官网 https://www.python.org/downloads,下载安装Python安装包,一路点击安装即可。 验证是否安装成功 打开终端,输入以下命令,查看...

    selenium测试环境搭建

    ### Selenium测试环境搭建详解 #### 一、概述 在当今快速发展的软件开发领域,自动化测试已成为提高产品质量和开发效率不可或缺的一部分。Selenium作为一种强大的开源工具,被广泛应用于Web应用程序的功能测试之中...

    selenium3+python测试环境搭建

    ### selenium3+python测试环境搭建 #### 一、概述 Selenium是一个强大的自动化测试框架,主要用于Web应用程序的自动化测试。它支持多种编程语言(如Java、C#、Python等),并且能够与各种主流浏览器(如Chrome、...

    selenium grid最新版本

    1. **分布式测试**:Selenium Grid的核心功能是支持跨多个主机和浏览器实例同时运行测试。这极大地提高了测试的并行性,减少了整体测试时间,尤其对于大型项目或需要跨平台、跨浏览器测试的场景,效率提升显著。 2....

    Selenium终极自动化测试环境搭建(一):Selenium+Eclipse+Junit+TestNG.docx

    Selenium 终极自动化测试环境搭建 Selenium 是一个功能强大且流行的自动化测试工具,广泛应用于 Web 应用程序的自动化测试中。下面是 Selenium 终极自动化测试环境搭建的详细步骤: 一、安装 JDK JDK 是 Java ...

    selenium gird 简介

    Selenium Grid 是一个强大的工具,用于扩展 Selenium WebDriver 的功能,特别是在进行分布式自动化测试时。它允许用户在多台机器上并行运行测试,显著提高了测试覆盖率和效率,减少了整体测试时间。下面将详细介绍 ...

    selenium grid教程

    4. **未来趋势**:随着云计算技术的发展,Selenium Grid 可能会更多地应用于云测试环境中,实现更加灵活和高效的测试资源分配。 #### 三、安装与配置 1. **环境准备**: - 需要安装 Ant 和 JDK。这些工具通常用于...

    java selenium 自动化测试环境搭建 配套软件 Firefox53 浏览器 配套驱动

    Java Selenium自动化测试环境的搭建是软件测试领域中的一个重要环节,特别是在Web应用的测试中。Selenium是一个强大的浏览器自动化工具,支持多种编程语言,包括Java,它使得开发者能够编写可跨浏览器运行的自动化...

    Python + selenium搭建自动化测试环境

    本文将深入探讨如何使用Python结合Selenium搭建自动化测试环境,并涉及PyCharm的使用以及相关库的安装。 首先,我们需要安装Python。Python是一种高级编程语言,具有简洁的语法和丰富的第三方库,是自动化测试的...

    python+selenium自动化测试环境搭建软件工具(整套)

    Python+Selenium自动化测试环境搭建是现代软件开发过程中不可或缺的一部分,特别是在进行Web应用的质量保障时。Selenium是一款强大的、跨平台的Web自动化测试框架,它允许开发者编写脚本模拟用户在浏览器上的各种...

    python+selenium+unittest环境搭建1

    Python+Selenium+Unittest环境搭建是自动化测试中的一种重要组件,本文将详细介绍如何搭建Python+Selenium+Unittest环境,并解决在搭建过程中可能遇到的问题。 一、安装Python 在安装Python时,需要勾选将Python...

    Selenium+Python测试环境搭建教程

    ### Selenium+Python自动化测试环境搭建知识点详解 #### 一、安装Python - **访问Python官方网站**:为了确保获得最新的Python版本及其更新,建议从官方站点(https://www.python.org/downloads/)下载Python...

    Selenium:Selenium Grid.zip

    史上最全软件测试技术全套教程,包括: Postman Selenium 单元测试 压力测试 回归测试 安全测试 性能测试 测试工具 集成测试 等流行技术的系列教程

    selenium-server-grid(包含版本3.14和4.0.0)

    Selenium Grid允许我们在多台计算机上并行运行测试,并集中管理不同的浏览器版本和浏览器配置(而不是在每个单独的测试中)。 Selenium Grid解决了一些常见的委派和分发问题,但是例如将无法管理您的基础结构。

    phpunit+selenium测试环境搭建浅谈

    这可能包括安装PHP、Composer、`phpunit`、`selenium-server`(或`selenium grid`,如果需要在多台机器上并行测试)、以及相应的浏览器驱动程序。安装过程中,要注意版本兼容性,因为不同版本的`phpunit`、`selenium...

    python+selenium自动化测试环境搭建

    总结一下,搭建 Python+Selenium 自动化测试环境的步骤包括: 1. 安装 Python 并配置环境变量。 2. 安装 `setuptools`。 3. 安装 Selenium 库。 4. 安装 IEDriverServer 以支持 IE。 5. 根据需要,安装额外的库如 `...

    Selenium自动化测试

    Selenium自动化测试的主要内容可以分为几个部分,包括Selenium简介和基础、Selenium-IDE、Selenium命令、Selenium-R、Selenium测试案例设计、Selenium用户扩展、Selenium-Grid以及Selenium 2.0和WebDriver等内容。...

Global site tag (gtag.js) - Google Analytics