webdriver自动化测试

工具下载地址:http://pan.baidu.com/s/1cKC2CY

开发工具eclipse

录制相关脚本:

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Login {
	private WebDriver driver;
	private String baseUrl;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();
	static final String BORSWER_PATH = "src\\test\\resources\\chromedriver.exe";

	static {
		System.setProperty("webdriver.chrome.driver", BORSWER_PATH);
	}

	@Before
	public void setUp() throws Exception {
		ChromeOptions option = new ChromeOptions();
		option.addArguments("-test-type");
		option.addArguments("-start-maximized");// 最大化浏览器窗口
		driver = new ChromeDriver(option);
		baseUrl = "https://www.mmhlive.com";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testLogin() throws Exception {
		driver.get(baseUrl + "/");
//		Thread.sleep(5000);
		driver.findElement(By.linkText("登录")).click();
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys("13611010232");
		driver.findElement(By.id("password")).clear();
		driver.findElement(By.id("password")).sendKeys("010232");
		driver.findElement(By.name("submit")).click();
		Thread.sleep(5000000);
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	private boolean isAlertPresent() {
		try {
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}

依赖jar包

testCompile "org.seleniumhq.selenium:selenium-firefox-driver:2.53.0"
testCompile "org.seleniumhq.selenium:selenium-chrome-driver:2.53.0"

相关截图:





相关文章
相关标签/搜索