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