gpt4 book ai didi

java - 生成 webdriver 代码后,如何以及在何处修改代码

转载 作者:行者123 更新时间:2023-11-28 20:43:14 24 4
gpt4 key购买 nike

我是 selenium 测试用例的新手,我的疑问在生成的网络驱动程序代码(java;j-unit)和如何运行该代码并对该代码进行一些更改并在项目中发现错误之间徘徊

在那之前我可能知道我们想在代码中的什么地方进行更改,我们已经生成了什么。

例如:

我记录了登录表单页面,使用 selenium IDE 中的验证链接按钮生成了 Java/Junit 4/webdriver 代码,并将其作为包导入到 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;
}
}

最佳答案

要得到你想要的你应该修改:

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

希望对您有所帮助!

关于java - 生成 webdriver 代码后,如何以及在何处修改代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com