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 dataprivate static final String BASE_URL = "https://www.saucedemo.com";private static final String USERNAME = "standard_user";private static final String PASSWORD = "secret_sauce";@BeforeMethodpublic void setUp() {// Initialize ChromeDriverdriver = new ChromeDriver();// Initialize WebDriverWait with 10 seconds timeoutwait = new WebDriverWait(driver, Duration.ofSeconds(10));// Maximize browser windowdriver.manage().window().maximize();// Navigate to SauceDemo websitedriver.get(BASE_URL);}@Testpublic void testLoginWithValidCredentials() {// Find username field and enter usernameWebElement usernameField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("user-name")));usernameField.sendKeys(USERNAME);// Find password field and enter passwordWebElement passwordField = driver.findElement(By.id("password"));passwordField.sendKeys(PASSWORD);// Find login button and click itWebElement loginButton = driver.findElement(By.id("login-button"));loginButton.click();// Verify successful login by checking if we're on the products pageWebElement productsTitle = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("title")));// Assert that we successfully logged inAssert.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!");}@AfterMethodpublic void tearDown() {// Close the browserif (driver != null) {driver.quit();}}
}
5.在IDE里面运行
打开Aqua新建Selenium项目
新建SauceDemoLoginTest类
粘贴生成的代码,然后运行
错误原因:TestNG版本和Java 8不兼容
解决方法:打开File—>Project Structure—>Project修改SDK版本
运行测试
可以看到Claude生成的代码1次就测试通过了