JavascriptExecutor js =(JavascriptExecutor)driver;

String title=js.executeScript("return document.title").toString();//执行js脚本


driver.get("http://jqueryui.com/recources/demos/draggable/scroll.html");

WebElement dragable=driver.findElement(By.xpath("//img[@alt='jQuery UI in Action by TJ VanToll']"));

for(int i=0;i<5;i++){

new Actions(driver).dragAndDropBy(dragable, 0, 10).build().perform();//拖曳元素操作

}

Actions action = new Actions(driver);

action.contextClick(driver.findElement(By.id("query"))).perform();//右键事件

action.moveToElement(driver.findElement(By.id("query"))).perform();//移到制定元素上

action.clickAndHold(driver.findElement(By.id("query"))).perfrom();//点击然后Hole住

Thread.sleep(2000);Hole住两秒

action.release(driver.findElement(By.id("query"))).perfrom();//放开左键释放




WebElement input = driver.findElement(By.id("id"))

input.getAttribute("value");获取value属性值

input.getCssValue("width");获取CSS属性值


driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);//隐式等待10秒,不推荐使用,设了10秒,即使5秒后元素出现,也必须等待10秒???


    WebDriverWait wait=new WebDriverWait(driver,10);//显示等待10s

    wait.until(ExpectedConditions.elementToBeClickable(By));//元素是否可被点击的

    wait.until(ExpectedConditions.elementToBeSelected(By));//元素是否可被选中

    wait.until(ExpectedConditions.presenceOfElementLocated((By))元素是否存在

    wait.until(ExpectedConditions.textToBePresentInElement(locator, text))

    wait.until(ExpectedConditions.titleContains(title));//页面title是否含有指定字符

PS:只有满足显示等待的条件要求,测试代码才会向后执行,当显示等待条件未被满足时,未一直停在那直到阈值时间结束,超出阈值时间,会抛出异常


自定义显示等待

wait.until(new ExpectedConditions<WebElement>(){


@Override

public WebElement apply(WebDriver d){
return d.findElement(By.id("id"))
}}//元素


wait.until(new ExpectedConditions<Boolean>(){


@Override

public Boolean apply(WebDriver d){
return d.findElement(By.id("id")).isDisplayed();
}}//返回布尔类型



private boolean IsElementPreaent(By by){

try{

driver.findElement(by);

return true;

}catch (NoSuchElementException e){

return false;

}

}

//增加一个判断页面元素是否存在的函数



String parentWindowHandle=driver.getWindowHandle();

driver.switchTo().window(WindowHandle).getTitle();//操作新弹出的窗口


Alert alert=driver.switchTo().alert();//操作alert弹窗

alert.getText();获取文字内容

alert.accept();//确定


Alert alert=driver.switchTo().alert();//操作confirm弹窗

alert.getText();获取文字内容

alert.accept();//确定

alert.dismiss();//取消



Alert alert=driver.switchTo().alert();//操作prompt弹窗

alert.getText();获取文字内容

alert.sendKeys("");//输入值

alert.accept();//确定

alert.dismiss();//取消


操作Frame:

frameset:frameset.html--包含frame1,frame2,frame3

<frame id="frame1" src="frame1.htm"/>

<frame id="frame2" src="frame2.htm"/>

<frame id="frame3" src="frame3.htm"/>

frame1:frame1.html

frame2:frame2.html

frame3:frame3.html


driver.switchTo().frame("frame1")//进入frame1 (id)

driver.switchTo().defaultContent();//返回到frameset页面,如果不执行,无法进入到其它页面

driver.switchTo().frame("frame2")//进入frame2

driver.switchTo().defaultContent();//返回到frameset页面,如果不执行,无法进入到其它页面

driver.switchTo().frame("frame3")//进入frame3

driver.switchTo().defaultContent();//返回到frameset页面,如果不执行,无法进入到其它页面


操作浏览器cookie:

Set<Cookie> cookies = driver.manage().getCookies();//获取cookies 集合

System.out.println(String.format("Domain->name->value->expiry->path"));

for(Cookie cookie:cookies){

System.out.println(String.format("%s->%s->%s->%s->%s",cookie.getDomain(),cookie.getName(),cookie.getValue(),cookie.getExpiry(),cookie.getPath()));//打印出cookie 所在域、name、value、有效日期、路径

}

删除cookie3种方式:

Cookie newCookie=new Cookie("cookieName","cookieValue");

driver.manage().deleteCookieNamed("cookieName");通过Cookie的name属性

driver.manage().deleteCookie(newCookie);通过Cookie对象

driver.manage().deleteAllCookies();全部删除