0
我需要拍攝網頁快照,並且爲此我使用了selenium RC(這是個不錯的選擇嗎?)和eclipse for java語言。我使用它作爲JUnit測試用例。這是我的代碼。在java中使用selenium服務器拍攝網頁快照
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
@SuppressWarnings("deprecation")
public class mainClassTest extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.gmail.com/");
//water= new DefaultSelenium("localhost", 4444, "*firefox", "http://www.gmail.com/");
selenium.start();
}
@Test
public void testFinalSelenium() throws Exception {
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
selenium.captureScreenshot("C:\\test\\simpleQuora.png");
selenium.captureEntirePageScreenshot("C:\\test\\CompleteQuora.png", "");
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}
它的工作正常,但如果我必須拍攝多個URL的快照,那麼有什麼辦法做到這一點? 我們可以做到這一點,而不用它作爲JUnit測試用例並在主函數中使用硒?
因爲如果我嘗試運行這段代碼:
package com.example.tests;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class MainClass {
void func(String url, String file)
{
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", url);
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
String file1= "C:\\test\\"+file+".png";
String file2= "C:\\test\\"+file+"2.png";
selenium.captureScreenshot(file1);
selenium.captureEntirePageScreenshot(file2, "");
selenium.stop();
}
public static void main(String[] args)
{
MainClass demo = new MainClass();
demo.func("www.facebook.com","face");
//demo.func("www.reddit.com","reddit");
}
}
我 this error(雖然我已經從CMD啓動服務器)。
更好更新的webdriver而是採用老技術 – 2012-07-16 11:50:15