selenuim 2+webdriver+Junit 实现自动化登录

来源:互联网 发布:mac book air使用教程 编辑:程序博客网 时间:2024/06/14 10:00

以下代码是实现登录自动化,分享出来与大学一起讨论学习:

package com.example.tests;



import java.io.File;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriver;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import java.util.regex.Pattern;


@SuppressWarnings("unused")
public class testeda {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private Object selenium;


@Before
// 打开数据仓库登录界面
public void setUp() throws Exception {
DesiredCapabilities ieCapabilities = DesiredCapabilities
.internetExplorer();
ieCapabilities
.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
driver = new InternetExplorerDriver(ieCapabilities);
WebDriverWait wait = new WebDriverWait(driver, 30);
File file = new File("C:/Windows/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
baseUrl = "http://192.168.*.*";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// driver.get(baseUrl);
driver.navigate().to(baseUrl);


java.util.Set<Cookie> cookies = driver.manage().getCookies();
Cookie[] allCookies = new Cookie[cookies.size()];
cookies.toArray(allCookies);
// 打印已有的cookie数量和内容
System.out.println("当前cookie集合的数量为:" + cookies.size());
System.out.println("");
for (int i = 0; i < allCookies.length; i++) {
System.out.println("第" + (i + 1) + "个cookie的各项属性为:");
System.out.println("cookie名称     - " + allCookies[0].getName());
System.out.println("cookie值       - " + allCookies[0].getValue());
System.out.println("cookie所在域   - " + allCookies[0].getDomain());
System.out.println("cookie路径     - " + allCookies[0].getPath());
System.out.println("cookie过期时间 - " + allCookies[0].getExpiry());
System.out.println("");
}
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.add(java.util.Calendar.DATE, +1); // 获取前一天的日期
java.util.Date date = calendar.getTime();
// 添加cookie
Cookie newCookie = new Cookie("newcookie", "新cookie值", "baidu.com", "",
date);
cookies.add(newCookie);
System.out.println("新增的cookie的各项属性为:");
System.out.println("cookie名称     - " + newCookie.getName());
System.out.println("cookie值       - " + newCookie.getValue());
System.out.println("cookie所在域   - " + newCookie.getDomain());
System.out.println("cookie路径     - " + newCookie.getPath());
System.out.println("cookie过期时间 - " + newCookie.getExpiry());
System.out.println("");
// 添加后显示cookie数量
System.out.println("添加cookie后,cookie集合的数量为:" + cookies.size());
System.out.println("");
// 删除cookie,先找新添加的cookie,然后删除
allCookies = new Cookie[cookies.size()];
cookies.toArray(allCookies);
cookies.remove(allCookies[1]);
// 删除后显示cookie数量
System.out.println("删除cookie后,cookie集合的数量为:" + cookies.size());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


@Test
public void testLogin() throws Exception {
// 定义用户名与密码:
driver.switchTo().frame("loginf");
WebElement username = driver.findElement(By.name("staffCode"));
WebElement password = driver.findElement(By.name("password"));
WebElement checkcode = driver.findElement(By.name("checkCode"));
// 输入用户名、密码与验证码:
username.sendKeys("edatest");
password.sendKeys("123456@ffcs");
checkcode.sendKeys("abcd");
// 点击login登录:
WebElement login = driver.findElement(By.id("dl"));
login.click();
// 设置页面等待直到出现界面:
(new WebDriverWait(driver, 500))
.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver dr) {
return dr.findElement(By.id("workMsgTitle"));
}
});
// 得到title
String title = driver.getTitle();
// 得到当前页面url
String currentUrl = driver.getCurrentUrl();
// 输出title和currenturl
System.out.println(title + "\n" + currentUrl);


((JavascriptExecutor) driver)
.executeScript("alert(\"hello,this is a alert!\")");
// System.out.println(driver.switchTo().alert().getText());
// driver.switchTo().alert().sendKeys("请输入!");
}


@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;
}
}
}
0 0
原创粉丝点击