网站首页
网站导航
Ctrl+D收藏
首 页
代码段
源码包
文档库
工具箱
代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
selenuim 2+webdriver+Junit 实现自动化登录
作者:
/ 发布于
2016/12/29
/
521
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; } } }
评论列表
本站所提供的代码,版权归原作者所有,若有侵犯作者版权,请与我们联系,我们将立即删除或修改。谢谢!
本站所有代码发布及提供者。
试试其它关键字
同语言下
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
.
根据出生日期和当前时间计算年龄
可能有用的
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
.
根据出生日期和当前时间计算年龄
贡献的其它代码
Label
地图
本站
我们
服务
版权
联系
回馈
博客