public class TestThree {
WebDriver driver;
String url1="http://www.sogou.com";
@BeforeMethod
public void beforeTest(){
System.setProperty("webdriver.firefox.bin","D:\my program\firefox\firefox.exe");
driver = new FirefoxDriver();
}
//使用javascript语句进行页面元素的单击
@Test
public void testHandleiFrame() throws Exception{
driver.get(url1);
WebElement searchInputBox = driver.findElement(By.id("query"));
WebElement searchButton = driver.findElement(By.id("stb"));
searchInputBox.sendKeys("使用;
;
}
public void throws Exception{
try{
if(ele.isEnabled() && ele.isDisplayed()){
System.out.println("使用;
((;
}else{
System.out.println("页面上的元素无法单击");
}
}catch(StaleElementReferenceException e){
System.out.println("页面上的元素没有附加在网页"+e.getStackTrace());
}catch(NoSuchElementException e){
System.out.println("页面中没有找到要操作的元素"+e.getStackTrace());
}catch(Exception e){
System.out.println("无法完成单击动作"+e.getStackTrace());
}
}
//在Ajax方式产生的浮动框中,单击选择包含某个关键词的选项
@Test
public void testAjaxDivOption(){
driver.get(url1);
WebElement searchInputBox = driver.findElement(By.id("query"));
searchInputBox.click();
List suggestionOptions = driver.findElements(By.xpath("//*[@id='v1']/div/ul/li[3]"));
System.out.println(suggestionOptions);
for(WebElement ele:suggestionOptions){
if(ele.getText().contains("世界最帅13岁正太")){
System.out.println(ele.getText());
ele.click();
break;
}else{
System.out.println("没有找到相关信息");
}
}
}
//设置一个页面对象的属性值
@Test
public void testAttribute(){
driver.get(url1);
WebElement textInput = driver.findElement(By.id("query"));
setAttribute(driver,textInput,"value","文本框的长度属性已被修改");
setAttribute(driver,textInput,"size","10");
removeAttribute(driver,textInput,"size");
}
public void setAttribute(WebDriver driver,WebElement ele,String attributeName,String value){
JavascriptExecutor js = (driver;
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", ele,attributeName,value);
}
public void removeAttribute(WebDriver driver,WebElement ele,String attributeName){
JavascriptExecutor js = (driver;
js.executeScript("arguments[0].removeAttribute(arguments[1],arguments[2])", ele,attributeName);
}
//在日期选择器上进行选择
@Test
public void testDatePicker(){
driver.get(url1);
WebElement dateInputBox = driver.findElement(By.id("datepicker"));
dateInputBox.sendKeys("12/31/2015");//如果不支持输入,可通过改变控件的属性为可编辑,然后输入
}
//使用sendKeys上传附件
@Test
public void testUploadFile() throws Exception{
WebElement fileInputBox = driver.findElement(By.id("file"));
fileInputBox.sendKeys("d:\a.txt");
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("filesubmit")));
WebElement fileSubmitBtn = driver.findElement(By.id("filesubmit"));
fileSubmitBtn.click();
wait.until(ExpectedConditions.titleContains("文件上传成功"));
}
//使用robot对象操作键盘
@Test
public void
testRobotOperateKeyboard(){
driver.get(url1);
WebDriverWait wait = new
WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("query")));
setAndctrlVCboardData("测试");
pressTabKey();
pressEnterKey();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void
setAndctrlVCboardData(String str){
StringSelection ss = new
StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,
null);
Robot robot = null;
try{
robot =
new Robot();
}catch(AWTException e){
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
}
public void
pressTabKey(){
Robot robot = null;
try{
robot =
new Robot();
}catch(AWTException e){
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
public void
pressEnterKey(){
Robot robot = null;
try{
robot =
new Robot();
}catch(AWTException e){
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
@AfterMethod
public void
afterMethod() {
driver.quit();
}
}