簡體   English   中英

在Java中使用Selenium服務器獲取網頁快照

[英]Taking webpages snapshot using selenium server in java

我需要拍攝網頁快照,為此我正在使用帶有Java語言的eclipse的Selenium RC(這是不錯的選擇嗎?)。 我將其用作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測試用例並在主要功能中使用硒而做到這一點?

因為如果我嘗試運行以下代碼:

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"); 

    }
}

我收到此錯誤 。(盡管我已從cmd啟動服務器)。

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

更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM