1 封装WebDriver
package com.selenium.test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class InitWebDriver { public static WebDriver myDriver(String browser) { WebDriver driver = null; if ("firefox".equals(browser.toLowerCase())) { //启动本机firefox ProfilesIni allProfiles = new ProfilesIni(); FirefoxProfile profile = allProfiles.getProfile("default"); //启动浏览器 driver = new FirefoxDriver(profile); driver.manage().window().maximize(); }else if ("ie".equals(browser.toLowerCase())) { //关闭保护模式 DesiredCapabilities capability = new DesiredCapabilities(); capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); capability.setCapability("ignoreProtectedModeSettings",true); //指定驱动位置,并加载驱动 System.setProperty("webdriver.ie.driver", "drivers/IEDriverServer.exe"); driver = new InternetExplorerDriver(capability); driver.manage().window().maximize(); } else if ("chrome".equals(browser.toLowerCase())) { //指定驱动位置,并加载驱动 System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); }else{ System.out.println("浏览器指定错误!!!"); } return driver; } }
package com.selenium.test; import org.openqa.selenium.WebDriver; public class InitWebDriverTest { public static void main(String[] args) { WebDriver myBrowser = InitWebDriver.myDriver("firefox"); myBrowser.navigate().to("http://www.cnblogs.com/lizitest/"); // WebDriver myBrowser = InitWebDriver.myDriver("ie"); // myBrowser.navigate().to("http://www.cnblogs.com/lizitest/"); // // WebDriver myBrowser = InitWebDriver.myDriver("chrome"); // myBrowser.navigate().to("http://www.cnblogs.com/lizitest/"); } }
2 使用配置文件
<?xml version="1.0" encoding="UTF-8"?> <chuanke> <browser>firefox</browser> <url>http://www.cnblogs.com/lizitest/</url> <waitTime>2</waitTime> </chuanke>
package com.selenium.tool; import java.io.File; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * @author 栗子测试 * 使用SAX(Simple APIs for XML,也即XML简单应用程序接口)解析XML文件 * */ public class ParseXML { private Document document; /** * 构造函数 * 在新建对象的同时加载XML文件 */ public ParseXML(String filePath){ this.loadXML(filePath); } /** * 加载XML文件 */ public void loadXML(String filePath){ //新建文件 File file = new File(filePath); if(file.exists()){ //dom4j包中SAXReader SAXReader saxReader = new SAXReader(); try { document = saxReader.read(file); } catch (DocumentException e) { e.printStackTrace(); } }else{ System.out.println("XML文件不存在"); } } /** * 获取节点上的文本 */ public String getSingleNodeText(String nodePath){ Element element = (Element) document.selectSingleNode(nodePath); if(element != null){ return element.getTextTrim(); }else{ System.out.println("节点不存在!"); return null; } } }
package com.selenium.util; import com.selenium.tool.ParseXML; public interface Config { public static ParseXML xml = new ParseXML(".\\config\\config.xml"); public static String browser = xml.getSingleNodeText("//browser"); public static String url = xml.getSingleNodeText("//url"); public static String waitTime = xml.getSingleNodeText("//waitTime"); }
package com.selenium.util; import org.openqa.selenium.WebDriver; public class InitWebDriverTest2 { public static void main(String[] args) { WebDriver myBrowser = InitWebDriver.myDriver(Config.browser); myBrowser.navigate().to(Config.url); } }
3 xpath及控件识别
package com.selenium.util; import java.util.List; import java.util.Set; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.ElementNotVisibleException; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.NoSuchWindowException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.Select; public class MyBrowser { WebDriver driver =null; public MyBrowser(String browsr) { this.driver = InitWebDriver.myDriver(browsr); } //页面导航 public void navigateTo(String url) { driver.navigate().to(url); } //输入框 public WebElement webEdit(String xpath){ try{ WebElement webEdit = driver.findElement(By.xpath(xpath)); webEdit.clear(); return webEdit; }catch(NoSuchElementException e){ System.out.println("输入框不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个输入框!"); return null; } } //按钮 public WebElement webButton(String xpath){ try{ WebElement webButton = driver.findElement(By.xpath(xpath)); return webButton; }catch(NoSuchElementException e){ System.out.println("按钮不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个按钮!"); return null; } } //链接 public WebElement link(String xpath) { try{ WebElement link = driver.findElement(By.xpath(xpath)); return link; }catch(NoSuchElementException e){ System.out.println("链接按钮不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个链接!"); return null; } } //悬停 public void hover(String xpath) { try{ WebElement element = driver.findElement(By.xpath(xpath)); Actions action = new Actions(driver); action.moveToElement(element).perform(); }catch(NoSuchElementException e){ System.out.println("悬停对象不存在!"); }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个悬停对象!"); } } //窗口切换 public void switchToWindow(String title) { try { String currentHandle = driver.getWindowHandle(); Set<String> handles = driver.getWindowHandles(); for (String handle : handles){ if(handle.equals(currentHandle)){ continue; }else{ driver.switchTo().window(handle); if (driver.getTitle().contains(title)){ break; } else{ continue; } } } } catch (NoSuchWindowException e) { System.out.println("没有找到窗口!"); } } //单选按钮 public List<WebElement> webRadioGroup(String xpath) { try{ List<WebElement> radios = driver.findElements(By.xpath(xpath)); //定位所有单选按钮 return radios; }catch(IndexOutOfBoundsException e){ System.out.println("单选按钮不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个单选按钮!"); return null; } } //多选按钮 public List<WebElement> WebCheckBox(String xpath){ try{ List<WebElement> checkboxs = driver.findElements(By.xpath(xpath)); //定位所有多选按钮 return checkboxs; }catch(IndexOutOfBoundsException e){ System.out.println("多选按钮不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个多选按钮!"); return null; } } //下拉框 public Select webList(String xpath){ try{ WebElement selectElement = driver.findElement(By.xpath(xpath)); //先定位下拉框 Select select = new Select(selectElement); return select; }catch(NoSuchElementException e){ System.out.println("下拉框不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个下拉框!"); return null; } } //上传文件(input) public WebElement upLoadFile(String xpath){ try{ WebElement file = driver.findElement(By.xpath(xpath)); return file; }catch(NoSuchElementException e){ System.out.println("上传文件控件不存在!"); return null; }catch (ElementNotVisibleException e) { System.out.println("XPath匹配多个上传文件控件!"); return null; } } //JS public void executeJS(String jsString){ JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript(jsString); } //iframe public void switchToIframe(String xpath){ WebElement iframe = driver.findElement(By.xpath(xpath)); driver.switchTo().frame(iframe); } //弹出框 public Alert switchToAlert(){ Alert alert = driver.switchTo().alert(); //切换到弹出窗 return alert; } }
package com.seleniu.pages; //接口只是一个抽象方法声明和静态不能被修改的数据的集合 public interface BaiduHome { public String LINK_LOGIN = "//div[@id='u1']/a[text()='登录']"; public String WEBEDIT_USERNAME = "//input[@id='TANGRAM__PSP_8__userName']"; public String WEBEDIT_PASSWORD = "//input[@id='TANGRAM__PSP_8__password']"; public String WEBBUTTON_USERNAME = "//input[@id='TANGRAM__PSP_8__submit']"; public String LINK_USERNAME = "//a[@id='s_username_top']/span"; }
package com.selenium.util; import com.seleniu.pages.BaiduAccountSetting; import com.seleniu.pages.BaiduHome; import com.seleniu.pages.BaiduPersonalSetting; public class MyBrowserTest implements BaiduHome,BaiduAccountSetting,BaiduPersonalSetting{ public static void main(String[] args) throws InterruptedException { MyBrowser myBrowser = new MyBrowser(Config.browser); myBrowser.navigateTo(Config.url); myBrowser.link(LOGIN).click(); Thread.sleep(3000); //登录 myBrowser.webEdit(USERNAME).sendKeys("栗子测试"); myBrowser.webEdit(PASSWORD).sendKeys("2472471982"); myBrowser.webButton(WEBBUTTON_USERNAME).click(); Thread.sleep(3000); //悬停 myBrowser.hover(LINK_USERNAME); //账号设置 myBrowser.link(ACCOUNTSETTING).click(); myBrowser.switchToWindow(AS_TITLE); //修改资料 myBrowser.link(MODIFYDATA).click(); myBrowser.switchToWindow(PS_TITLE); //基本资料 myBrowser.webRadioGroup(GENDER).get(0).click(); myBrowser.link(DIV_BLOOD).click(); myBrowser.link(LINK_BLOOD).click(); myBrowser.webButton(SAVE).click(); //详细资料 myBrowser.link(DETAILEINFO).click(); myBrowser.WebCheckBox(CHARACTER).get(5).click(); myBrowser.webButton(SAVE).click(); //其他 myBrowser.webList(OTHER).selectByVisibleText("XX"); myBrowser.upLoadFile(OTHER).sendKeys("XXX"); myBrowser.executeJS("XXX"); myBrowser.switchToIframe("XXX"); myBrowser.switchToAlert().accept(); } }