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();
}