selenium学习笔记
1 、webdriver 与 selenium RC
?
?
2、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("//li[@id='userBtn']/a/img")
?
4、点击链接,使用driver.getTitle()来验证
?
6、IE下出现了ElementNotFoundException ,但FF运行正常
网上查了半天,都说是IE的保护模式,但是貌似只有vista系统,在IE选项->安全里才有那个选项。
重装IE7,问题得到解决。
?
7、火狐出现什么问题呢?
driver.get(www.google.com)
不能通过,要加上http://www.google.com才能够正常运行
?
8、webdriver 如何处理ajax
?
案例:我评论了一条信息,但是页面上显示的是ajax处理的,我要判断页面上是否新增了这条评论。直接判断确拉错了信息(因为AJAX反应没那么快)。
所以采用以下写法,5秒之内判断是否能够成功返回正确值,如果可以择判断运行正确。
?
?
?
boolean result=new WebDriverWait(driver, 5).until (new ExpectedCondition<Boolean>() { 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);?
?
while(true){ if(ele.isDisplayed())break; }for(int second=1;;second++){ if(second>60)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')/div[1]/h2")).getText(); }else{ text=driver.findElement(By.xpath("id('ui-dlg-emailShare')/div[1]/h2")).getText(); } n++; }else{ break; } }
?
10、IE自动保存表单,FF不能,要读取profile
IE-选项-内容-》保存表单
?
11、有关xpath取值不正确
用FF插件弄的是
?By.xpath("/html/body/div[3]/div[3]/div/a"));
?用GOOGLE插件xpathOnClick 这个就能找到element
?
By.xpath("/html/body/div[contains(concat(' ', @class, ' '), ' wrap ')]/div[3]/div[contains(concat(' ', @class, ' '), ' dt_sidbox ')]/a"));
?12、一个神奇的问题
之前用JUNIT做测试,引用了他的包,后来改成了testng,
在做suite的时候,发现运行不了。
解决结果:删除junit包。
使用ant ?build.xml生成的报告也产生了影响。
?
13、chrome最大化
?
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 + "' doesn't exist!")); } return status; }
?16、自定义table
?
public 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常用操作http://blog.csdn.net/xiecj_2006/article/details/7869033
18、截图
?
package test;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.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 = "http://www.51.com";//打开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 = "http://www.google.com";// Create the Selenium implementationSelenium selenium = new WebDriverBackedSelenium(driver, baseUrl);// Perform actions with seleniumselenium.open("http://www.google.com");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支持sari
?
DesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setBrowserName("safari");CommandExecutor executor = new SeleneseCommandExecutor(new URL("http://localhost:4444/"), new URL("http://www.google.com/"), 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.
driver.get("https://demo.mynexia.com/login");?? ? WebElement ele = driver.findElement(By.cssSelector("#content_footer .left"));?? ? String actual = ele.getCssValue("background-image");?
? ? String expected = "/assets/schlage_footer_logo.gif";?
? ? assertTrue(actual.contains(expected));
5、鼠标滑过
有时候遇到了ElementsNotVisible是因为要鼠标滑过触发JS,所以只要执行鼠标动作,再点击就OK了
?
//Perform mouse over action Actions mouseMenu = new Actions(driver); mouseMenu.moveToElement(menu).build().perform();
System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe"); //driver = new FirefoxDriver();
服务器运行java -jar selenium-server-standalone-2.25.0.jar -role hub
节点注册java -jar selenium-server-standalone-2.25.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 6666