设为首页 加入收藏

TOP

Selenium之偷懒教程(二)
2015-07-20 18:04:56 来源: 作者: 【 】 浏览:5
Tags:Selenium 偷懒 教程
) { 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);


?

等待网页加载完毕以后就是下面 的样子:

?

\

?

然后就是定位我所要选择和填写的控件:首先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.

?

\

?

?

然后按一下工具中的光标按钮,点击你想看的控件。

?

\

?

所以上面三个选择框都是通过id来定位的。

?

下面来定位时间输入框:

?

?

WebElement text = mDriver.findElement(By.id("field_CostTime"));
text.sendKeys(mHours + "");

这个很容易,以id来定位。时间的计算我是通过实时来计算的,以9点上班为准,计算当前时间与9点的差值。每个公司的规章制度不一,每个公司计算方式也不一样的。

?

?

\

?

然后就填写日报内容,我用的方式是把日报的内容写在一个txt文档中,然后读这个文档写。

?

\

?

但是定位日报输入框有点麻烦,因为它是存在于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);

\

?

?

填完上面的内容以后就要提交啦。但是提交按钮却不在刚才的iframe中。

?

\

?

所以还得先转换到当前上下文,然后再定位提交按钮。

?

?

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);

?

?

?

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇杭电 1108 最小公倍数 下一篇输入法InputConnection

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: