[转载]使用Selenium2测试含有iframe的Ajax网页
原文地址:http://www.cnblogs.com/hexin0614/archive/2012/03/24/2415670.html
1.设置 Chrome 启动参数的实例脚本源代码, 忽略安全证书:
1 public static ChromeOptions chromeOptions(){
2 ChromeOptions options = new ChromeOptions();
3 DesiredCapabilities capabilities = DesiredCapabilities.chrome();
4 capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
5 options.addArguments("--test-type", "--start-maximized");
6 options.addArguments("--test-type", "--ignore-certificate-errors");
7
8 return options;
1 <span style="white-space: normal; ">test.html</span>
2 <html>
3 <head><title>Test Popup Window</title></head>
4 <body>
5 <a id = "51" href = "http://www.51.com/" target = "_blank">Let's go!</a>
6 </body>
7 </html>
import java.util.Iterator;
import java.util.Set; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class PopupWindowTest {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
String url ="\\Your\\Path\\to\\main.html";
dr.get(url);
dr.findElement(By.id("51")).click(); //得到当前窗口的句柄
String currentWindow = dr.getWindowHandle();
//得到所有窗口的句柄
Set<String> handles = dr.getWindowHandles(); Iterator<String> it = handles.iterator();
while(it.hasNext()){
String handle = Iter.next();
if(currentWindow.equals(handle)) continue;
WebDriver window = dr.switchTo().window(handle);
System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());
}
} }
如果scrollTop=0 时,滚动条就会默认在最上方 javascript{this.browserbot.getUserWindow()document.getElementById("text").scrollTop=10000}
#coding=utf-8 from selenium import webdriver import time #访问百度 driver=webdriver.Firefox() driver.get("http://www.baidu.com") #搜索 driver.find_element_by_id("kw").send_keys("selenium") driver.find_element_by_id("su").click() time.sleep(3) #将页面滚动条拖到底部 js="var q=document.documentElement.scrollTop=10000" driver.execute_script(js) time.sleep(3) #将滚动条移动到页面的顶部 js="var q=document.documentElement.scrollTop=0" driver.execute_script(js) time.sleep(3) driver.quit()
1 WebElement target = driver.findElement(By.xpath(xPathExpression)); 2 ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", target);
package com.example.tests;
Naming convention 文件命名规范
一般采用,对像加行为的描述,也用附加特定条件或范围的。也可以将行为放在前面。 DRY 原则 (imperative or declarative)
将相同步骤cucumber 变成一个step,即复用你的cucumber steps Cucumber steps基本规范
Cucumber的steps尽量用人类看得懂的语言,所有 正则表达式,css, xpath,等各种代码不应该在cucumber steps中出现。需要时,就另外写一个steps封装一下就好。 是否按标准的User story写背景和description
一种写法是 As xxx Role, I would do yyy so that zzz 这种写法会对项目新成员会有一定的帮助,不好的地方就是维护成本了,所以那几个开源项目都没有严格按照User story的写法,只是简单完成即可。我偏向是简单写,直观明了。 Capybara
这个应该是默认的了,The cucumber book中,提及到为什么不用webrat和selenium,在这里就不废话了。
运行单个cucumber
cucumber features/billing/credit_card.feature:104 -f progress -r features
Cucumber rerun 重复运行出错的scenario
$ cucumber -f rerun --out rerun.txt
#结果 features/one.feature:367 features/another.feature:91:117