Selenium之偷懒教程
进来一直停留在基础理论知识的学习中,认为太乏味,就写了一个网页自己主动化的demo:自己主动写日报。省的以后自己打开网页写啦。
直接上代码:
自己主动填写日报DEMO
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import javax.mail.MessagingException;
import javax.mail.internet.AddressException; import junit.framework.TestCase; import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select; public class AutoLog extends TestCase {
private static final String URL = "your log website";
private static int mHours = 8;
private static final String CONTENT_FILE = System.getProperty("user.dir")
+ File.separator + "config" + File.separator + "logcontent.txt";
private static final String DRIVER_FILE = System.getProperty("user.dir")
+ File.separator + "driver" + File.separator + "IEDriverServer.exe";
private static String mContext = "Job :";
private static String mDate = "1900-1-1";
private WebDriver mDriver = null;
private String mFlag = "FAIL"; @Override
public void setUp() throws Exception {
calculateHours();
config();
} @Test
public void test_WriteLog() {
try {
System.setProperty("webdriver.ie.driver", DRIVER_FILE);
mDriver = new InternetExplorerDriver();
mDriver.get(URL); Select select = new Select(mDriver.findElement(By
.id("task_ProductName")));
select.selectByVisibleText("iTest");
Select select1 = new Select(mDriver.findElement(By
.id("field_Class_1")));
select1.selectByVisibleText("Test_Development");
Select select2 = new Select(mDriver.findElement(By
.id("field_Class_2")));
select2.selectByVisibleText("Coding");
WebElement text = mDriver.findElement(By.id("field_CostTime"));
text.sendKeys(mHours + "");
mDriver.switchTo().frame("contentFrame");
mDriver.switchTo().frame(0);
WebElement content = mDriver
.findElement(By.className("ke-content"));
content.click();
content.sendKeys(mContext); mDriver.switchTo().defaultContent();
WebElement submit = mDriver.findElement(By.name("submit2"));
// submit.click(); // WebElement table =
// mDriver.findElements(By.id("SubmitForm")).get(1)
// .findElement(By.id("myTodoTable"));
WebElement table = mDriver.findElement(By.id("this_last"))
.findElement(By.tagName("table"));
WebElement tr = table.findElement(By.tagName("tbody")).findElement(
By.tagName("tr"));
if (mDate
.equals(tr.findElements(By.tagName("td")).get(1).getText())) {
mFlag = "SUCCESS[" + tr.getText() + "]";
} } catch (Exception e) {
e.printStackTrace();
mFlag = "EXCEPTION";
} finally {
sendMail(mFlag); }
} public void tearDown() throws Exception {
mDriver.quit();
} public void calculateHours() {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
mDate = df.format(date);
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
mHours = hour - 9 - 1;
minute = minute < 30 ? 0 : 1;
mHours += minute;
mHours = mHours < 8 ? 8 : mHours;
} public void config() {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
CONTENT_FILE), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
mContext += sb;
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public void sendMail(String result) {
MailSender javaEmail = new MailSender();
javaEmail.setMailServerProperties();
try {
javaEmail.createEmailMessage(result);
javaEmail.sendEmail();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
以下说一下流程。
1.新建java项目
导入selenium-server-standalone-2.42.2.jar、mailapi.jar、smtp.jar和Junit库。第一个jar包为selenium-webdriver依赖jar包,后两个为发送邮件用的。
2.下载IEDriverServer
由于我用的是IE。所以我须要在代码里配置IE的驱动程序。
前提是要下到本地。分32位的和64位,我刚開始由于下错64位的,老是启动不了浏览器,折腾了非常长时间。所以看好机器再下载。我这里上传了32位的IEDriverServer。
3.代码编写
首先启动浏览器:第一步要把你的IEDriverServer的文件夹设置到环境变量中,然后启动浏览器,输入网址。
System.setProperty("webdriver.ie.driver", DRIVER_FILE);
mDriver = new InternetExplorerDriver();
mDriver.get(URL);
等待网页载入完成以后就是以下 的样子:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
然后就是定位我所要选择和填写的控件:首先3个选择框,通过selenium的select对象定位并选择我要的,
Select select = new Select(mDriver.findElement(By.id("task_ProductName")));
select.selectByVisibleText("iTest");
Select select1 = new Select(mDriver.findElement(By.id("field_Class_1")));
select1.selectByVisibleText("Test_Development");
Select select2 = new Select(mDriver.findElement(By.id("field_Class_2")));
select2.selectByVisibleText("Coding");
查看页面控件元素的方式是,在当前页面上按F12.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
然后按一下工具中的光标button,点击你想看的控件。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
所以上面三个选择框都是通过id来定位的。
以下来定位时间输入框:
WebElement text = mDriver.findElement(By.id("field_CostTime"));
text.sendKeys(mHours + "");
这个非常easy。以id来定位。时间的计算我是通过实时来计算的。以9点上班为准。计算当前时间与9点的差值。
每一个公司的规章制度不一,每一个公司计算方式也不一样的。
然后就填写日报内容,我用的方式是把日报的内容写在一个txt文档中,然后读这个文档写。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
可是定位日报输入框有点麻烦。由于它是存在于iframe中的。直接定位不到。
所以先得转换到iframe中,contentFrame中又包括了一个iframe。然后还得转。这个时候才真正的转到了class名为ke-edit-iframe中。
然后才干定位输入框。
mDriver.switchTo().frame("contentFrame");
mDriver.switchTo().frame(0);
WebElement content = mDriver.findElement(By.className("ke-content"));
content.click();
content.sendKeys(mContext);
填完上面的内容以后就要提交啦。可是提交button却不在刚才的iframe中。
所以还得先转换到当前上下文。然后再定位提交button。
mDriver.switchTo().defaultContent();
WebElement submit = mDriver.findElement(By.name("submit2"));
submit.click();
好,到这一步就算完毕了。
可是做为一个case。自然要有checkpoint。所以我在代码的后面加了一个推断,推断我是否成功填写日报,然后将结果发送到我的邮箱中。
WebElement table = mDriver.findElement(By.id("this_last"))
.findElement(By.tagName("table"));
WebElement tr = table.findElement(By.tagName("tbody")).findElement(
By.tagName("tr"));
if (mDate
.equals(tr.findElements(By.tagName("td")).get(1).getText())) {
mFlag = "SUCCESS[" + tr.getText() + "]";
} sendMail(mFlag);
Selenium之偷懒教程的更多相关文章
- Selenium IDE 基础教程
Selenium IDE 基础教程 1.下载安装 a 在火狐浏览其中搜索附件组件,查找 Selenium IDE b 下载安装,然后重启firefox 2.界面讲解 在菜单- ...
- [小北De编程手记] : Selenium For C# 教程目录
写<Selnium For C#>系列文章的初衷是因为有很多朋友问我应该从哪里开始学习自动化测试,于是就为大家写下了这个系列的文章,希望对你有些帮助吧.而我想表达的是Selenium(同时 ...
- 转:[小北De编程手记] : Selenium For C# 教程目录
写<Selnium For C#>系列文章的初衷是因为有很多朋友问我应该从哪里开始学习自动化测试,于是就为大家写下了这个系列的文章,希望对你有些帮助吧.而我想表达的是Selenium(同时 ...
- python 模块之 selenium 自动化使用教程
一.安装 pip install Selenium 二.初始化浏览器 Chrome 是初始化谷歌浏览器 Firefox 是初始化火狐浏览器 Edge 是初始化IE浏览器 PhantomJS 是一个无界 ...
- Selenium IDE安装及环境搭建教程
摘自https://blog.csdn.net/ywyxb/article/details/59103683 Selenium IDE环境部署- Firefox浏览器Firefox-ESR版本下载(推 ...
- selenium + firefox/chrome/phantomjs登陆之模拟点击
登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...
- selenium webdriver (python)的基本用法一
阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...
- Selenium常用API详解介绍
转至元数据结尾 由 黄从建创建, 最后修改于一月 21, 2019 转至元数据起始 一.selenium元素定位 1.selenium定位方法 2.定位方法的用法 二.控制浏览器操作 1.控制 ...
- selenium 自动化安装火狐谷歌插件
谷歌插件下载地址 https://npm.taobao.org/mirrors/chromedriver selenium下载地址 https://pypi.org/simple/selenium/ ...
随机推荐
- 5 Logistic回归(二)
5.2.4 训练算法:随机梯度上升 梯度上升算法:在每次更新回归系数时都需要遍历整个数据集,在数十亿样本上该算法复杂度太高. 改进方法:随机梯度上升算法:一次仅用一个样本点更新回归系数. 由于可以在新 ...
- classic asp中使用ADODB.Command防止sql injection
原始代码如下 Set Conn = Server.CreateObject("Adodb.Connection") Conn.Open "Provider=Microso ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...
- Oracle EBS-SQL (SYS-13):查询DBA在系统中的打Patch的信息.SQL
查询DBA在系统中的打补丁信息 1. select * from ad_patch_drivers /*查看已经打了哪些Patch*/ 2. select * from ad_pat ...
- ARM Cortex M3(V7-M架构)硬件启动程序 二
解析 STM32 的启动过程 解析STM32的启动过程 当前的嵌入式应用程序开发过程里,并且C语言成为了绝大部分场合的最佳选择.如此一来main函数似乎成为了理所当然的起点——因为C程序往往从main ...
- 深入理解Linux网络技术内幕——中断与网络驱动程序
接收到帧时通知驱动程序 在网络环境中.设备(网卡)接收到一个数据帧时,须要通知驱动程序进行处理. 有一下几种通知机制: 轮询: 内核不断检查设备是否有话要说.(比較耗资源,但在一些情况 ...
- OC 中的block使用
在iOS的开发过程中,使用块的地方很多也很方便,但是在使用块的过程中要注意内存泄露的问题. 在块创建的时候,会对块内的所有对象的引用计数加一,直到块销毁,所以在使用块的过程中需要我们进行处理,在这里以 ...
- AngularJS ng-class用法
mark from https://my.oschina.net/gejiawen0913/blog/188547 ng-class是AngularJS预设的一个指令,用于动态自定义dom元素的css ...
- Qt(QML)本地化
Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all Literial UI strings ...
- BZOJ 4000: [TJOI2015]棋盘( 状压dp + 矩阵快速幂 )
状压dp, 然后转移都是一样的, 矩阵乘法+快速幂就行啦. O(logN*2^(3m)) ------------------------------------------------------- ...