博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webdriver test1
阅读量:4683 次
发布时间:2019-06-09

本文共 5350 字,大约阅读时间需要 17 分钟。

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

}

转载于:https://www.cnblogs.com/viktor520/p/5943503.html

你可能感兴趣的文章
linux sdcv命令
查看>>
BZOJ4836: [Lydsy1704月赛]二元运算【分治FFT】【卡常(没卡过)】
查看>>
MPU6050开发 -- 数据分析(转)
查看>>
springmvc入门详解
查看>>
用户名、密码等15个常用的js正则表达式
查看>>
对比多层字典是否相同函数
查看>>
你在哪编程?你的程序原料是什么?
查看>>
ehcache 简介
查看>>
java uuid 例子
查看>>
linux zip 压缩密码
查看>>
【SICP练习】26 练习1.32
查看>>
Centos下安装破解Jira7的操作记录
查看>>
Python AES_ECB_PKCS5加密代码
查看>>
SpringBoot--外部配置
查看>>
C#中的线程三 (结合ProgressBar学习Control.BeginInvoke)
查看>>
sqlserver工作日常使用sql--持续完善中
查看>>
文件I/O与标准I/O
查看>>
大数据学习之路(持续更新中...)
查看>>
项目开发总结报告(GB8567——88)
查看>>
enumerate使用
查看>>