以Java格式的脚本,在JUnit框架上执行和调试这些脚本。
项目语言与架构选项为
在pom.xml中添加的依赖
在kotlin目录中添加Java类,开始编写测试代码:
找到chrome的版本并下载对应版本的chromedriver,在setProperty中将chromedriver.exe的路径完善后就可以开始测试。
chromedriver.storage.googleapis.com/index.html
ChromeDriver Latest Releases Versions Downloads - Chrome for Testing availability (getwebdriver.com)
以下是包括了13个测试类的代码:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SauceDemoTest {
private WebDriver driver;
@Before
public void setUp() {
// 设置ChromeDriver路径
System.setProperty("webdriver.chrome.driver", "//path//chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.saucedemo.com/");
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
// 测试用例
// 登录成功
@Test
public void testLoginWithValidCredentials() {
WebElement usernameField = driver.findElement(By.id("user-name"));
WebElement passwordField = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("login-button"));
usernameField.sendKeys("standard_user");
passwordField.sendKeys("secret_sauce");
loginButton.click();
assertTrue(driver.findElement(By.className("title")).isDisplayed());
}
// 登录失败
@Test
public void testLoginWithInvalidCredentials() {
WebElement usernameField = driver.findElement(By.id("user-name"));
WebElement passwordField = driver.findElement(By.id("password"));
WebElement loginButton = driver.findElement(By.id("login-button"));
usernameField.sendKeys("invalid_user");
passwordField.sendKeys("invalid_password");
loginButton.click();
assertTrue(driver.findElement(By.xpath("//h3[contains(text(),'Epic sadface')]")).isDisplayed());
}
// 登录失败,输入空用户名和密码
@Test
public void testLoginWithEmptyCredentials() {
WebElement loginButton = driver.findElement(By.id("login-button"));
loginButton.click();
assertTrue(driver.findElement(By.xpath("//h3[contains(text(),'Epic sadface')]")).isDisplayed());
}
// 添加商品到购物车
@Test
public void testAddProductToCart() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
// 获取商品添加到购物车按钮
WebElement addToCartButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Add to cart')]")));
addToCartButton.click();
// 获取购物车数量
WebElement cartBadge = wait.until(ExpectedConditions.elementToBeClickable(By.className("shopping_cart_badge")));
assertEquals("1", cartBadge.getText());
}
//验证购物车数量更新
@Test
public void testRemoveProductFromCart() {
testAddProductToCart();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement removeFromCartButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Remove')]")));
removeFromCartButton.click();
boolean isCartBadgePresent = driver.findElements(By.className("shopping_cart_badge")).size() > 0;
assertTrue(!isCartBadgePresent);
}
//验证购物车数量更新
// @Test
// public void testUpdateProductQuantityInCart() {
// testAddProductToCart();
// WebElement cartButton = driver.findElement(By.className("shopping_cart_link"));
// cartButton.click();
//
// WebElement quantityField = driver.findElement(By.className("cart_quantity"));
// assertEquals("1", quantityField.getText());
//
// WebElement continueShoppingButton = driver.findElement(By.id("continue-shopping"));
// continueShoppingButton.click();
//
// WebElement addToCartButton = driver.findElement(By.xpath("//button[contains(text(),'Add to cart')]"));
// addToCartButton.click();
//
// cartButton.click();
// quantityField = driver.findElement(By.className("cart_quantity"));
// assertEquals("2", quantityField.getText());
// }
//验证购物车总价计算
@Test
public void testCartTotalPriceCalculation() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement addToCartButton1 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Add to cart')]")));
addToCartButton1.click();
WebElement addToCartButton2 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Add to cart')]")));
addToCartButton2.click();
WebElement cartButton = wait.until(ExpectedConditions.elementToBeClickable(By.className("shopping_cart_link")));
cartButton.click();
// 获取第一个商品价格
List<WebElement> productPrices = driver.findElements(By.className("inventory_item_price"));
// 获取第一个商品价格
WebElement firstItemPrice = productPrices.get(0);
// 获取第二个商品价格
WebElement secondItemPrice = productPrices.get(1);
// 现在你可以对这两个元素进行操作了,例如打印它们的文本内容
System.out.println("First item price: " + firstItemPrice.getText());
System.out.println("Second item price: " + secondItemPrice.getText());
// 计算总价
double totalPrice = Double.parseDouble(firstItemPrice.getText().substring(1)) +
Double.parseDouble(secondItemPrice.getText().substring(1));
//System.out.println("预期结果: "+ "Item total: $" + totalPrice);
// 验证总价标签内容
WebElement checkoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("checkout")));
checkoutButton.click();
WebElement firstNameField = driver.findElement(By.id("first-name"));
WebElement lastNameField = driver.findElement(By.id("last-name"));
WebElement postalCodeField = driver.findElement(By.id("postal-code"));
WebElement continueButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("continue")));
firstNameField.sendKeys("John");
lastNameField.sendKeys("Doe");
postalCodeField.sendKeys("12345");
continueButton.click();
// WebElement finishButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("finish")));
// finishButton.click();
// 获取总价标签
WebElement total = wait.until(ExpectedConditions.elementToBeClickable(By.className("summary_subtotal_label")));
//WebElement total = driver.findElement(By.className("summary_total_label"));
// 验证总价标签内容
//System.out.println("实际结果: " + total.getText());
assertEquals(String.format("Item total: $%.2f",+ totalPrice), total.getText());
}
@Test
public void testCartTotalPriceCalculation1() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement addToCartButton1 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Add to cart')]")));
addToCartButton1.click();
WebElement addToCartButton2 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Add to cart')]")));
addToCartButton2.click();
WebElement cartButton = wait.until(ExpectedConditions.elementToBeClickable(By.className("shopping_cart_link")));
cartButton.click();
// 获取第一个商品价格
WebElement firstItemPrice = driver.findElement(By.className("inventory_item_price"));
WebElement secondItemPrice = driver.findElement(By.className("inventory_item_price"));
// 计算总价
double totalPrice = Double.parseDouble(firstItemPrice.getText().substring(1)) +
Double.parseDouble(secondItemPrice.getText().substring(1));
// 验证总价标签内容
WebElement checkoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("checkout")));
checkoutButton.click();
WebElement firstNameField = driver.findElement(By.id("first-name"));
WebElement lastNameField = driver.findElement(By.id("last-name"));
WebElement postalCodeField = driver.findElement(By.id("postal-code"));
WebElement continueButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("continue")));
firstNameField.sendKeys("John");
lastNameField.sendKeys("Doe");
postalCodeField.sendKeys("12345");
continueButton.click();
// WebElement finishButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("finish")));
// finishButton.click();
// 获取总价标签
WebElement total = wait.until(ExpectedConditions.elementToBeClickable(By.className("summary_subtotal_label")));
//WebElement total = driver.findElement(By.className("summary_total_label"));
// 验证总价标签内容
assertEquals(String.format("Item total: $%.2f",+ totalPrice), total.getText());
}
//验证结账流程
@Test
public void testCheckoutProcess() {
testAddProductToCart();
//找到购物车按钮
WebElement cartButton = driver.findElement(By.className("shopping_cart_link"));
cartButton.click();
//找到结账按钮
WebElement checkoutButton = driver.findElement(By.id("checkout"));
checkoutButton.click();
//找名字等输入框
WebElement firstNameField = driver.findElement(By.id("first-name"));
WebElement lastNameField = driver.findElement(By.id("last-name"));
WebElement postalCodeField = driver.findElement(By.id("postal-code"));
WebElement continueButton = driver.findElement(By.id("continue"));
//输入名字等信息
firstNameField.sendKeys("John");
lastNameField.sendKeys("Doe");
postalCodeField.sendKeys("12345");
continueButton.click();
//找到完成按钮
WebElement finishButton = driver.findElement(By.id("finish"));
finishButton.click();
//找到确认信息
WebElement confirmationMessage = driver.findElement(By.className("complete-header"));
assertEquals("Thank you for your order!", confirmationMessage.getText());
}
//验证产品列表
@Test
public void testViewProductDetails() {
testLoginWithValidCredentials();
WebElement productLink = driver.findElement(By.className("inventory_item_name"));
productLink.click();
WebElement productTitle = driver.findElement(By.className("inventory_details_name"));
assertTrue(productTitle.isDisplayed());
}
//验证产品排序
@Test
public void testProductSorting() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebElement sortDropdown = driver.findElement(By.className("product_sort_container"));
sortDropdown.click();
// 找到并点击价格排序按钮
WebElement sortOption = driver.findElement(By.xpath("//option[contains(text(),'Price (low to high)')]"));
sortOption.click();
//找到第一个产品价格
List<WebElement> productPrices = driver.findElements(By.className("inventory_item_price"));
// 获取第一个商品价格
WebElement firstProductPrice = productPrices.get(0);
// 获取第二个商品价格
WebElement secondProductPrice = productPrices.get(productPrices.size() - 1);
double firstPrice = Double.parseDouble(firstProductPrice.getText().substring(1));
double secondPrice = Double.parseDouble(secondProductPrice.getText().substring(1));
assertTrue(firstPrice <= secondPrice);
}
//验证页面布局
@Test
public void testPageLayout() {
testLoginWithValidCredentials();
WebElement header = driver.findElement(By.className("header_secondary_container"));
WebElement footer = driver.findElement(By.className("footer"));
assertTrue(header.isDisplayed());
assertTrue(footer.isDisplayed());
}
//点击登录按钮,跳转到登录页面
@Test
public void testButtonFunctionality() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
// 等待菜单按钮变得可点击,然后点击它
WebElement menuButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("react-burger-menu-btn")));
menuButton.click();
// 等待登录按钮变得可见并且可点击,然后点击它
WebElement logoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("logout_sidebar_link")));
logoutButton.click();
// 找到登录按钮并验证它是否可见
WebElement loginButton = driver.findElement(By.id("login-button"));
assertTrue(loginButton.isDisplayed());
}
//点击twitter链接,跳转到twitter页面
@Test
public void testLinkNavigation() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
// 等待菜单按钮变得可点击,然后点击它
WebElement twitterLink = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(@href, 'twitter')]")));
twitterLink.click();
// 等待URL包含特定的字符串
wait.until(ExpectedConditions.urlContains("twitter.com"));
// 获取当前URL并进行断言
String currentUrl = driver.getCurrentUrl();
assertTrue(currentUrl.contains("twitter.com"));//国内禁止访问twitter因此这里用contains判断后会无法访问,属于测试正常失败
}
//测试导航到about页面
@Test
public void testNavigationToAboutPage() {
testLoginWithValidCredentials();
// 创建一个WebDriverWait对象,设置最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
// 等待菜单按钮变得可点击,然后点击它
WebElement menuButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("react-burger-menu-btn")));
menuButton.click();
// 等待关于链接变得可见并且可点击,然后点击它
WebElement aboutLink = wait.until(ExpectedConditions.elementToBeClickable(By.id("about_sidebar_link")));
aboutLink.click();
// 等待URL包含特定的字符串
wait.until(ExpectedConditions.urlContains("saucelabs.com"));
// 获取当前URL并进行断言
String currentUrl = driver.getCurrentUrl();
assertTrue(currentUrl.contains("saucelabs.com"));
}
//测试搜索产品 这网页就没搜索框你搜啥呢?
// @Test
// public void testSearchProduct() {
// testLoginWithValidCredentials();
//
// WebElement searchBox = driver.findElement(By.id("search-box"));
// searchBox.sendKeys("Backpack");
//
// WebElement searchButton = driver.findElement(By.id("search-button"));
// searchButton.click();
//
// WebElement product = driver.findElement(By.xpath("//div[contains(text(),'Backpack')]"));
// assertTrue(product.isDisplayed());
// }
}
标签:webdriver,Web,driver,WebElement,功能测试,findElement,id,click,wait From: https://blog.csdn.net/weixin_69904753/article/details/139232446