java+selenium环境部署

1、下载IDEA,
2、下载chromedriver,项目中会用到,下载地址:http://chromedriver.chromium.org/downloads
3、新建一个maven项目
3、配置pom.xml文件

 加一个标签
 <dependencies></dependencies>
标签内输入
     < dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.2</version>
        </dependency>
保存成功后,会自动下载下载设置的selenium版本的包和testng的包。
4、实现登录功能

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestHoutai {
    public WebDriver driver;
    String url="http://baidu.com";
    @Test
    public void baiduSearch(){
        driver.get(url+"/");
        WebElement input=driver.findElement(By.id("kw"));
        input.sendKeys("csdn");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        driver.findElement(By.id("su")).click()}
    @BeforeMethod
    public void beforMethod(){
        System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        driver=new ChromeDriver();
    }
    @AfterTest
    public void afterTest(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        driver.quit();
    }
}

猜你喜欢

转载自blog.csdn.net/adawyy/article/details/83183646