1.配置MCP服务器
打开Claude Desktop—>Settings—>Developer—>Edit Config
{
"mcpServers": {
"selenium": {
"command": "npx",
"args": ["-y", "@angiejones/mcp-selenium"]
}
}
}
配置完成后重启Claude Desktop
2.验证安装
3.测试交互
4.生成Selenium脚本
package com.saucedemo.tests;
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.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.Duration;
public class SauceDemoLoginTest {
private WebDriver driver;
private WebDriverWait wait;
// Test data
private static final String BASE_URL = "https://www.saucedemo.com";
private static final String USERNAME = "standard_user";
private static final String PASSWORD = "secret_sauce";
@BeforeMethod
public void setUp() {
// Initialize ChromeDriver
driver = new ChromeDriver();
// Initialize WebDriverWait with 10 seconds timeout
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Maximize browser window
driver.manage().window().maximize();
// Navigate to SauceDemo website
driver.get(BASE_URL);
}
@Test
public void testLoginWithValidCredentials() {
// Find username field and enter username
WebElement usernameField = wait.until(
ExpectedConditions.presenceOfElementLocated(By.id("user-name"))
);
usernameField.sendKeys(USERNAME);
// Find password field and enter password
WebElement passwordField = driver.findElement(By.id("password"));
passwordField.sendKeys(PASSWORD);
// Find login button and click it
WebElement loginButton = driver.findElement(By.id("login-button"));
loginButton.click();
// Verify successful login by checking if we're on the products page
WebElement productsTitle = wait.until(
ExpectedConditions.presenceOfElementLocated(By.className("title"))
);
// Assert that we successfully logged in
Assert.assertEquals(productsTitle.getText(), "Products",
"Login failed - Products page not displayed");
// Additional verification - check URL contains "inventory"
Assert.assertTrue(driver.getCurrentUrl().contains("inventory"),
"Login failed - URL doesn't contain inventory");
System.out.println("Login test passed successfully!");
}
@AfterMethod
public void tearDown() {
// Close the browser
if (driver != null) {
driver.quit();
}
}
}
5.在IDE里面运行
打开Aqua新建Selenium项目
新建SauceDemoLoginTest类
粘贴生成的代码,然后运行
错误原因:TestNG版本和Java 8不兼容
解决方法:打开File—>Project Structure—>Project修改SDK版本
运行测试
可以看到Claude生成的代码1次就测试通过了