Selenium之偷懒教程

发表于:2016-2-15 13:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:lcchuguo    来源:51Testing软件测试网采编

  进来一直停留在基础理论知识的学习中,认为太乏味,就写了一个网页自己主动化的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);
  等待网页载入完成以后就是以下 的样子:
  然后就是定位我所要选择和填写的控件:首先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");
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号