一.启动/放大/关闭浏览器
1.IE
下载IE驱动并放在IE安装目录下
System.setProperty("webdriver.ie.driver","C:/Program Files/Internet Explorer/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
启动IE,关闭下图是个区域的保护模式
2.火狐
WebDriver driver = new FirefoxDriver();
3.谷歌
要先下载谷歌驱动放在谷歌安装路径下
System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe"); //指定驱动路径
WebDriver driver = new ChromeDriver();
最大化浏览器:
driver.manage().window().maximize();
打开测试页面
(1) driver.get("http://www.google.com");
(2)driver.navigate().to("http://www.baidu.com/");
P.S.navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等
关闭浏览器:
(1)driver.close();
(2)driver.quit();
二.页面元素定位方法
<input class="input_class" type="text" name="passwd" id="passwd-id" />
- By.id: WebElement element = driver.findElement(By.id("passwd-id"));
- By.name: WebElement element = driver.findElement(By.name("passwd"));
- By.xpath: WebElement element =driver.findElement(By.xpath("//input[@id=‘passwd-id‘]"));
- By.className : WebElement element = driver.findElement(By.className("input_class"));
- By.cssSelector: WebElement element = driver.findElement(By.cssSelector(".input_class"));(WebElement userName=driver.findElement(By.cssSelector(“html body div div form input”));)
- By.tagName: WebElement element =driver.findElement(By.tagName("input"));
<a href="http://www.google.com/search?q=aa">cheese</a>
- WebElementcheese=driver.findElement(By.linkText("cheese"));精准匹配
- WebElementcheese=driver.findElement(By.partialLinkText("cheese"));模糊匹配
三.操作页面各类元素
1.输入框(text field or textarea)
WebElement element = driver.findElement(By.id("passwd-id"));
- element.sendKeys(“test”);//在输入框中输入内容:
- element.clear(); //将输入框清空
- element.getText(); //获取输入框的文本内容:
2.下拉选择框(Select)
Select select = new Select(driver.findElement(By.id("select")));
- select.selectByVisibleText(“A”);
- select.selectByValue(“1”);
- select.deselectAll();
- select.deselectByValue(“1”);
- select.deselectByVisibleText(“A”);
- select.getAllSelectedOptions();
- select.getFirstSelectedOption();
3.单选项(Radio Button)
WebElement radio=driver.findElement(By.id("BookMode"));
- radio.click(); //选择某个单选项
- radio.clear(); //清空某个单选项
- radio.isSelected(); //判断某个单选项是否已经被选择
4.多选项(checkbox)
WebElement checkbox = driver.findElement(By.id("myCheckbox."));
- checkbox.click();
- checkbox.clear();
- checkbox.isSelected();
- checkbox.isEnabled();
5.按钮(button)
WebElement btn= driver.findElement(By.id("save"));
- btn.click(); //点击按钮
- btn.isEnabled (); //判断按钮是否enable
6.弹出对话框(Popup dialogs)
Alert alert = driver.switchTo().alert();
- alert.accept(); //确定
- alert.dismiss(); //取消
- alert.getText(); //获取文本
7.表单(Form)
Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();或approve.submit();//只适合于表单的提交
8.上传文件
上传文件的元素操作:
WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
9.Windows 和 Frames之间的切换
- driver.switchTo().defaultContent(); //返回到最顶层的frame/iframe
- driver.switchTo().frame("leftFrame"); //切换到某个frame:
- driver.switchTo().window("windowName"); //切换到某个window
10.调用Java Script
Web driver对Java Script的调用是通过JavascriptExecutor来实现的,例如:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("JS脚本");
11.超时设置
WebDriver driver = new FirefoxDriver();
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //识别元素时的超时时间
- driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); //页面加载时的超时时间
- driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS); //异步脚本的超时时间