浏览器操作:
打开浏览器:
chrome:System.setProperty("webdriver.chrome.driver", “浏览器驱动路径”);firefox:System.setProperty("webdriver.gecko.driver",“浏览器驱动路径”);IE:System.setProperty("webdriver.ie.driver",“浏览器驱动路径”);如果系统为MAC或者linux,驱动需要修改为对应系统驱动。RemoteWebDriver Browser= new ChromeDriver();WebDriver Browser= new FirefoxDriver(profile);WebDriver Browser= new InternetExplorerDriver();
打开页面:
Browser.get(“http://www.baidu.com”)
前进
Browser.navigate().forward();
后退
Browser.navigate().back();
刷新
Browser.navigate().refresh();
最大化:
Browser.manage().window().maximize();
cookies:
添加:Browser.manage().addCookie(cookie);删除:Browser.manage().deleteAllCookies();Browser.manage().deleteCookie(cookie);获取:Browser.manage().getCookies();Browser.manage().getCookieNamed(name);
打开新页面
Browser.navigate().to(url);
打开新窗口
Actions action = new Actions(driver);action.sendKeys(Keys.CONTROL + "n").perform();switchToWindow("新标签页", driver);driver.get("http://www.sina.com.cn");切换窗口browser.switchTo().window(nameOrHandle)
控件类型
输入框:输入、读取。按钮:单击、双击。表格:点击表格内数据、读取表格内数据iframe:选择iframe、退出iframe、返回顶层framecombobox:按id选择、按name选择、按value选择单选框:点击、isselected多选框:勾选、取消勾选、isselectedlable:获取数据日期控件:点击年、月、日导入、导出控件:导入、导出数据
控件操作
输入:browser.findElement(By.id("")).sendKeys("");;获取文本:browser.findElement(By.id("")).getText();单击:browser.findElement(By.id("")).click();双击:Actions a=new Actions(browser);
a.doubleClick();
获取表格数据:
String xpath="//*[@id='table138']/tbody/tr["+row+"]/td["+column+"]";WebElement table=browser.findElement(By.xpath(xpath));String text=table.getText();
悬浮:
Actions a=new Actions(browser);a.moveToElement(target)
拖动滚动条:
JavascriptExecutor js = (JavascriptExecutor)Browser;js.executeScript("scrollTo(0,10000)");//-----------------------------向下拉到底js.executeScript("scrollBy(0, 0-document.body.scrollHeight *99/100)");//---向上拉到顶js.executeScript("scrollBy(0, 0-document.body.scrollWidht *1/2)");//------左右拉到中间
拖动元素到另一个元素:
a.dragAndDrop(source, target);
键盘控制键盘输入
Actions action = new Actions(Browser);action.keyDown(Browser.findElement(By.id(locator)), Keys.CONTROL);
上下左右
Actions action = new Actions(Browser);enter:action.keyDown(Browser.findElement(By.id(locator)), Keys.down); //key.操作Actions action = new Actions(Browser);action.keyDown(Browser.findElement(By.id(locator)), Keys.enter); //key.操作
……流程控制条件判断:if循环:while 、until、do while延时:sleep、wait等待元素出现:
WebDriverWait driverWait = (WebDriverWait) new WebDriverWait(Browser, 30, 500).ignoring(StaleElementReferenceException.class).withMessage("元素在30秒内没有出现!");return driverWait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {try {
if (driver.findElement(By.xpath(xpath)).isDisplayed()) {
// System.out.println(driver.findElement(By.xpath(xpath)));return true;
}
} catch (IndexOutOfBoundsException e) {
Assert.fail("WaitforElement:"+"元素" +xpath+"不存在");} catch (NoSuchElementException e) {
Assert.fail("WaitforElement:"+"元素" +xpath+"不存在");
}
return false;}
});
判断元素是否存在
private boolean isElementPresent( String locator) {
WebElement a;try {a = Browser.findElement(By.xpath(locator));return true;} catch (RuntimeException e) {e.printStackTrace();return false;}
}
判断文本是否存在:private boolean isTextPresent(WebDriver driver, String Text) {
boolean status = false;try {
driver.findElement(By.xpath("//*[contains(.,'" + Text + "')]"));System.out.println(Text + " is appeard!");status = true;
} catch (NoSuchElementException e) {
status = false;System.out.println("'" + Text + "' doesn't exist!");
}return status;
}