进来一直停留在基础理论知识的学习中,认为太乏味,就写了一个网页自己主动化的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.jarmailapi.jarsmtp.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之偷懒教程的更多相关文章

  1. Selenium IDE 基础教程

    Selenium IDE 基础教程 1.下载安装     a 在火狐浏览其中搜索附件组件,查找 Selenium IDE     b 下载安装,然后重启firefox 2.界面讲解      在菜单- ...

  2. [小北De编程手记] : Selenium For C# 教程目录

    写<Selnium For C#>系列文章的初衷是因为有很多朋友问我应该从哪里开始学习自动化测试,于是就为大家写下了这个系列的文章,希望对你有些帮助吧.而我想表达的是Selenium(同时 ...

  3. 转:[小北De编程手记] : Selenium For C# 教程目录

    写<Selnium For C#>系列文章的初衷是因为有很多朋友问我应该从哪里开始学习自动化测试,于是就为大家写下了这个系列的文章,希望对你有些帮助吧.而我想表达的是Selenium(同时 ...

  4. python 模块之 selenium 自动化使用教程

    一.安装 pip install Selenium 二.初始化浏览器 Chrome 是初始化谷歌浏览器 Firefox 是初始化火狐浏览器 Edge 是初始化IE浏览器 PhantomJS 是一个无界 ...

  5. Selenium IDE安装及环境搭建教程

    摘自https://blog.csdn.net/ywyxb/article/details/59103683 Selenium IDE环境部署- Firefox浏览器Firefox-ESR版本下载(推 ...

  6. selenium + firefox/chrome/phantomjs登陆之模拟点击

    登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...

  7. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

  8. Selenium常用API详解介绍

    转至元数据结尾   由 黄从建创建, 最后修改于一月 21, 2019 转至元数据起始   一.selenium元素定位 1.selenium定位方法 2.定位方法的用法 二.控制浏览器操作 1.控制 ...

  9. selenium 自动化安装火狐谷歌插件

    谷歌插件下载地址 https://npm.taobao.org/mirrors/chromedriver selenium下载地址 https://pypi.org/simple/selenium/ ...

随机推荐

  1. hdu 4059 The Boss on Mars 容斥

    题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...

  2. [摘]ASP.Net标准控件(TextBox控件)

    TextBox控件 TextBox控件又称文本框控件,为用户提供输入文本的功能. 1.属性 TextBox控件的常用属性及说明如表1所示. 表1 TextBox控件常用属性及说明 属    性 说   ...

  3. Drools

    http://www.infoq.com/news/2009/06/drools-5.0-release http://www.javacodegeeks.com/2014/03/event-proc ...

  4. 在VirtualBox虚拟机上采集Fedora15系统

    在VirtualBox虚拟机上采集Fedora15系统 第一部分:创建系统并磁盘分区 1.点击VirtualBox上的新建 2.添加名称,选择类型和版本,点下一步 3.写入内存(不要超过物理内存),点 ...

  5. sql优化-总结

    1.尽量缩小数据范围. 2.能一个sql解决的,坚决不用两条sql.利用case when或decode. select month_id, corppkno, sum(exportSum_new) ...

  6. 三点顺序_nyoj_68(计算几何).java

    三点顺序 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 3   描述 现在给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,现在让你判断A,B,C是顺时针给出的 ...

  7. 无废话ubuntu 13.4w文件共享配置

    目标:实现windows和linux混合组成的操作 系统中可以共享文件,并可以通过机器名互相访问 安装文件共享服务 0.更改本机主机名,修改 /etc/hostname文件和/etc/hosts文件中 ...

  8. 编程之美之数独求解器的C++实现方法

    编程之美的第一章的第15节.讲的是构造数独.一開始拿到这个问题的确没有思路, 只是看了书中的介绍之后, 发现原来这个的求解思路和N皇后问题是一致的. 可是不知道为啥,反正一開始确实没有想到这个回溯法. ...

  9. mlpack:可伸缩C++机器学习库(转)

    摘要:mlpack是一个可伸缩C++机器学习库,它的目的是让新用户通过简单.一致的API使用机器学习,同时为专业用户提供C++的高性能和最大灵活性. mlpack是一个直观.快速.可伸缩的C++机器学 ...

  10. IO库 8.2

    题目:编写一个测试函数,将cin作为参数传入. #include <iostream> using std::istream; istream& func(istream& ...