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次就測試通過了