|
之前用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管理的文章。 |
|