51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3894|回复: 6
打印 上一主题 下一主题

webdriver和testng如何一起用?

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2012-5-2 10:09:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
之前用testng+selenium RC;
但是用webdriver后,和testng也和原来一样结合使用么?用webdriver的话,testcase如何管理呢?

我现在的代码如下:
package yhd;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.SeleneseTestNgHelper;
import com.thoughtworks.selenium.Selenium;
import com.beust.testng.*;
import org.testng.annotations.*;
public class mailTest extends SeleneseTestNgHelper{
        public WebDriver driver;
        public String baseUrl;       
        @BeforeSuite
                public void setUp(){
                 driver = new FirefoxDriver();
                 baseUrl="http://mail.163.com/";
                 driver.get(baseUrl);
                 try {
                        Thread.sleep(3000);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                        }
                @Test
                public void test(){
                WebElement elementUserName=driver.findElement(By.id("idInput"));
        elementUserName.sendKeys("username");
                WebElement elementPWD=driver.findElement(By.id("pwdInput"));
                elementPWD.sendKeys("password");
                }

                @AfterSuite
                public void tearDown() {
                        //selenium.stop();
                        driver.close();
                }
       
}

testng.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test" preserve-order="true">
    <classes>
      <class name="yhd.mailTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

但是执行后总是报错如下:
[TestNG] Running:
  C:\Documents and Settings\yhd2\Local Settings\Temp\testng-eclipse--1891114995\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)
        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)

之前用seleniumRC的代码如下,且运行是正常的:
public class Firefox extends SeleneseTestNgHelper {
        @BeforeSuite(alwaysRun = true)
        public void setupBeforeSuite() throws Throwable {
                   RemoteControlConfiguration cfg = new RemoteControlConfiguration();
                File filefoxProfile=new File("D:\\firefox-profile");
                cfg.setPort(4444);
                cfg.setBrowserSideLogEnabled(true);
        cfg.setSingleWindow(true);
                cfg.setFirefoxProfileTemplate(filefoxProfile);
                SeleniumServer server = null;
                try {
                        server = new SeleniumServer(cfg);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                server.boot();
  }
       
        @BeforeTest
        public void setUp() throws Exception {
        //        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.baidu.com/");
                selenium = new DefaultSelenium("localhost", 4444, "*exploreproxy", "http://www.yihaodian.com/");
                selenium.start();       
                selenium.windowMaximize();
        }
        @AfterTest
        public void tearDown() throws Exception {
       selenium.stop();
       System.out.println("!!!!selenium stop!!!!");
        }
请大家指导下,webdriver到底怎么用以及case如何管理,我看了网上的资料基本都是单独介绍webdriver的,很少有和selenium结合使用以及关于case管理的文章。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2012-5-12 13:45:37 | 只看该作者
真的谢谢了!
回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2012-5-22 14:36:29 | 只看该作者
可是没人帮我解决问题呀。请假大虾们啊;;;
如果觉得帖子还可以帮我顶顶,。谢谢
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2012-7-7 16:51:11 | 只看该作者
同问~,期待中
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2012-7-19 00:04:25 | 只看该作者
回复 支持 反对

使用道具 举报

该用户从未签到

6#
发表于 2012-7-19 00:04:47 | 只看该作者
教程
回复 支持 反对

使用道具 举报

该用户从未签到

7#
发表于 2012-7-19 00:05:22 | 只看该作者
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-10 14:25 , Processed in 0.075035 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表