selenium自动化测试实现对网站的登陆(使用java编写)

package brand;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrandLogin {
	private WebDriver driver;
	private Navigation navigation;
	private String baseUrl = "完整URL地址,需加http";

	@Test
	public void brandLogin() {
                //设置firefox浏览器的位置
		System.setProperty("webdriver.firefox.bin",
				"D:/programs/Mozilla Firefox/firefox.exe");
		
                //创建WebDriver对象
		driver = new FirefoxDriver();
		navigation = driver.navigate();
		
		//加载到指定url
		navigation.to(baseUrl);
		
		//获取输入框的id,并在输入框中输入用户名
		WebElement loginInput = driver.findElement(By.id("uname"));
		loginInput.sendKeys("输入网站的用户名");
		
		//获取输入框的id,并在输入框中输入密码
		WebElement pwdInput = driver.findElement(By.id("upwd"));
		pwdInput.sendKeys("输入网站的密码");
		
		//获取登陆按钮的className,并点击
		WebElement loginBtn = driver.findElement(By.className("LBtn"));
		loginBtn.click();
	}
}

你可能感兴趣的:(自动化测试,java)