selenium学习笔记

来源:互联网 发布:网络剧老炮儿哪里能看 编辑:程序博客网 时间:2024/05/16 18:01
package demo;


import java.util.List;


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.reporters.EmailableReporter;


public class WebDriverMethods {
public static void main(String[] args) {
//设置系统属性:firefox路径
System.setProperty("webdriver.firefox.bin","D:\\firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
// 访问一个网页
// driver.get("http://www.baidu.com");
driver.navigate().to("http://www.baidu.com");
//查找定位UI元素   by.id/by.className/by.tagName/byLinkText/
WebElement element = driver.findElement(By.id("kw"));
List<WebElement> className=driver.findElements(By.className("fm"));
WebElement tagName=driver.findElement(By.tagName("form"));
WebElement name=driver.findElement(By.name("ie"));
WebElement linkText=driver.findElement(By.linkText("ie"));
WebElement PartiallinkText=driver.findElement(By.partialLinkText("ie"));
WebElement CSS=driver.findElement(By.cssSelector("#food span.dairy aged"));

List<WebElement> inputs=driver.findElements(By.xpath("//input"));

WebElement element2=(WebElement)((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
// 编立select标签
WebElement select=driver.findElement(By.tagName("select"));
List<WebElement> allOpthions=driver.findElements(By.tagName("option"));
for(WebElement option:allOpthions){
System.out.println(String.format("values is :%s",option.getAttribute("value")));
option.click();
}

//选择某一个选项
Select select2=new Select(driver.findElement(By.tagName("select")));
select2.deselectAll();
select2.selectByVisibleText("edam");

//上传文件
WebElement fileupload=driver.findElement(By.id("upload"));
String filePath="c:\\test\\uploadfile\\media_ads\\test.jpg";
fileupload.sendKeys(filePath);

//提交 submit在form中
driver.findElement(By.id("submit")).click();
//提交 submit不在form中
driver.findElement(By.id("submit")).submit();
//拖拽
WebElement source=driver.findElement(By.id("source"));
WebElement target=driver.findElement(By.id("source"));
(new Actions(driver)).dragAndDrop(source, target).perform();

//窗口切换
driver.switchTo().window("windowsname");

for(String handle:driver.getWindowHandles()){
driver.switchTo().window(handle);
}
//frame
driver.switchTo().frame("framename");
driver.switchTo().frame("framename.0.child");
//弹窗
Alert alert=driver.switchTo().alert();

//浏览器的前进后退
driver.navigate().forward();
driver.navigate().back();

element.sendKeys("Selenium");
element.submit();
System.out.println("Page title is " + driver.getTitle());
// new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
// public Boolean apply(WebDriver d) {
// return d.getTitle().toLowerCase().startsWith("Selenium");
// }
// });


System.out.println("page title is :" + driver.getTitle());

driver.quit();

/**
* 一些编程思路技巧
*/
//条件循环

Selenium selenium = new DefaultSelenium("localhot", 4444, "*iehta", "http://www.google.com/"); 
if(selenium.isElementPresent("q")){
selenium.type("q", "Seleniu RC");
}else {
// System.out.println("Element :"+q +"is not avalialbe on page");
}
String[] checkboxids=selenium.getAllFields();
for (String checkboxid : checkboxids) {
if(checkboxid.contains("addForm")){
selenium.click("q");
}
}
//定位ajax
for(int second=0;;second++){
if(second>=60){
try {
if(selenium.isElementPresent("link=ajaxLink")){
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
// 封装selenium函数
/* selenium.click("sss");
selenium.waitForPageToLoad("a.html");
public void clickandwait(String elementLocator,String waitPeriod){
selenium.click("sss");
selenium.waitForPageToLoad("a.html");
}*/

//将变量写配置文件方式映射一下properties
//页面对象方式设计模式
//数据库编程

}
}
原创粉丝点击