WebDriver + TestNG 应用

上一篇 / 下一篇  2012-05-21 21:55:19 / 个人分类:自动化测试

Selenium 2 最大的更新就是集成了WebDriver。这两者是什么关系呢?如果你搜索WebDriver,第一条结果是Selenium。其实WebDriver和Selenium可以说是在实现UI Automation的竞争对手。Selenium是运行在JavaScript的sandbox里面,所以很容易就支持不同的浏览器;而WebDriver则是直接操作浏览器本身,更接近用户的真实操作,但正因为如此,所以WebDriver在多浏览器/操作系统的支持上就要落后于Selenium。不过从Selenium 2开始,这两个项目合并了,可以继续用原来的Selenium,也可以考虑迁移到WebDriver。我个人认为WebDriver应该是以后的大趋势,还是值得迁移的。至于你信不信,我反正是信了。

作为一个轻量级的UI Automation框架,需要写一些驱动它的代码,大部分人会选择JUnit,因为JUnit是单元测试的事实标准;但是我会用TestNG。这些UI Automation的东西,它们本身不是单元测试,而且也没有太多单元测试的风格。

从一段简单的测试开始

public class GoogleTest  {

    @Test

    public void search(ITestContext context) {

        WebDriver driver = new FirefoxDriver();

 

        driver.get("http://www.google.com");

 

        WebElement element = driver.findElement(By.name("q"));

 

        element.sendKeys("magus");

        element.submit();

 

        Assert.assertTrue(driver.getTitle().contains("magus"), "Something wrong with title");                           

    }

}

TestNG应用了Java的Annotations,只需要在测试方法上面打上@Test就可以标示出search是一个测试方法。用TestNG运行测试还需要一个testng.xml的文件,文件名其实可以随便起,没有关系的。

<suite name="Magus demo" verbose="2">

    <test name="Search function">

        <classes>

            <class name="test.GoogleTest">

                <methods>

                    <include name="search" />

                </methods>

            </class>

        </classes>

    </test>

</suite>

我想让测试更加灵活,1. 可以配置使用任意支持的浏览器进行测试;2. 配置所有Google的URL;3. 配置搜索的关键字。修改后的代码:

public class GoogleTest  {

    WebDriver driver;

 

    @Parameters({"browser"})

    @BeforeTest

    public void setupBrowser(String browser){

        if (browser.equals("firefox")){

            driver = new FirefoxDriver();

        } else {

            driver = new ChromeDriver();

        }   

    }   

 

    @Parameters({ "url", "keyword" })

    @Test

    public void search(String url, String keyword, ITestContext context) {        driver.get(url);

        WebElement element = driver.findElement(By.name("q"));

        element.sendKeys(keyword);

        element.submit();

        Assert.assertTrue(driver.getTitle().contains(keyword), "Something wrong with title");        }   

}

Testng.xml

<suite name="Magus demo" verbose="2">

    <parameter name="browser" value="firefox" />

    <parameter name="url" value="http://www.google.com" />

    <parameter name="keyword" value="magus" />

    <test name="Search function" preserve-order="true">

        <classes>

            <class name="test.GoogleTest">

                <methods>

                    <include name="setupBrowser" />

                    <include name="search" />

                </methods>

            </class>

        </classes>

    </test>

</suite>

利用TestNG的@Parameters标签,让测试方法从testng.xml里面读取参数,实现参数化。在testng.xml的配置中,test节点需要增加一个属性的配置: preserve-order=”true”。这个preserve-order默认是false,在节点下面的所有方法的执行顺序是无序的。把它设为true以后就能保证在节点下的方法是按照顺序执行的。TestNG的这个功能可以方便我们在testng.xml里面拼装测试。假设我们有很多独立的测试方法,例如

· navigateCategory

· addComment

· addFriend

· login

· logout

就可以在testng.xml里面拼出不同的测试,例如

<test name="Add friend" preserve-order="true">

    <classes>

        <class name="test.GoogleTest">

            <methods>

                <include name="login" />

                <include name="addFriend" />

                <include name="logout" />

            </methods>

        </class>

    </classes>

</test>

<test name="Add comment to category" preserve-order="true">

    <classes>

        <class name="test.GoogleTest">

            <methods>                <include name="login" />

                <include name="navigateCategory" />

                <include name="addComment" />

                <include name="logout" />

            </methods>

        </class>

    </classes>

</test>

TestNG比JUnit更加适合做一些非单元测试的事情,不是说JUnit不好,而是不能把JUnit当成万能的锤子,到处钉钉子。WebDriver的API比Selenium的更加简洁,会是以后的大趋势。


TAG:

wxxfcda的个人空间 引用 删除 wxxfcda   /   2012-09-06 11:30:29
不会吧,我啥也没设置,它不按照顺序来的啊
原帖由DoDouNSX于2012-08-01 13:20:01发表
写得挺好吧, 不过testng默认是按顺序执行的, By default, TestNG will run your tests in the order the.
引用 删除 DoDouNSX   /   2012-08-01 13:20:01
写得挺好吧, 不过testng默认是按顺序执行的, By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictible order, set the preserve-order attribute to false (http://testng.org/doc/documentation-main.html)
candy_girl的个人空间 引用 删除 candy_girl   /   2012-05-27 11:01:10
原帖由fengzhulin于2012-05-24 10:19:52发表
FAILED CONFIGURATION: @BeforeTest setUp(null, null)
java.lang.RuntimeException: Could not contac.

看错误提示啊, Could not contact Selenium Server; have you started it on 'localhost:4444' ?
Read more at http://seleniumhq.org/projects/remote-control/not-started.html
慢慢悠悠的测试 引用 删除 fengzhulin   /   2012-05-24 10:19:52
FAILED CONFIGURATION: @BeforeTest setUp(null, null)
java.lang.RuntimeException: Could not contact Selenium Server; have you started it on 'localhost:4444' ?
Read more at http://seleniumhq.org/projects/remote-control/not-started.html
Connection refused: connect
        at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:102)
        at com.thoughtworks.selenium.SeleneseTestBase.setUp(SeleneseTestBase.java:134)
        at com.thoughtworks.selenium.SeleneseTestBase.setUp(SeleneseTestBase.java:103)
        at com.thoughtworks.selenium.SeleneseTestNgHelper.setUp(SeleneseTestNgHelper.java:29)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:551)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
        at org.testng.TestRunner.beforeRun(TestRunner.java:641)
        at org.testng.TestRunner.run(TestRunner.java:609)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
        at org.testng.TestNG.run(TestNG.java:1022)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

FAILED CONFIGURATION: @AfterMethod selectDefaultWindow
com.thoughtworks.selenium.SeleniumException: Connection refused: connect
        at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:121)
        at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:101)
        at com.thoughtworks.selenium.DefaultSelenium.selectWindow(DefaultSelenium.java:370)
        at com.thoughtworks.selenium.SeleneseTestNgHelper.selectDefaultWindow(SeleneseTestNgHelper.java:74)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:551)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:783)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:768)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
        at org.testng.TestNG.runSuitesSequentially(TestNG.ja
慢慢悠悠的测试 引用 删除 fengzhulin   /   2012-05-24 10:19:10
你好,按照你的方法做了。但是总报错
[TestNG] Running:
  C:\Documents and Settings\yhd2\Local Settings\Temp\testng-eclipse-804206061\testng-customsuite.xml

com.thoughtworks.selenium.SeleniumException: Connection refused: connect
        at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:121)
        at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:101)
        at com.thoughtworks.selenium.DefaultSelenium.captureScreenshot(DefaultSelenium.java:744)
        at com.thoughtworks.selenium.ScreenshotListener.onTestFailure(ScreenshotListener.java:27)
        at com.thoughtworks.selenium.ScreenshotListener.onConfigurationFailure(ScreenshotListener.java:41)
        at org.testng.internal.Invoker.runConfigurationListeners(Invoker.java:1856)
        at org.testng.internal.Invoker.handleConfigurationFailure(Invoker.java:334)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:237)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
        at org.testng.TestRunner.beforeRun(TestRunner.java:641)
        at org.testng.TestRunner.run(TestRunner.java:609)
 

评分:0

我来说两句

Open Toolbar