获取多个浏览器快照



我是一个ASP。. NET web开发人员和需要提交开发页面的工件,给测试人员。

这些包括:

  1. 页面在不同浏览器中打开时的快照。
  2. 同样的事情,不同版本的浏览器。

我手动执行(一个接一个),也从不同的系统执行。(因为IE8只能在特定的系统上使用…. .)。是的,当然很耗时。

有没有一种方法可以简化我的生活。(比如browserstack.com)

我正在看硒网格。但这也需要一些编码和使用eclipse等。我还不确定是否可以截图,我对硒一无所知。

感谢任何帮助。谢谢你

为Selenium Hub创建一个配置文件

文件名应该为例如hubConfig.json,并将其保存在启动Hub的文件夹中。host表示HUB的IP, port表示端口号…:)

{
  "host": "127.0.0.1",
  "port": 4444
}

运行Selenium Hub,并加载配置文件

创建一个名称如下的新文件:startSeleniumHub.cmd,并在其中添加以下命令:

java -jar selenium-server-standalone.jar -role hub -hubConfig hubconfig.json

您可以在Selenium HQ网站下载最新版本的Selenium Server Standalone:

http://www.seleniumhq.org/download/

为Node创建一个配置文件

文件名应该是例如nodeConfig.json,并将其保存在启动Node的同一PC上的同一文件夹中。

{
    "capabilities":[
        {
            "platform":"WINDOWS",
            "browserName":"firefox",
            "firefox_binary":"C:\Program Files (x86)\Mozilla Firefox\firefox.exe",
            "maxInstances":5,
            "seleniumProtocol":"WebDriver"
        },
        {
            "platform":"WINDOWS",
            "browserName":"chrome",
            "maxInstances":5,
            "seleniumProtocol":"WebDriver"
        },
        {
            "platform":"WINDOWS",
            "browserName":"internet explorer",
            "maxInstances":5,
            "version":11,
            "seleniumProtocol":"WebDriver"
        }
    ],
    "configuration":{
        "port":5555,
        "host":"IP_OF_THE_NODE_PC",
        "register":true,
        "hubHost":"IP_OF_THE_HUB",
        "hubPort": 4444,
        "maxSession":1
    }
}

如果你在这个配置中有什么不理解的地方,你可以在下面的页面上阅读更多关于它的细节:

https://code.google.com/p/selenium/wiki/Grid2

如果你愿意,你可以添加更多的浏览器,如Edge, Safari, Opera等…

将您的pc连接到Selenium Hub作为一个带有配置文件和WebDrivers的节点

创建一个名为startSeleniumNode.cmd的新文件,并在其中添加以下命令:

java -jar "selenium-server-standalone-X.XX.X.jar" -role node -nodeConfig "nodeConfig.json" -Dwebdriver.chrome.driver="chromedriver.exe" -Dwebdriver.ie.driver="IEDriverServer.exe"

您可以在下载Selenium Server Standalone的同一页面上下载最新的Chrome和IE WebDriver:

http://www.seleniumhq.org/download/

写一些JAVA代码:)

这只是一个例子,我相信有更好的解决办法。例如,你可以使用TestNG来创建不同的测试,为测试添加参数,或者并行运行测试等等。您可以在TestNG网站上找到更多信息:http://testng.org/doc/index.html

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class ScreenshotMakerTest
{
    public static void main(String [] args) throws IOException
    {
        // Init a new DesiredCapabilites which will setup the WebDriver to open a specific browser.
        DesiredCapabilities dc = new DesiredCapabilities();
        // Set the browser. If you want to open a Chrome, you can modify it to: DesiredCapabilites.chrome(); etc...
        dc = DesiredCapabilities.internetExplorer();
        // Set the Platform. It must be the same what you defined in the nodeConfig.json.
        dc.setPlatform(Platform.WINDOWS);
        // Set the version of the browser. If you didn't set any version in the nodeConfig.json, you can skip this line.
        dc.setVersion("11");
        // Create the WebDriver which will open the given browser on a Node
        WebDriver driver = new RemoteWebDriver(new URL("IP_OF_THE_HUB:4444/wd/hub"), dc);
        // Open a page
        driver.get("http://google.com");
        // Create screenshot about the current page
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // Save the screenshot into a file
        FileUtils.copyFile(scrFile, new File("c:\screenshots\screenshot.png"));
        // Close the browser
        driver.quit();
    }
}

在本例中,您的代码将在Windows节点上打开Internet Explorer 11,打开http://google.com url并创建关于它的屏幕截图,并将其保存到c:/screenshots/screenshot.png

希望能有所帮助。

最新更新