使用Claude和MCP增强Selenium

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次就测试通过了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值