83 lines
2.3 KiB
Java
83 lines
2.3 KiB
Java
import static org.junit.Assert.fail;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import org.jsoup.select.Elements;
|
|
import org.junit.After;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.openqa.selenium.By;
|
|
import org.openqa.selenium.WebDriver;
|
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
|
|
|
|
public class FetchPageUtil {
|
|
private static WebDriver driver;
|
|
private StringBuffer verificationErrors = new StringBuffer();
|
|
@Before
|
|
public void setUp() throws Exception {
|
|
driver = new FirefoxDriver();
|
|
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
|
|
}
|
|
|
|
|
|
/**
|
|
* 打印传入页面渲染后的源码
|
|
* @param url
|
|
* @throws InterruptedException
|
|
*/
|
|
public static void printPageSource(String url) throws InterruptedException{
|
|
driver.get(url);
|
|
Thread.sleep(3000);
|
|
System.out.println( driver.getPageSource());
|
|
driver.quit();
|
|
}
|
|
/**
|
|
* 打印页面源码和选择器匹配后的结果
|
|
* @param url
|
|
* @param selectStr
|
|
* @throws InterruptedException
|
|
*/
|
|
public static void printselects(String url ,String selectStr) throws InterruptedException{
|
|
driver.get(url);
|
|
Thread.sleep(3000);
|
|
Elements leles = JQueryUtils.select(selectStr, driver.getPageSource());
|
|
System.out.println( leles);
|
|
driver.quit();
|
|
}
|
|
/**
|
|
* 测试分页是否正常运行
|
|
* @param url
|
|
* @param nextStr
|
|
* @throws InterruptedException
|
|
*/
|
|
public static void TestNext(String url ,String nextStr) throws InterruptedException{
|
|
driver.get(url);
|
|
driver.findElement(By.linkText(nextStr)).click();
|
|
Thread.sleep(3000);
|
|
driver.findElement(By.linkText(nextStr)).click();
|
|
Thread.sleep(3000);
|
|
driver.findElement(By.linkText(nextStr)).click();
|
|
Thread.sleep(3000);
|
|
driver.findElement(By.linkText(nextStr)).click();
|
|
Thread.sleep(3000);
|
|
driver.findElement(By.linkText(nextStr)).click();
|
|
Thread.sleep(3000);
|
|
driver.quit();
|
|
}
|
|
@After
|
|
public void tearDown() throws Exception {
|
|
driver.quit();
|
|
String verificationErrorString = verificationErrors.toString();
|
|
if (!"".equals(verificationErrorString)) {
|
|
fail(verificationErrorString);
|
|
}
|
|
}
|
|
@Test
|
|
public void runMain() throws InterruptedException{
|
|
//printPageSource("http://bol.szhome.com/search.aspx");//第一步,查看分页的渲染后源码
|
|
printselects("http://bol.szhome.com/search.aspx",".imgdiv a");//根据源码写出选择器String
|
|
//TestNext("http://bol.szhome.com/search.aspx",">");//测试分页
|
|
}
|
|
}
|