用Java的测试工具:Selenium + JUnit 可以打开网页,点击网页上的链接。 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.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait;public class SeleniumTest { protected WebDriver driver; @Before public void setUp() { driver = new SafariDriver(); } @After public void cleanUp() { // Close the browser driver.quit(); } @Test public void search() { // And now use this to visit Baidu driver.get("http://www.baidu.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.google.com"); // Find the text input element by its id WebElement element = driver.findElement(By.id("kw")); // Enter something to search for element.sendKeys("Cheese!"); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); // Now submit the form. WebDriver will find the form for us from the element // driver.findElement(By.id("su")).click(); element.submit(); // Baidu's search is rendered dynamically with JavaScript. // Wait for the page to load, timeout after 20 seconds (new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { return driver.getTitle().toLowerCase().startsWith("cheese!"); } }); // Should see: "Cheese!_百度搜索" System.out.println("Page title is: " + driver.getTitle()); } }
用Selenium可以实现楼主需求,就是一个测试软件
public class SeleniumTest { protected WebDriver driver;
@Before public void setUp() { System.setProperty("webdriver.ie.bin","C:\\Program Files\\Internet Explorer\\iexplore.exe"); try{ driver= new InternetExplorerDriver(); // driver = new FirefoxDriver(); }catch (Exception e) { e.printStackTrace(); } }
@After public void cleanUp() { // Close the browser driver.quit(); }
@Test public void search() { //DefaultSelenium selenium = new DefaultSelenium( "localhost" , 4444, "*firefox D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe","http://www.baidu.com"); driver.get("http://www.baidu.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.google.com");
// Find the text input element by its id WebElement element = driver.findElement(By.name("wd"));
// Enter something to search for element.sendKeys("Cheese!");
// Check the title of the page System.out.println("Page title is: " + driver.getTitle());
// Now submit the form. WebDriver will find the form for us from the element driver.findElement(By.id("su1")).click(); //element.submit();
// Baidu's search is rendered dynamically with JavaScript. // Wait for the page to load, timeout after 20 seconds (new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { return driver.getTitle().toLowerCase().startsWith("cheese!"); } });
// Should see: "Cheese!_百度搜索" System.out.println("Page title is: " + driver.getTitle()); } } 楼上的我试了下有问题!改了一下可以用
可以打开网页,点击网页上的链接。
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;public class SeleniumTest {
protected WebDriver driver; @Before
public void setUp() {
driver = new SafariDriver();
} @After
public void cleanUp() {
// Close the browser
driver.quit();
} @Test
public void search() {
// And now use this to visit Baidu
driver.get("http://www.baidu.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com"); // Find the text input element by its id
WebElement element = driver.findElement(By.id("kw")); // Enter something to search for
element.sendKeys("Cheese!"); // Check the title of the page
System.out.println("Page title is: " + driver.getTitle()); // Now submit the form. WebDriver will find the form for us from the element
// driver.findElement(By.id("su")).click();
element.submit(); // Baidu's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 20 seconds
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.getTitle().toLowerCase().startsWith("cheese!");
}
}); // Should see: "Cheese!_百度搜索"
System.out.println("Page title is: " + driver.getTitle());
}
}
protected WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.ie.bin","C:\\Program Files\\Internet Explorer\\iexplore.exe");
try{
driver= new InternetExplorerDriver();
// driver = new FirefoxDriver();
}catch (Exception e) {
e.printStackTrace();
}
}
@After
public void cleanUp() {
// Close the browser
driver.quit();
}
@Test
public void search() {
//DefaultSelenium selenium = new DefaultSelenium( "localhost" , 4444, "*firefox D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe","http://www.baidu.com");
driver.get("http://www.baidu.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its id
WebElement element = driver.findElement(By.name("wd"));
// Enter something to search for
element.sendKeys("Cheese!");
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Now submit the form. WebDriver will find the form for us from the element
driver.findElement(By.id("su1")).click();
//element.submit();
// Baidu's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 20 seconds
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "Cheese!_百度搜索"
System.out.println("Page title is: " + driver.getTitle());
}
} 楼上的我试了下有问题!改了一下可以用