拍摄多个浏览器快照

时间:2015-08-14 06:06:21

标签: selenium testing

我是一名ASP.NET Web开发人员,需要向测试人员提交已开发页面的工件。

这些包括:

  1. 在不同浏览器中打开时的页面快照。
  2. 同样的事情,使用不同版本的浏览器。
  3. 我手动(逐个)以及来自不同系统。(因为IE8仅在特定系统上可用......所以......)。是的,当然是耗时的。

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

    我在看Selenium网格。但这需要一些编码并使用eclipse等。不确定是否也可以截取屏幕截图并且我不了解硒。

    感谢您的任何帮助。谢谢

1 个答案:

答案 0 :(得分:1)

为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/

为节点

创建配置文件

文件名应为nodeConfig.json,并将其保存在启动节点的同一台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等......

使用配置文件和WebDrivers将您的PC作为节点连接到Selenium Hub

使用以下名称创建一个新文件: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网址并创建相关屏幕截图,并将其保存到c:/screenshots/screenshot.png

希望它有所帮助。

相关问题