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