selenium java封装_Java Selenium封装--RemoteWebDriver(示例代码)

1 packagecom.selenium.driver;2 importjava.io.File;3 importjava.io.IOException;4 importjava.net.URL;5 importjava.util.HashMap;6 importjava.util.Map;7 importjava.util.Set;8 importjava.util.regex.Matcher;9 importjava.util.regex.Pattern;10 importorg.apache.commons.io.FileUtils;11 importorg.openqa.selenium.Alert;12 importorg.openqa.selenium.Capabilities;13 importorg.openqa.selenium.Cookie;14 importorg.openqa.selenium.JavascriptExecutor;15 importorg.openqa.selenium.NoSuchElementException;16 importorg.openqa.selenium.OutputType;17 importorg.openqa.selenium.TakesScreenshot;18 importorg.openqa.selenium.WebDriver;19 importorg.openqa.selenium.WebElement;20 importorg.openqa.selenium.remote.Augmenter;21 importorg.openqa.selenium.remote.RemoteWebDriver;22 importorg.openqa.selenium.remote.RemoteWebElement;23 public classJSWebDriver{24 private RemoteWebDriver wd = null;25 private JavascriptExecutor jse = null;26

27 publicJSWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {28 wd = newRemoteWebDriver(remoteAddress, desiredCapabilities);29 }30

31 ///

32 ///浏览器url导航

33 ///

34 public voidgoTo(String url){35 wd.get(url);36 }37

38 ///

39 ///浏览器退出

40 ///

41 public voidquit(){42 wd.quit();43 }44

45 ///

46 ///浏览器后退

47 ///

48 public voidback(){49 wd.navigate().back();50 }51

52 ///

53 ///浏览器前进

54 ///

55 public voidforward(){56 wd.navigate().forward();57 }58

59 ///

60 ///浏览器刷新

61 ///

62 public voidrefresh(){63 wd.navigate().refresh();64 }65

66 ///

67 ///切换到新浏览器窗口;按照title、url、index;支持正则匹配

68 ///

69 public void switchToWindow(String by, String value, String...match) throwsException{70 String currenthandle =wd.getWindowHandle();71 Set handles =wd.getWindowHandles();72 int currentIndex = -1;73 String searchString = "";74 for(String handle : handles){75 currentIndex += 1;76 if(handle.equals(currenthandle)){77 continue;78 }else{79 wd.switchTo().window(handle);80 if (match.length == 1 && match[0].equals("regex")){81 if (by.equals("title")){82 searchString =wd.getTitle();83 }else if (by.equals("url")){84 searchString =wd.getCurrentUrl();85 }86 Pattern pattern =Pattern.compile(value);87 Matcher matcher =pattern.matcher(searchString);88 if(matcher.find()){89 return;90 }91 }else{92 if (by.equals("title")){93 searchString =wd.getTitle();94 }else if (by.equals("url")){95 searchString =wd.getCurrentUrl();96 }else if (by.equals("index")){97 searchString =Integer.toString(currentIndex);98 }99 if(searchString.equals(value)){100 return;101 }102 }103 }104 }105 Exception e = new Exception("Swtich Window Failed, Please Make Sure The Locator Was Right.");106 throwe;107 }108

109 ///

110 ///JS弹框确认

111 ///

112 public voidclickAlertSure(){113 Alert alert =wd.switchTo().alert();114 alert.accept();115 }116

117 ///

118 ///JS弹框取消

119 ///

120 public voidclickAlertDismiss()121 {122 Alert alert =wd.switchTo().alert();123 alert.dismiss();124 }125

126 ///

127 ///设置prompt弹框内容

128 ///

129 public voidsetPromptMessage(String parameter){130 Alert alert =wd.switchTo().alert();131 alert.sendKeys(parameter);132 }133

134 ///

135 ///获取JS弹框内容

136 ///

137 publicString getPromptMessage(){138 Alert alert =wd.switchTo().alert();139 returnalert.getText();140 }141

142 ///

143 ///切换到Frame窗口;先定位到iframe元素

144 ///

145 public voidswitchToFrame(JSWebElement jselement){146 wd.switchTo().frame(jselement.getNativeWebElement());147 }148

149 ///

150 ///执行JS脚本

151 ///

152 public voidexecuteScript(String parameter){153 JavascriptExecutor js =getJSE();154 js.executeScript(parameter);155 }156

157 ///

158 ///获取指定cookie

159 ///

160 publicString getCookie(String name){161 Cookie cookie=wd.manage().getCookieNamed(name);162 if (cookie == null){ return "null"; }163 returncookie.getValue();164 }165

166 ///

167 ///获取所有cookie

168 ///

169 public MapgetCookies(){170 Map newCookies = new HashMap();171 Set cookies=wd.manage().getCookies();172 for(Cookie cookie : cookies){173 newCookies.put(cookie.getName(), cookie.getValue());174 }175 returnnewCookies;176 }177

178 ///

179 ///截取屏幕

180 ///

181 public voidgetScreen(String filepath){182 WebDriver augmentedDriver = new Augmenter().augment(this.wd);183 TakesScreenshot ts =(TakesScreenshot) augmentedDriver;184 File screenShotFile =ts.getScreenshotAs(OutputType.FILE);185 try{186 FileUtils.copyFile (screenShotFile, newFile(filepath));187 }catch(IOException e){188 e.printStackTrace();189 }190 }191

192 ///

193 ///获取title

194 ///

195 publicString getTitle(){196 returnwd.getTitle();197 }198

199 ///

200 ///获取url

201 ///

202 publicString getUrl(){203 returnwd.getCurrentUrl();204 }205

206 ///

207 ///获取HTML源码

208 ///

209 publicString getSource(){210 try{211 Thread.sleep(500);212 } catch(InterruptedException e) {213 e.printStackTrace();214 }215 returnwd.getPageSource();216 }217

218 ///

219 ///滚动页面到指定位置

220 ///

221 public voidscroll(String x, String y){222 if (x.equals("left")){223 x = "0";224 }else if (x.equals("right")){225 x = "document.body.scrollWidth";226 }else if (x.equals("middle")){227 x = "document.body.scrollWidth/2";228 }229 if (y.equals("top")){230 y = "0";231 }else if (y.equals("buttom")){232 y = "document.body.scrollHeight";233 }else if (y.equals("middle")){234 y = "document.body.scrollHeight/2";235 }236 this.executeScript(String.format("scroll(%s,%s);", x, y));237 }238

239 ///

240 ///最大化浏览器

241 ///

242 public voidmaximize(){243 wd.manage().window().maximize();244 }245

246 publicJSWebElement findElementById(String using) {247 try{248 return newJSWebElement((RemoteWebElement)wd.findElementById(using));249 }catch(NoSuchElementException e){250 return newJSWebElement();251 }252 }253

254 publicJSWebElement findElementByCssSelector(String using) {255 try{256 return newJSWebElement((RemoteWebElement)wd.findElementByCssSelector(using));257 }catch(NoSuchElementException e){258 return newJSWebElement();259 }260 }261

262 publicJSWebElement findElementByXPath(String using) {263 try{264 return newJSWebElement((RemoteWebElement)wd.findElementByXPath(using));265 }catch(NoSuchElementException e){266 return newJSWebElement();267 }268 }269

270 publicJSWebElement findElementByLinkText(String using) {271 try{272 return newJSWebElement((RemoteWebElement)wd.findElementByLinkText(using));273 }catch(NoSuchElementException e){274 return newJSWebElement();275 }276 }277

278 publicJSWebElement findElementByDom(String using) {279 try{280 JavascriptExecutor js = this.getJSE();281 WebElement we = (WebElement)js.executeScript(String.format("return %s", using));282 return newJSWebElement((RemoteWebElement)we);283 }catch(NoSuchElementException e){284 return newJSWebElement();285 }286 }287

288 ///

289 ///获取原生的RemoteWebdriver对象

290 ///

291 publicRemoteWebDriver getNativeWebDriver(){292 return this.wd;293 }294

295 privateJavascriptExecutor getJSE(){296 if (this.jse == null){297 this.jse = (JavascriptExecutor) this.wd;298 }299 returnjse;300 }301 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程介绍你是否在寻找机会进入自动化测试领域? 你是否渴望学习selenium webdriver + Java以及最新的框架和技术进行web自动化测试? 你是否感兴趣学习Selenium如何用在你现有的项目里的? 这门课带你从Selenium搭建环境开始讲起,然后学习selenium,TestNG, logback, maven, jenkins。 我们假设学员没有任何自动化经验,来设计的这套课程。每个课题都从最基础的开始讲起。Selenium相关的该覆盖的课题都覆盖了。 例子都是来自于真实的web应用项目,帮助你理解不同的组件怎么用上自动化,这将展示给你一个行业层面的框架,增加自信心。 全网没有其他课程像这门课涵盖到如此之深的细节。 您将会学到什么 学完课程以后,你将拥有完整的Selenium Webdriver知识 你将具备从头开始设计Page Object、Page Factory、DATADRIVEN等搭建自动化框架的能力 用100多个实例对Selenium现实场景应用进行深入理解 全面了解TestNG, Maven, Jenkins, HTML报告,多浏览器并行测试 了解数据库测试和使用Selenium进行性能测试 你将彻底了解testNG框架 你从网上随便选择一个网站,都可以实现自动化,用所有可能的测试用例进行自动化测试 将提高你的编码技能,以编写最优化的自动化测试用例代码 你基本可以搞定任何Selenium面试,并能从设计阶段开始领导整个Selenium自动化项目 你应该能够使用应用程序的GUI来验证数据完整性 你将能够创建漂亮的报告来打动客户或领导 更深入地理解自动化指南和代码质量标准 会附带一个练习网站,可以用上所有可用的WebDriver功能,实现自动化 【适合人群】 软件手动测试人员想转为自动化测试的人员 自动化软件测试人员想加强专业技能的 刚毕业学生想从事软件行业 QA 组长或项目经理 【课程优势】 学完课程以后,你将拥有完整的Selenium Webdriver知识 【讲师介绍】 资质介绍: 12年以上软件测试工作经验,其中7年以上自动化测试开发经验 新书“Python3+Selenium3自动化测试项目实战”作者

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值