我正在运行我的脚本,它会在4秒后终止。已经坚持了几个小时,任何帮助非常感谢。难道我可能在Junit 4脚本中的某个地方使用JUnit 3代码吗?只是一个想法。我也在调用一个单独的PageObjects脚本,这可能是原因吗?代码如下:
package com.testcases;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import jxl.*;
import com.PageObjects.RegisterUserPageObject;
import java.io.File;
import jxl.write.*;
public class RegisterUser {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://newtours.demoaut.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testRegisterUser() throws Exception {
RegisterUserPageObject registerUserPage = PageFactory.initElements (driver, RegisterUserPageObject.class);
Workbook wb = Workbook.getWorkbook(new File("C:\\Users\\mce\\Documents\\Selenium\\Projects\\Mecury Tours\\Data1.xls"));
WritableWorkbook copy = Workbook.createWorkbook(new File("C:\\Users\\mce\\Documents\\Selenium\\Projects\\Mecury Tours\\Data2.xls"),wb);
WritableSheet s = copy.getSheet(0);
for(int i=1;i<s.getRows();i++)
{
driver.get(baseUrl + "/");
driver.findElement(By.xpath("//a[contains(text(),'Register \n here')]")).click();
driver.findElement(By.name("firstName")).sendKeys("Tester");
driver.findElement(By.name("lastName")).clear();
driver.findElement(By.name("lastName")).sendKeys("01");
driver.findElement(By.name("phone")).clear();
driver.findElement(By.name("phone")).sendKeys("010101010");
driver.findElement(By.id("userName")).clear();
driver.findElement(By.id("userName")).sendKeys("test@test.com");
driver.findElement(By.name("address1")).clear();
driver.findElement(By.name("address1")).sendKeys("1 Test Ave");
driver.findElement(By.name("city")).clear();
driver.findElement(By.name("city")).sendKeys("Dublin");
driver.findElement(By.name("state")).clear();
driver.findElement(By.name("state")).sendKeys("Leinster");
new Select(driver.findElement(By.name("country"))).selectByVisibleText("IRELAND");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(s.getWritableCell("A"+i).getContents());
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("confirmPassword")).clear();
driver.findElement(By.name("confirmPassword")).sendKeys("password");
driver.findElement(By.name("register")).click();
driver.findElement(By.linkText("Home")).click();
s.addCell(new Label(4,i,"Executed"));
}
copy.write();
copy.close();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}