我需要拍摄网页快照,因为我正在使用selenium RC(这是一个不错的选择吗?)和eclipse for java language。我将它用作JUnit测试用例。这是我的代码。
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测试用例并在main函数中使用selenium吗?
因为如果我尝试运行此代码:
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启动了服务器)。
答案 0 :(得分:1)
demo.func("www.facebook.com","face");
更改为
demo.func("http://www.facebook.com","face");