为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

selenium学习2-启动浏览器

2017-12-04 8页 doc 26KB 18阅读

用户头像

is_531654

暂无简介

举报
selenium学习2-启动浏览器selenium学习2-启动浏览器 新建Java 项目结构: 1. 用JAVA去启动各种浏览器 1.1启动火狐浏览器 /* * 1)声明driver对象(选择启动什么浏览器) * 2)driver去打开浏览器并输入你要测试的网页地址(使用get方法打开站点) * navigation 对象也可以打开地址 * 3)找到你要操作的元素(利 用 * 4)对元素进行输入、点击、断言操作 * 5)关闭浏览器,释 放资源(弄明白quit、close方法的区别) */ publicstaticvoidmyFireFox(){ /...
selenium学习2-启动浏览器
selenium学习2-启动浏览器 新建Java 项目结构: 1. 用JAVA去启动各种浏览器 1.1启动火狐浏览器 /* * 1)声明driver对象(选择启动什么浏览器) * 2)driver去打开浏览器并输入你要测试的网页地址(使用get方法打开站点) * navigation 对象也可以打开地址 * 3)找到你要操作的元素(利 用 * 4)对元素进行输入、点击、断言操作 * 5)关闭浏览器,释 放资源(弄明白quit、close方法的区别) */ publicstaticvoidmyFireFox(){ //若浏览器安装不在默认路径下, 则需要通过 //System.setProperty("webdriver.firefox.bin", "C:/Program Files WebDriverdr = newFirefoxDriver(); dr.manage().window().maximize();//使启动的浏览器最大化 Navigation ng = dr.navigate(); ng.to(""); System.out.println(dr.getTitle()); //获取输入框 //启动火狐浏览器并在搜索框中输入“,点击搜索框 // dr.get("");//也可以转到想要打开的页面 // } WebElementsearchinput = dr.findElement(By.name("q")); //在搜 索输入框中添加搜索信息 searchinput.sendKeys("selenium"); //获取 搜索提交按钮 WebElementsearchbutton = ——————————————————————————————————————————————— dr.findElement(By.id("search-button")); //模拟点击按钮 searchbutton.click(); //获取搜索框 WebElementtt = dr.findElement(By.id("keyword")); //检测搜索框信息是否是“” Assert.assertEquals(tt.getAttribute("value"), "selenium"); try { } Thread.sleep(3000);//等待3秒钟 // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { dr.close(); dr=null; 1.2启动IE浏览器 //再启动其他浏览器的时候,以来各浏览器驱动,如果不想再 System.setProperty()中设置,可以将驱动放到系统system32文件夹中 //启动IE浏览器 publicstaticvoidmyIE(){ } //若将驱动未 放在系统System32文件夹下,放在其他路径,需要通过下面设置一 下 System.setProperty("webdriver.ie.bin", "files/IEDriverServer.exe"); WebDriverdr = newInternetExplorerDriver(); dr.manage().window().maximize();//使启动的浏览器最大化 Navigation ng = dr.navigate(); ng.to(""); dr.close(); dr=null; // 1.3启动谷歌浏览器 publicstaticvoidmyChrome(){ // System.setProperty("webdriver.chrome.bin", "files/chromedriver.exe"); } WebDriverdr = newChromeDriver(); ——————————————————————————————————————————————— dr.manage().window().maximize();//使启动的浏览器最大化 Navigation ng = dr.navigate(); ng.to(""); dr.close(); dr=null; 1.4启动浏览器时加载插件 //启动Firefox浏览器时加载插件 publicstaticvoidmyFireFoxPlugins(){ } //定义插件所在 的位置 File file = newFile("files/firebug-2.0.13-fx.xpi"); //声明一个 Profile对象,里面保存file的信息。 FirefoxProfileffp = newFirefoxProfile(); try { } //设置插件相关参数 ffp.setPreference("extensions.firebug.currentVersion", "2.0.13"); newFirefoxDriver(ffp); //把加载到Profile对象中 ffp.addExtension(file); // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { 启动后图示: //启动谷歌浏览器加载插件选择加载一款快速查看网页CSS样式 插件 publicstaticvoidmyChromePlugins(){ } File file = newFile("files/CSSViewer_v1.6.crx"); ChromeOptions co = newChromeOptions(); co.addExtensions(file); newChromeDriver(co); 启动后图示: 1.5 Selenium2启动Firefox时的profile设置 1) 在Firefox地址栏中输入:about:config,可以查看有哪些可 以设置 2) 配置其他插件类似1.4中启用firebug插件应用 ——————————————————————————————————————————————— 3) 启动Firefox浏览器时使用默认配置 //启动 publicstaticvoidmyFireFoxDefault(){ } 启动火狐浏览器默认启动firebug并通过netexport保存”.har” 文件 打开火狐浏览器,输入about:config,设置相关项为ture,设置 netexport导出地址extensions.firebug.allPagesActivation;on firebug 自启动 extensions.firebug.netexport.alwaysEnableAutoExport;ture自动导 出 ProfilesInipfi = newProfilesIni(); FirefoxProfileffpf = pfi.getProfile("default"); newFirefoxDriver(ffpf); extensions.firebug.netexport.defaultLogDir;d:\设置导出路径 extensions.firebug.netexport.saveFiles;ture保存文件 1.6 在B机器上加载A机器的用户配置 1) 如果在机器B上要启动机器A上的firefox配置,则先导出A 的配 置,然后加载: a)将A机器上的Profiles文件夹” C:\Users\CF\AppData\Local\Mozilla\Firefox\Profiles\”给拷贝出来 b)代 码: publicstaticvoidmyFireFoxDefalutAtoB(){ File file = ——————————————————————————————————————————————— newFile("C:/Users/CF/AppData/Local/Mozilla/Firefox/Profiles/ynje27ih.default"); 2) 如果在机器B上要启动机器A上的谷歌浏览器配置,则先导 出A的 配置,然后加载: a) 先将A机器上的user data 文件拷贝出来: C:\Users\CF\AppData\Local\Google\Chrome\User Data b) 代码: //在B机器上加载A机器的用户配置信息浏览器实现 publicstaticvoidmyChromeUserDataAtoB(){ 1.7 Selenium2启动IE时的设置 启动IE时需关闭保护模式 1) 手工关闭 2) 代码方式关闭 //启动IE浏览器关闭保护设置 publicstaticvoidmyIECloseProtected(){ DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); capabilities.setCapability("ignoreProtectedModeSettings", true); //IE默认的就是开启保护模式,要么手动在浏览器的设置中关闭,要 ——————————————————————————————————————————————— 么加上这么一句实现 dr = newInternetExplorerDriver(capabilities); } 2. 在启动firefox时,如何设置firefox的默认下载文件路径,让 在需要下载的地方自动的把文件下载到设置的文件夹中去。请给出代 码解决。 publicstaticvoidsetDefaultDir(){ } WebDriverdr ; FirefoxProfile profile = newFirefoxProfile(); //示例代码中 “browser.download.downloadDir”不能实现,需要设置 profile.setPreference("browser.download.dir", "d:\\data"); //browser.download.folderList设置默认下载文件夹,0 桌面、1 系统 profile.setPreference("browser.download.folderList", 2); dr = newFirefoxDriver(profile); dr.get("www.haosou.com"); try { } dr.quit(); Thread.sleep(60000); // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { “browser.download.dir”才可以 用户下载 2自定义文件夹 3. 在启动Firefox时,如何设置firefox的http 代理,请给出代码解决方案 publicstaticvoidStartFireFoxByProxy(){ String proxyIp = "10.150.71.19"; intproxyPort = 8888; FirefoxProfile profile = newFirefoxProfile(); //设置代理参数 ——————————————————————————————————————————————— profile.setPreference("network.proxy.type", 1); profile.setPreference("network.proxy.http", proxyIp); profile.setPreference("network.proxy.http_port", proxyPort); newFirefoxDriver(profile); } 3.对于字符串"abcafgakkaal", a.请找出里面含有多少个"a", publicstaticintcheckA(){ }int sum=0; String str = "abcafgakkaal"; for (inti = 0; i<str.length(); i++) if (str.charAt(i) == 'a') { sum++; } return sum; b.取出第2个与第3个a间的字符串 publicstatic String chooseString(){ }String str ="abcafgakkaal"; intintSecondA = str.indexOf('a', str.indexOf('a')+1); returnstr.substring(intSecondA+1, intThirdA); intintThirdA = str.indexOf('a', intSecondA+1); c.把第二个a替换成数字1 publicstatic String replaceAto1(){ }String str = "abcafgakkaal"; //找出第二个a所在位置 intintSecondA = str.indexOf('a', str.indexOf('a')+1); //将第二个a之 前的字符串截取出来 String str1 = str.substring(0,intSecondA); //截取 第二个a之后的字符串病将a替换成1 String str2 = ——————————————————————————————————————————————— str.substring(intSecondA).replace("a", "1"); return str1+str2; d.把所有的a替换成b publicstatic String replaceAllAtoB(){ }String str = "abcafgakkaal"; returnstr.replaceAll("a","b"); ———————————————————————————————————————————————
/
本文档为【selenium学习2-启动浏览器】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索