selenium学习札记.docx_第1页
selenium学习札记.docx_第2页
selenium学习札记.docx_第3页
selenium学习札记.docx_第4页
selenium学习札记.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

selenium学习笔记1 、webdriver 与 selenium RC2、css选择器By.cssSelector(#f_modify_box div.hd a.cls) 实际如下3、xpath选择器id为userBtn 下的 a下的img selenium.isElementPresent(id(userBtn)/x:a/x:img)By.xpath(/liid=userBtn/a/img)4、点击链接,使用driver.getTitle()来验证6、IE下出现了ElementNotFoundException ,但FF运行正常网上查了半天,都说是IE的保护模式,但是貌似只有vista系统,在IE选项-安全里才有那个选项。重装IE7,问题得到解决。7、火狐出现什么问题呢?driver.get()不能通过,要加上才能够正常运行8、webdriver 如何处理ajax案例:我评论了一条信息,但是页面上显示的是ajax处理的,我要判断页面上是否新增了这条评论。直接判断确拉错了信息(因为AJAX反应没那么快)。所以采用以下写法,5秒之内判断是否能够成功返回正确值,如果可以择判断运行正确。boolean result=new WebDriverWait(driver, 5).until (new ExpectedCondition() public Boolean apply(WebDriver driver) boolean result = false; try List elements=driver.findElements(By.xpath(id(content_li)/li/p); if(elements.size()0)Assert.fail(评论失败,没有增加评论); WebElement ele=(WebElement) elements.get(0); System.out.println(text:+ele.getText(); return ele.getText().contains(s); catch(Exception e) System.out.println(error); return result; ); if(result)Assert.assertTrue(true);else Assert.assertTrue(false);Wait wait = new WebDriverWait(driver, 30);WebElement element= wait.until(visibilityOfElementLocated(By.id(some_id);Where “visibilityOfElementLocated” is implemented as:public ExpectedCondition visibilityOfElementLocated(final By locator) return new ExpectedCondition() public WebElement apply(WebDriver driver) WebElement toReturn = driver.findElement(locator); if (toReturn.isDisplayed() return toReturn; return null; ;9、该element is not visible在页面上肉眼确实TEXTAREA可见,但是运行click函数的时候,抛出异常。于是写了一个循环 一直判断是否可见,经观察,是由于此控件加载未完成。于是等待此控件加载完成才,click。WebDriverWait wait = new WebDriverWait(driver,10);wait.until(ExpectedConditions.elementToBeClickable(By.xpath( ); while(true) if(ele.isDisplayed()break; for(int second=1;second+) if(second60)Assert.fail(超时了); if(text.equals() mouseMenu.click(ele); mouseMenu.perform(); Thread.sleep(1000); System.out.println(n); if(i=3) text=driver.findElement(By.xpath(id(ui-dlg-linkShare)/div1/h2).getText(); else text=driver.findElement(By.xpath(id(ui-dlg-emailShare)/div1/h2).getText(); n+; else break; 10、IE自动保存表单,FF不能,要读取profileIE-选项-内容-保存表单11、有关xpath取值不正确用FF插件弄的是By.xpath(/html/body/div3/div3/div/a);用GOOGLE插件xpathOnClick 这个就能找到elementBy.xpath(/html/body/divcontains(concat( , class, ), wrap )/div3/divcontains(concat( , class, ), dt_sidbox )/a);12、一个神奇的问题之前用JUNIT做测试,引用了他的包,后来改成了testng,在做suite的时候,发现运行不了。解决结果:删除junit包。使用ant build.xml生成的报告也产生了影响。13、chrome最大化DesiredCapabilities capabilities = DesiredCapabilities.chrome();capabilities.setCapability(chrome.switches, Arrays.asList(-start-maximized);WebDriver driver = new ChromeDriver(capabilities);14、增加FF插件File file = new File(.resfirebug-1.9.1-fx.xpi); FirefoxProfile firefoxProfile = new FirefoxProfile(); try firefoxProfile.addExtension(file); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); firefoxProfile.setPreference(extensions.firebug.currentVersion,1.9.1);15、webdriver代替TextIsPresent的方法 public boolean isContentAppeared(WebDriver driver,String content) boolean status = false; try driver.findElement(By.xpath(/*contains(., + content + ); System.out.println(content + is appeard!); status = true; catch (NoSuchElementException e) status = false; System.out.println( + content + doesnt exist!); return status; 16、自定义tablepublic class Table private String locator; private WebDriver driver; public Table(WebDriver d, String locator) this.driver = d; this.locator = locator; public String getCellText(int row, int col) String xpath = locator + /tr + row +/td + col + ; WebElement cell = driver.findElement(By.xpath(xpath); return cell.getText(); 17、webdriver常用操作/xiecj_2006/article/details/786903318、截图package test;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import mons.io.FileUtils;import org.openqa.selenium.OutputType;import org.openqa.selenium.TakesScreenshot;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.ie.InternetExplorerDriver;public class Test_ShotScreen public static void main(String args) throws IOException, InterruptedException String url = ;/打开chrome WebDriver dr = new InternetExplorerDriver(); dr.get(url); /这里等待页面加载完成 Thread.sleep(5000); /下面代码是得到截图并保存在D盘下 File screenShotFile = (TakesScreenshot)dr).getScreenshotAs(OutputType.FILE); /FileUtils.copyFile(screenShotFile, new File(D:/test.png); FileUtils.copyFile(screenShotFile, new File(D:AutoScreenCapture + getCurrentDateTime()+ .jpg); dr.quit(); public static String getCurrentDateTime()SimpleDateFormat df = new SimpleDateFormat(yyyyMMdd_HHmmss);/设置日期格式/System.out.println(df.format(new Date();return df.format(new Date();19、webdriver与selenium切换/ You may use any WebDriver implementation. Firefox is used here as an exampleWebDriver driver = new FirefoxDriver();/ A base url, used by selenium to resolve relative URLs String baseUrl = ;/ Create the Selenium implementationSelenium selenium = new WebDriverBackedSelenium(driver, baseUrl);/ Perform actions with seleniumselenium.open();selenium.type(name=q, cheese);selenium.click(name=btnG);/ Get the underlying WebDriver implementation back. This will refer to the/ same WebDriver instance as the driver variable above.WebDriver driverInstance = (WebDriverBackedSelenium) selenium).getWrappedDriver();/Finally, close the browser. Call stop on the WebDriverBackedSelenium instance/instead of calling driver.quit(). Otherwise, the JVM will continue running after/the browser has been closed.selenium.stop();20、webdriver支持sariDesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setBrowserName(safari);CommandExecutor executor = new SeleneseCommandExecutor(new URL(http:/localhost:4444/), new URL(/), capabilities);WebDriver driver = new RemoteWebDriver(executor, capabilities);21、suite之间不应该存在依赖,虽然代码static存在依赖,但是浏览器之间不是同一个driver了。22、如何上传文件Yup the Selenium devs thought about this ages ago. You can just use driver.findElement(By.id(“foo”).sendKeys(“AbsoluteFileLocation”); If the element you are sending text to is and it will upload the file.23、如何下载参考/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/ 24、校验图片driver.get(https:/demo.mynexi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论