设为首页 加入收藏

TOP

Selenium之偷懒教程(一)
2015-07-20 18:04:56 来源: 作者: 【 】 浏览:4
Tags: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(
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇杭电 1108 最小公倍数 下一篇输入法InputConnection

评论

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