selinium如何多线程_Selenium&Webdriver远程测试和多线程并发测试

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() {

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() {

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值