webdriver3

Selenium2.0之WebDriver学习总结(3)  

来自lvzeting   2012-03-06 10:27:25|  分类: 自动化测试|字号 订阅

 
 

接下来将通过几个具体的例子来使用操作上述功能,以下代码都是通过junit框架编写。

例子1:该例子主要是登录网易相册,创建一个私人相册,并进入相册进行评论,然后删除这个相册。在这个例子中,可以找到各种方式定位元素方法的具体实例:

package demo;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

publicclassDemoElement1{

privateWebDriver driver;
privateString baseUrl;

@Before
publicvoid setUp()throwsException{
driver =newFirefoxDriver();
baseUrl ="http://photo.163.com/";
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}

@Test
publicvoid test()throwsException{
//打开一个网址
driver.get(baseUrl);
//输入用户名
//by css
driver.findElement(By.cssSelector("input[name=\"username\"]")).clear();
driver.findElement(By.cssSelector("input[name=\"username\"]")).sendKeys("demophoto");
driver.findElement(By.cssSelector("div.js-cur")).click();
//输入密码
//by name
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("qa1234");
//点击登录
//by id
driver.findElement(By.id("photo_index_login")).click();
//获取页面title
//by js
JavascriptExecutor js =(JavascriptExecutor) driver;
String title =(String)js.executeScript("return document.title");
System.out.println(title);
//点击 创建相册
//by linkText
driver.findElement(By.linkText("创建相册")).click();
//输入 相册名称
//by name
driver.findElement(By.name("name")).clear();
driver.findElement(By.name("name")).sendKeys("new album");
//选择私人相册
//by id
driver.findElements(By.name("auth")).get(3).click();
//选择aaa分类
//by css
Selectselect=newSelect(driver.findElement(By.cssSelector("select.fc5.bdwa")));
//选择的三种不同实现
//select.selectByValue("aaa");
//select.selectByIndex(1);
select.selectByVisibleText("aaa");
//点击 创建 两种方式
//by css
driver.findElement(By.cssSelector("button.ui-btn.ui-btn-sub0")).click();
//driver.findElement(By.cssSelector("button.ui-btn.ui-btn-sub0")).submit();
Thread.sleep(5000);
//输入评论
//因为评论框放在一个iframe中,所以要切换到iframe中,结束后将焦点切换回去
//by tagName
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
driver.switchTo().activeElement().sendKeys("评论内容!");
driver.switchTo().defaultContent();
//点击 发表
//by className
driver.findElement(By.className("ui-btn-main0")).click();
//点击 返回
//by xpath
driver.findElement(By.xpath("//a[@class='back' and (text()='<< 返回')]")).click();
//鼠标hover到相册上(目前firefoxDriver的鼠标事件还是有缺陷的,运行这代码的时候请确保光标在浏览器内)
//by partialLinkText
newActions(driver).moveToElement(driver.findElement(By.partialLinkText("new album"))).build().perform();
//点击 删除
//by linkText
driver.findElement(By.linkText("删除")).click();
//点击确认
driver.findElement(By.cssSelector("button.ui-btn.ui-btn-sub0")).click();
//退出登录
//by id
driver.findElement(By.id("headerLogout")).click();
//浏览器后退
driver.navigate().back();
}

@After
publicvoid tearDown()throwsException{
driver.quit();
}

}


例子2:该主要是登录网易群相册上传修改个人头像。在这个例子中,可以找到如何通过上传空间进行上传操作:

package demo;

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

publicclassDemoElement2{
privateWebDriver driver;
privateString baseUrl;
@Before
publicvoid setUp()throwsException{
driver =newFirefoxDriver();
baseUrl ="http://photo.163.com/";
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
publicvoid test1111()throwsException{
driver.get(baseUrl +"/q/690000");
driver.findElement(By.linkText("[马上登录]")).click();
driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys("demophoto");
driver.findElement(By.cssSelector("div.js-cur")).click();
driver.findElement(By.cssSelector("td > input[name=\"password\"]")).clear();
driver.findElement(By.cssSelector("td > input[name=\"password\"]")).sendKeys("qa1234");
driver.findElement(By.name("login")).click();
driver.findElement(By.id("J-set-account")).click();
File file =newFile(".\\res\\1.jpg");

//获取需要上传照片的绝对地址
String filepath = file.getCanonicalPath();
//上传控件操作
driver.findElement(By.id("J-account-file")).sendKeys(filepath);
Thread.sleep(10000);
driver.findElement(By.cssSelector("span.inside")).click();
driver.findElement(By.cssSelector("button.ui-btn.ui-btn-sub0")).click();
driver.findElement(By.linkText("返回>>")).click();
driver.findElement(By.linkText("退出")).click();
}
@After
publicvoid tearDown()throwsException{
driver.quit();
}

}

 
例子3 :该主要是登录网易群相册进行拖动排序。通过这个例子我们可以学习如何实现浏览器中鼠标拖动事件:

package demo;
import java.util.concurrent.TimeUnit;
import org.junit.*;
importstatic org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

publicclassDemoElement3{
privateWebDriver driver;
privateString baseUrl;
privateStringBuffer verificationErrors =newStringBuffer();
@Before
publicvoid setUp()throwsException{
driver =newFirefoxDriver();
baseUrl ="http://photo.163.com/";
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}

@Test
publicvoid test()throwsException{
driver.get(baseUrl +"/");
driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys("demophoto");
driver.findElement(By.cssSelector("div.js-cur")).click();
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("qa1234");
driver.findElement(By.id("photo_index_login")).click();
driver.findElement(By.partialLinkText("排序相册")).click();
driver.findElement(By.cssSelector("#J-photoListSort > span.title")).click();
driver.findElement(By.linkText("自定义排序")).click();
//拖动图片
(newActions(driver)).dragAndDrop(driver.findElement(By.id("7515773989")), driver.findElement(By.id("7515803792"))).perform();
driver.findElement(By.linkText("确定排序")).click();
Thread.sleep(10000);
driver.findElement(By.cssSelector("#J-photoListSort > span.title")).click();
driver.findElement(By.linkText("新相片在前")).click();

}

@After
publicvoid tearDown()throwsException{
driver.quit();
String verificationErrorString = verificationErrors.toString();
if(!"".equals(verificationErrorString)){
fail(verificationErrorString);
}
}

privateboolean isElementPresent(Byby){
try{
driver.findElement(by);
returntrue;
}catch(NoSuchElementException e){
returnfalse;
}
}
}

你可能感兴趣的:(webdriver)