5.3 选择浏览器开始测试—Selenium 2

1. 选择浏览器开始测试
要开始测试,首先得创建Selenium的实例,也就是对应的Driver。
如果需要对Firefox进行测试,则需要用到FirefoxDriver,代码如程序清单所示。
packageProject1;
importorg.openqa.selenium.*;//注意这里导入了selenium包中内容
importorg.openqa.selenium.WebDriver.*;//注意这里导入了selenium包中内容
importorg.openqa.selenium.firefox.*;//注意这里导入了selenium包中内容
publicclassProject1Class{
publicstaticvoidmain(String[]args){
//如果启动出现问题,可以使用System.setProperty指出firefox.exe的路径
//System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles(x86)\\MozillaFirefox\\firefox.exe");
WebDriverdriver=newFirefoxDriver();
}
}

使用IE
package Project1;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.ie.*;
public class Project1Class {
public static void main(String[] args) {
//如果启动出现问题,可以使用System.setProperty指明webdriver.ie.driver的路径,webdriver.ie.driver可以在http://code.google.com/p/selenium/downloads/list下载
//System.setProperty("webdriver.ie.driver","E:\\IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
}
}

对Chrome也是一样的,只需将命名空间改为OpenQA.Selenium.Chrome,实例化对象改为new ChromeDriver()即可。
编译并执行程序清单的代码,对应的浏览器将会打开。

2. 浏览器导航对象Navigation
打开了浏览器器之后,就可以打开指定的页面来进行测试了。在Selenium 1中,可以直接通过Selenium的open()方法来打开页面,但在Selenium 2中则不同,要导航页面,需要用到Navigation对象。
可以通过WebDriver的Navigate()方法获得Navigation对象实例
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();

在获取该对象后,就可以执行跳转到指定URL、前进、后退、刷新页面等操作了。

2.1 GoToUrl()/to()
对Java来说,可以使用to()来进行跳转。
navigation.to(http://www.baidu.com);

执行代码,将打开百度主页。
注意:执行GoToUrl()/to()方法时,代码会自动等待页面加载完毕再执行下一句,也就是浏览器状态栏为“完成”时再执行下一句。

2.2 Back()/Forward()
在浏览器上,可以按“前进”和“后退”按钮来进行导航,通过Back()/Forward()方法,也可以实现这种导航功能。
下面将举例说明先打开百度主页,再打开Google主页,之后进行后退和前进操作,代码如程序清单为防止执行过快,每个操作后面加了3秒等待时间Thread.Sleep(3000)。
package Project1;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.*;
public class Project1Class {
public static void main(String[] args) {
//如果启动出现问题,可以使用System.setProperty指出firefox.exe的路径
//System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
navigation.to("http://www.google.com.hk");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
navigation.back();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
navigation.forward();
}
}

程序清单代码执行后可以发现,程序共打开了两个页面:百度和谷歌。然后,页面先后退到了第一个页面(百度),再前进到了第二个页面(谷歌)。

2.3 Refresh()
使用该方法将刷新整个页面(类似于按F5的效果),多用于执行某些操作后需要刷新的情况(例如登录后页面未自动刷新)
package Project1;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.*;
public class Project1Class {
public static void main(String[] args) {
//如果启动出现问题,可以使用System.setProperty指出firefox.exe的路径
//System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
navigation.refresh();
}
}

猜你喜欢

转载自865325772.iteye.com/blog/2050363
5.3