我是selenium测试案例的新手,我怀疑在生成的Web驱动程序代码(java; j-unit)之间漫游,以及如何在代码中运行该代码并在项目中发现错误
在此之前我可能知道我们想在代码中做出哪些改变,我们已经生成了什么。对于Eaxmple:
我记录了登录表单页面,生成的Java / Junit 4 / webdriver代码以及来自selenium IDE的验证链接按钮,我将其作为包导入eclipse ...
我在该登录表单中添加了功能或更改,我只是忘记保留链接按钮
使用selenium测试我之前生成的Java / Junit 4 / webdriver代码,我想打印O / P为“未找到链接按钮”
package web;
//import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
//import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;
public class webdriver {
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://localhost/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/CRM/Default.aspx");
driver.findElement(By.id("LoginMain_UserName")).clear();
driver.findElement(By.id("LoginMain_UserName")).sendKeys("Admin");
driver.findElement(By.id("LoginMain_Password")).clear();
driver.findElement(By.id("LoginMain_Password")).sendKeys("xxx");
try {
assertTrue(isElementPresent(By.id("LoginMain_UserNameLabel")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("LoginMain_PasswordLabel")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("LoginMain_UserName")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("LoginMain_Password")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("LoginMain_LoginButton")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("LinkButtonRegister")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
driver.findElement(By.id("LoginMain_LoginButton")).click();
try {
assertEquals("Webdriver Testingest.,", driver.getTitle());
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("ctl00_ctl00_ImageButton1")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.id("ctl00_ctl00_ImageButton2")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.linkText("Home")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.linkText("Sales")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertTrue(isElementPresent(By.linkText("Masters")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
}
@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;
}
}
答案 0 :(得分:1)
为了得到你想要的你应该修改:
try {
assertTrue("Link button not found",isElementPresent(By.id("LinkButtonRegister")));
} catch (Error e) {
//Add here any log message if necesary
System.out.println("Link button not found");
verificationErrors.append(e.toString());
}
希望有所帮助!