Selenium之WebDriver(一)

之前用过selenium 1.0的API做过测试,最近发现waitForPageToLoad方法不起作用了?版本是2.41.0,决定试用一下WebDriver。以下测试环境均在Windows7系统上进行。

尝试了下,官方文档里的demo,代码如下:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class DriverTest {

    public static void main(String[] args) throws Exception {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface,
        // not the implementation.
        WebDriver driver = new FirefoxDriver();
        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");
        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));
        // Enter something to search for
        element.sendKeys("Cheese!");
        // Now submit the form. WebDriver will find the form for us from the
        // element
        element.submit();
        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        // Google’s search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });
        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        // Close the browser
        driver.quit();
    }
}

这段代码在Windows7上运行正确,但如果把WebDriver换成 org.openqa.selenium.ie.InternetExplorerDriver,会得到如下错误:

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the 
webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver. 
The latest version can be downloaded from http://selenium-release.storage.googleapis.com/index.html
	at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
	at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
	at org.openqa.selenium.ie.InternetExplorerDriverService.access$1(InternetExplorerDriverService.java:1)
	at org.openqa.selenium.ie.InternetExplorerDriverService$Builder.build(InternetExplorerDriverService.java:230)
	at org.openqa.selenium.ie.InternetExplorerDriver.setupService(InternetExplorerDriver.java:250)
	at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:171)
	at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:146)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at thoughtworks.DriverTest.main(DriverTest.java:21)

这是因为没有正确设置IE WebDriver驱动,网上给出的解决方案是在代码中设置property:

System.setProperty("webdriver.ie.driver", "D:/selenium/IEDriverServer.exe");

这里给出更方便有效的方案--设置VM参数:-Dwebdriver.ie.driver=D:/selenium/IEDriverServer.exe,如果是用Eclipse调试的话,在菜单Run->Run Configuration...,选择{project},然后在Arguments->VM arguments中设置webdriver.ie.driver VM参数。这样的代码更灵活。

另外demo代码中hardcode了具体的WebDriver,也不利于代码的灵活使用,可以用自定义VM参数webdriver.driver来指定具体的webdriver class,然后通过反射来动态创建。比如设置-Dwebdriver.driver=org.openqa.selenium.ie.InternetExplorerDriver来指定使用IE驱动,相应的代码可以修改如下:

        Class<?> cls = Class.forName(System.getProperty("webdriver.driver"));
        WebDriver driver = (WebDriver)cls.newInstance();
这样我们可以在不修改代码的情况下切换WebDriver。


Refer:

http://www.cnblogs.com/dream0577/archive/2012/10/07/2714579.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值