【环境准备】
解压缩完Eclipse,无法正常使用,提示:JVM版本过低,得1.7以上才行。好吧,之前什么时候我自己装了个1.6版本的JRE导致的。
那就重新装个最新的JDK。MAC OS装了新版JDK之后,可以用/usr/libexec/java_home命令行查看javahome路径,我的如下:
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
JDK和JRE有什么区别?
- JDK是Java Development Kit,开发工具集;
- JRE是Java Runtime Environment,运行时环境;
- JDK包含JRE,JRE包含JVM;
- 与JDK相比,JRE不包含开发工具——编译器、调试器和其它工具;
- JVM,Java Virtual Mechinal(JAVA虚拟机)。JVM是JRE的一部分,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的。JVM有自己完善的硬件架构,如处理器、堆栈、寄存器等,还具有相应的指令系统。JVM 的主要工作是解释自己的指令集(即字节码)并映射到本地的 CPU 的指令集或 OS 的系统调用。Java语言是跨平台运行的,其实就是不同的操作系统,使用不同的JVM映射规则,让其与操作系统无关,完成了跨平台性。
有一篇博文讲的比较清楚:http://java-mzd.iteye.com/blog/838514
接下来去官网下载需要用到的jar包
selenium-server-standalone-2.46.0.jar,这个jar是关于selenium server的
http://goo.gl/cvntq5(为啥链接长这样,我也好奇~官网上这么贴的)
selenium-java-2.46.0.jar,这个jar是用来使用特定语言跟selenium server或者selenium WebDriver进行交互用的,我们用的是java,所以下载这个
http://selenium-release.storage.googleapis.com/2.46/selenium-java-2.46.0.zip
【代码】
上述两个jar,在我们工程建好后,通过Build Path -> Configure Build Path -> Libraries -> Add External Jars加入到我们的工程中来
简要的说下过程:
- 在登录页面登录,在论坛首页找到『签到有礼』子版本的href,打开这个子版块;
- 找到今天的签到帖子链接,打开这个帖子(签到有礼7.03每天签到得铜钱,每人限回一次);
- 在帖子内容里面用正则匹配找到它指定的需要回复的内容(如下图,今天要求回复『挖财 我的资产管家』);
- 通过xpath找到回复的textarea元素,sendKeys(指定的回复内容);
- 通过xpath找到fastpostsubmit按钮,click()提交回复。
<pre name="code" class="java">package autoLoginReply;
import java.util.concurrent.TimeUnit;
import java.util.Calendar;
import java.util.regex.*;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AutoLoginReply {
private WebDriver driver;
private String loginUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
loginUrl = "https://www.wacai.com/user/user.action?url=http%3A%2F%2Fbbs.wacai.com%2Fportal.php";
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test
public void testLogin() throws Exception {
//open the login page
driver.get(loginUrl);
driver.findElement(By.id("account")).clear();
driver.findElement(By.id("account")).sendKeys("***");
driver.findElement(By.id("pwd")).clear();
driver.findElement(By.id("pwd")).sendKeys("***");
driver.findElement(By.id("login-btn")).click();
//get the bbs portal page, find the specific forum href
String a = driver.findElement(By.linkText("签到有礼")).getAttribute("href");
driver.navigate().to(a);//get(a);
//Thread title
Calendar c = Calendar.getInstance();
int m = c.get(Calendar.MONTH)+1;
int d = c.get(Calendar.DAY_OF_MONTH);
String dd = ""+d;
if (d < 10)
dd = "0"+dd;
String threadTitle = "签到有礼"+m+"."+dd+"每天签到得铜钱";
//System.out.println(threadTitle);
//find today's check-in thread
a = driver.findElement(By.partialLinkText(threadTitle)).getAttribute("href");
//System.out.println(a);
driver.navigate().to(a);
//using regrex to find the content we use to reply
Pattern p = Pattern.compile("回帖内容必须为.+</font>非此内容将收回铜钱奖励");
Matcher r = p.matcher(driver.getPageSource().toString());
StringBuffer key = new StringBuffer();
while(r.find()){
key.append(r.group());
}
//using xpath locate the textarea and submit button
driver.findElement(By.xpath("//textarea[@id='fastpostmessage']")).sendKeys(key.substring(key.indexOf(">")+1, key.indexOf("</")-1));
//driver.manage().window().maximize();
//System.out.println(driver.getPageSource());
driver.findElement(By.xpath("//button[@id='fastpostsubmit']")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
【小知识点Get】
- driver.navigate().to(url):跳转到指定的url,只执行跳转动作,不判断、不等待指定的页面是否加载成功;
- driver.get(url):跳转到指定的url,并且检查页面是否加载完毕,如果指定了timeout,而在指定时间内没有加载完毕则会抛出org.openqa.selenium.TimeoutException。