首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java中使用selenium服务器拍摄网页快照

在java中使用selenium服务器拍摄网页快照
EN

Stack Overflow用户
提问于 2012-07-16 19:10:33
回答 1查看 1.4K关注 0票数 0

我需要拍摄网页的快照,为此我使用了selenium RC (这是个不错的选择,对吧?)我将它用作JUnit测试用例。这是我的代码。

代码语言:javascript
复制
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测试用例和在主函数中使用selenium的情况下这样做吗?

因为如果我尝试运行这段代码:

代码语言:javascript
复制
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启动了服务器)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-16 19:20:55

代码语言:javascript
复制
    demo.func("www.facebook.com","face"); 

更改为

代码语言:javascript
复制
    demo.func("http://www.facebook.com","face"); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11503039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档