webdriver test1

package testweb;

import static org.junit.Assert.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;
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.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Duoxc {
public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");

@Test
public void test() {
System.out.println(Thread.currentThread().getId()+"-----------------------"+sf.format(new Date()));//打印线程启动时间

DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

//设置,用来匹配node中要使用的浏览器
capability.setBrowserName("internet explorer");
capability.setVersion("9");
capability.setPlatform(Platform.WINDOWS);

WebDriver driver = null;

//设置本地驱动,如果你实例化Driver的时候是"WebDriver driver = new InternetExplorerDriver(capability)"这种方式,就必须设置
System.setProperty("webdriver.ie.driver","D:\\iedriver\\IEDriverServer.exe");

try{
driver = new InternetExplorerDriver(capability);//本地测试,非远程方式
// driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);//远程方式,想用多线程需要这么写
System.out.println(Thread.currentThread().getId()+"访问网页开始时间:"+sf.format(new Date()));

driver.get("https://www.zyxr.com");//打开网页

try {
//等待页面打开,超时设置3秒
WebElement username = new WebDriverWait(driver, 3).until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.cssSelector("input.pc-register-input.login-phone"));
}
});
if(null==username){
System.out.println(Thread.currentThread().getId()+" Visit https://dev.zyxr.com/login.html Timeout !!!");
this.CaptureScreenshot(Thread.currentThread().getId()+"visit", driver);
driver.quit();
Thread.currentThread().interrupt();
}else{
System.out.println(Thread.currentThread().getId()+"访问中业官网:"+sf.format(new Date()));

username.clear();//清除username的value
username.sendKeys("13999999998");//设置username的value

WebElement pwd = driver.findElement(By.cssSelector("input.pc-register-input.login-psd"));
pwd.clear();
pwd.sendKeys("123456");

WebElement submit = driver.findElement(By.cssSelector("button.register-bottom-btn"));

System.out.println(Thread.currentThread().getId()+"登录开始时间:"+sf.format(new Date()));
submit.click();//提交

try {
//等待登录成功,超时设置3秒
WebElement login_flag = new WebDriverWait(driver, 3).until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.linkText("退出"));
}
});
if(null==login_flag){
System.out.println(Thread.currentThread().getId()+" Loign Timeout !!!");
this.CaptureScreenshot(Thread.currentThread().getId()+"login", driver);
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()));
login_flag.click();

try {
//等待打开收件箱页面,超时设置3秒
Boolean flag = new WebDriverWait(driver, 3).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.id("_mail_button_4_114")) != null;
}
});
if(!flag){
System.out.println(Thread.currentThread().getId()+" Open inBox page Timeout !!!");
this.CaptureScreenshot(Thread.currentThread().getId()+"openInboxPage", driver);
driver.quit();
Thread.currentThread().interrupt();
}else{
System.out.println(Thread.currentThread().getId()+"打开收件箱时间:"+sf.format(new Date()));
driver.quit();
}
} catch (Exception e) {
System.out.println(Thread.currentThread().getId()+" Open inBox page Error !!!");
e.printStackTrace();
this.CaptureScreenshot(Thread.currentThread().getId()+"openInboxPage", driver);
driver.quit();
Thread.currentThread().interrupt();
}
}
} catch (Exception e) {
System.out.println(Thread.currentThread().getId()+" Loign email Error !!!");
e.printStackTrace();
this.CaptureScreenshot(Thread.currentThread().getId()+"login", driver);
driver.quit();
Thread.currentThread().interrupt();
}
}
} catch (Exception e) {
System.out.println(Thread.currentThread().getId()+" Visit http://mail.163.com/ Error !!!");
e.printStackTrace();
this.CaptureScreenshot(Thread.currentThread().getId()+"visit", driver);
driver.quit();
Thread.currentThread().interrupt();
}
}catch (Exception e) {
e.printStackTrace();
this.CaptureScreenshot(Thread.currentThread().getId()+"visit", driver);
driver.quit();
}finally{
if(null!=driver){
driver.quit();
}
}



}


/**
* 截屏方法
* @param fileName
* @param driver
*/
public void CaptureScreenshot(String fileName, WebDriver driver) {
String dirName = "d:/screenshot";
if (!(new File(dirName).isDirectory())) {
new File(dirName).mkdir();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String time = sdf.format(new Date());
TakesScreenshot tsDriver = (TakesScreenshot) driver;
File image = new File(dirName + File.separator + time + "_" + fileName==null?"":fileName + ".png");
tsDriver.getScreenshotAs(OutputType.FILE).renameTo(image);
}

}

posted @ 2016-10-09 19:32  我爱周慧敏  阅读(156)  评论(0)    收藏  举报