我已经在被测试的应用程序中填写了一个输入字段。现在,我希望通过编辑表单来验证输入的字段是否正确保存。我希望使用Selenium来实现这一点。
有人能给出方法和完整的代码建议,比如如何继续吗?
发布于 2023-02-16 06:10:11
要验证使用Selenium填充的应用程序中的字段,可以执行以下步骤:
下面是一个示例代码()让您开始工作:
进口org.openqa.selenium.By;进口org.openqa.selenium.WebDriver;进口org.openqa.selenium.WebElement;进口org.openqa.selenium.chrome.ChromeDriver;
公共类FormTest { public静态void (String[] args) { //设置WebDriver并导航到应用程序System.setProperty("webdriver.chrome.driver","/path/ to /chromedriver");WebDriver驱动程序=新ChromeDriver();driver.get(“http://your-application-url.com“”);
// Fill out and save the form
WebElement nameField = driver.findElement(By.id("name"));
nameField.sendKeys("John Doe");
WebElement emailField = driver.findElement(By.id("email"));
emailField.sendKeys("johndoe@example.com");
WebElement saveButton = driver.findElement(By.id("save-button"));
saveButton.click();
// Edit the form and verify the updated information
WebElement editButton = driver.findElement(By.id("edit-button"));
editButton.click();
WebElement newNameField = driver.findElement(By.id("name"));
newNameField.clear();
newNameField.sendKeys("Jane Doe");
WebElement newEmailField = driver.findElement(By.id("email"));
newEmailField.clear();
newEmailField.sendKeys("janedoe@example.com");
saveButton.click();
String updatedName = driver.findElement(By.id("name")).getAttribute("value");
String updatedEmail = driver.findElement(By.id("email")).getAttribute("value");
if (updatedName.equals("Jane Doe") && updatedEmail.equals("janedoe@example.com")) {
System.out.println("Form updated successfully.");
} else {
System.out.println("Form update failed.");
}
driver.quit();
}}
您需要将/path/to/chromedriver替换为机器上的ChromeDriver文件的实际路径,并更新元素选择器(例如,By.id("name")、By.id("email")等)。以匹配您的应用程序中的内容。
https://sqa.stackexchange.com/questions/51267
复制相似问题