Selenium Examples
Today, one of my friends wanted to query his GCT(Graduate Candidate Test) scores,and he knew his ID number, but he forgot his Admission Form No., it is easy to find out the first 11 digits assigned to his school,
but he just knew the range of last four digits, so he asked me to help to figure it out.
The first thought is to write a program and use HttpURLConnection to submit form and analyze response,
but it failed for some reason, and don't want the friend to wait for a long time, so I gave up my
first idea, and resort to Selenium.
1: start selenium server:
Download Selenium RC.
cd selenium-remote-control-1.0.1-dist\selenium-server-1.0.1
java -jar selenium-server.jar (to run the server in interactive mode execute java -jar selenium-server.jar -interactive)
2: Then write the following code:
import com.thoughtworks.selenium.SeleneseTestCase;
public class Scores extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://**.edu.cn/", "*");
}
public void testScores() throws Exception {
String idNo = "*******************";
String admFormNoPrefix = "************";
int admFormNoUnknownPartStart = 1000, admFormNoUnknownPartEnd = 3000;
boolean notMatch = true;
selenium.setTimeout("300000");
for (int i = admFormNoUnknownPartStart; i < admFormNoUnknownPartEnd; i++) {
selenium.open("/graduate/**.htm");
selenium.type("zjhm", idNo);
selenium.type("ksbh", admFormNoPrefix + i);
selenium.click("B1");
selenium.waitForPageToLoad("10000");
notMatch = selenium.isTextPresent("Id No. and Admission Form No. don't match.");
if (!notMatch) {
System.out.println("Admission Form No.= " + admFormNoPrefix + i);
}
assertTrue(notMatch);
}
}
}
Then Run it as JUnit Test.
At last, problem resolved, and thanks, Selenium :)
Resources:
http://blog.taragana.com/index.php/archive/5-minute-guide-to-selenium-ide-and-selenium-rc-test-tools/
http://blog.taragana.com/index.php/archive/9-important-tips-for-selenium-remote-control-java-client-test-tool/