Alert 类介绍
? 表现形式:
1. Alert 类,是指windows弹窗的一些操作
? 主要操作:
driver.findElement(By. className ("alert")).click();
Alert alert = driver.switchTo().alert();
String text = alert.getText();
alert.accept();
? 说明:
1. 先要switch到windows弹窗上面
2. 该Alert类有二个重要的操作getTest(),取得弹窗上面的字符串,accept是指点击确定/ok类的按钮,使弹窗消失
1 public void testAlert() 2 { 3 // Driver.findElement(By.xpath(".//input[@onclick=‘duihua()‘]")).click(); 4 // String message=alert.getText(); 5 // System.out.println(message); 6 WebElement element=Driver.findElement(By.xpath(".//input[@onclick=‘duihua()‘]")); 7 Actions action=new Actions(Driver); 8 action.click(element).perform(); 9 Alert alert=Driver.switchTo().alert(); 10 alert.accept(); 11 }
Action类介绍
? 表现形式:
1. 一般是在要触发一些js函数或者一些JS事件时,要用到这个类
2. <input class="over" type="button" onmouseout="mouseOut()" onmouseover="mouseOver()" value="Action">
? 主要操作:
WebElement element = driver.findElement(By.className("over"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
? 说明:
1. 先要new一个Actions的类对象
2. 主要操作大家可以自已去看下API,但是最后的perform()一定要加上,否则执行不成功,切记!
1 public void testAction() 2 { 3 WebElement element=Driver.findElement(By.className("over")); 4 Actions action=new Actions(Driver); 5 action.moveToElement(element).perform(); 6 String actionmessage=Driver.findElement(By.id("over")).getText(); 7 System.out.print(actionmessage); 8 9 }
上传文件操作
? 表现形式: 1. 在html中表现为:<input id="load" type="file">
? 说明:
1. 一般是把路他径直接sendKeys到这个输入框中
2. 如果输入框被加了readonly属性,不能输入,则需要用JS来去掉readonly属性!
1 public void testUpload() 2 { 3 //autoit 4 WebElement element=Driver.findElement(By.id("uploadfile")); 5 String filePath="C:\\Users\\zhanghm\\Desktop\\生产环境进行订单支付时的小技巧.docx"; 6 element.sendKeys(filePath); 7 System.out.print("overload"); 8 }
调用JS介绍
? 目的:
1. 执行一段JS,来改变HTML
2. 一些非标准控件无法用selenium2的API时,可以执行JS的办法来取代
? 主要操作:
JavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("alert(‘hellow rold!‘)");
? 说明:
1. executeScript这个方法的参数为字符串,为一段JS代码
2. 注意,JS代码需要自已根本项目的需求来编写!
1 public void testJs() 2 { 3 JavascriptExecutor j=(JavascriptExecutor)Driver; 4 j.executeScript("alert(‘helloworld‘)"); 5 6 }
Iframe操作
? 表现形式:
1. 在HTML中为:<iframe width="800" height="330" frameborder="0" src="./demo1.html" name="aa">
? 主要操作: driver.switchTo().frame("aa");
? 说明:
1. 如果iframe标签有能够唯一确定的id或者name,就可以直接用id或者name的值:driver.switchTo().frame("aa");
2. 如果iframe标签没有id或者name,但是我能够通过页面上确定其是第几个(也就是通过index来定位iframe,index是从0开 始的):driver.switchTo().frame(0);
3. 也可以通过xpath的方式来定位iframe,写法如下:
① WebElement iframe = driver.findElement(By.xpath("//iframe[@name=‘aa‘]"));
② driver.switchTo().frame(iframe);
1 public void testIframe() 2 { 3 Driver.switchTo().frame("in"); 4 Driver.findElement(By.xpath(".//input[@type=‘text‘]")).sendKeys("活到老,学到老……"); 5 Driver.switchTo().defaultContent(); 6 Driver.findElement(By.xpath(".//input[@name=‘fname‘]")).sendKeys("哎呦,不错哦"); 7 }
多窗口切换
? 表现形式:
1. 在HTML中一般为:<a class="open" target="_bank" href="http://baidu.com">Open new window</a>
? 主要操作:
Set<String> handles = driver.getWindowHandles();
driver.switchTo().window()
? 说明:
1. getWindowHandles是取得driver所打开的所有的页面的句柄;
2. switchTo是指切换到相应的窗口中去,window中的参数是指要切过去的窗口的句柄;
1 //页面跳转 2 public void testNewWindow() 3 { 4 WebElement element=Driver.findElement(By.xpath(".//a[@href=‘http://www.baidu.com‘]")); 5 element.click(); 6 // Set<String> handles=Driver.getWindowHandles(); 7 String handle=Driver.getWindowHandle(); 8 Driver.switchTo().window(handle); 9 Driver.findElement(By.xpath(".//*[@id=‘kw‘]")).sendKeys("中国好声音"); 10 Driver.findElement(By.id("su")).click(); 11 } 12 13 //网络上常用的方式 14 public boolean switchToWindow(WebDriver driver,String windowTitle){ 15 boolean flag = false; 16 try { 17 String currentHandle = driver.getWindowHandle(); 18 Set<String> handles = driver.getWindowHandles(); 19 for (String s : handles) { 20 if (s.equals(currentHandle)) 21 continue; 22 else { 23 driver.switchTo().window(s); 24 if (driver.getTitle().contains(windowTitle)) { 25 flag = true; 26 System.out.println("Switch to window: " 27 + windowTitle + " successfully!"); 28 break; 29 } else 30 continue; 31 } 32 } 33 } catch (NoSuchWindowException e) { 34 flag = false; 35 } 36 return flag; 37 } 38 39 //实现方式三 40 public void switchToWindow(String windowTitle) 41 { 42 Set<String> windowhandles=Driver.getWindowHandles(); 43 for(String handle:windowhandles) 44 { 45 Driver.switchTo().window(handle); 46 String title=Driver.getTitle(); 47 if(title.equals(windowTitle)) 48 break; 49 } 50 }
Wait 机制及实现
? 目的: 1. 让元素对象在可见后进行操作, 取代Thread.sleep, 使其变成智能等待
? 主要操作:
boolean wait = new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>()
{ public Boolean apply(WebDriver d)
{ return d.findElement(By.className("red")).isDisplayed(); }
}
? 说明:
1. 在规定的时间内只要符合条件即返回,上面的代码中是只要isDisplayed即返回;
2. 应用到了WebDriverWait类,这种写法,请大家务必熟练;
1 //全局等待 2 Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 3 4 //智能等待 5 boolean wait = new WebDriverWait(Driver, 10).until 6 ( 7 new ExpectedCondition<Boolean>() 8 { 9 public Boolean apply(WebDriver d) 10 { 11 return d.findElement(By.className("red")).isDisplayed(); 12 } 13 } 14 ); 15 System.out.print(wait);
原文:http://www.cnblogs.com/leoliyue/p/4877482.html