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
- selenuim 2+webdriver+Junit 实现自动化登录
- Webdriver UI自动化测试 实现自动登录
- Selenuim RC + Junit 4 +eclipse 配置 自动化测试
- python webdriver实现自动化
- selenium webdriver junit自动化测试架构
- Webdriver自动化测试案例--客户登录
- selenium webdriver + Junit 自动化测试:打开不同浏览器网页
- Selenium IDE录制登录操作,导出Java / Junit 4 / WebDriver
- webdriver自动化
- selenuim
- Selenuim-Python 自动化之安装pip
- 手把手教你搭建 Selenuim 自动化环境
- selenium webdriver实现自动截图与Junit导出report测试报告
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
- python+selenuim webdriver 页面跳转后如何定位元素
- selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
- linux下模块化编程实验
- 在ubuntu 12.04 64位 安装JDK1.6.5
- 20个常用Expression Blend设计开发技巧
- Word2010制作三线表
- 文本文件大小探究
- selenuim 2+webdriver+Junit 实现自动化登录
- 本世纪的工程师应该是个什么样?
- Job found still running after platform shutdown.
- android网络开源框架volley(四)——谈谈图片加载续——九张图片相册的展示(微信微博等)
- 字符数组与字符串指针
- mysql执行sql脚本文件
- RelativeLayout中Button为啥获取不到焦点
- Java Date and Time系列(二)- System.currentTimeMillis()介绍
- HDU--杭电2844--Coins--背包,二进制压缩