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

标签 java selenium selenium-rc

我需要拍摄网页快照,为此我正在使用 selenium RC(这是一个不错的选择,对吗?)和 eclipse for java 语言。我将其用作 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启动了服务器)。

最佳答案

    demo.func("www.facebook.com","face"); 

更改为

    demo.func("http://www.facebook.com","face"); 

关于java - 在java中使用selenium服务器拍摄网页快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503039/

相关文章:

c# - Selenium-RC 无法处理确认框

c# - 使用 Selenium 拦截 Javascript 函数调用

java - 如何解密HSQL数据库中的密码字段

c# - Selenium : xpath following-sibling where siblings have more children

java - java中如何获取特定的日期类型

java - 带谓词的 Selenium 过滤器

python - USB : usb_device_handle_win. cc:1020 在 Windows10 上使用 Selenium 的 ChromeDriver v87/Chrome v87 无法从节点连接错误中读取描述符

java - 如何使用 Selenium 在下拉列表中选择值?

java - Spring Security - 访问被拒绝(用户不是匿名的)spring-security-core-4.0.3.RELEASE

java - 如何同时随机生成两个数字而不使它们重复?