Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用JAVA和Cucumber进行测试的每个步骤后捕获屏幕截图?_Java_Selenium_Cucumber_Integration Testing - Fatal编程技术网

如何在使用JAVA和Cucumber进行测试的每个步骤后捕获屏幕截图?

如何在使用JAVA和Cucumber进行测试的每个步骤后捕获屏幕截图?,java,selenium,cucumber,integration-testing,Java,Selenium,Cucumber,Integration Testing,运行集成测试时,在每个步骤后捕获屏幕截图的最佳方法是什么 测试是使用Selenium(3.0.1)和Cucumber(1.2.4)用Java编写的 下面是测试后截图的代码,但我需要在每个方法后截图,并用@Given、@When、@Then注释 @After public void after(Scenario scenario){ final byte[] screenshot = driver.getScreenshotAs(OutputType.BYTES); scenari

运行集成测试时,在每个步骤后捕获屏幕截图的最佳方法是什么

测试是使用Selenium(3.0.1)和Cucumber(1.2.4)用Java编写的

下面是测试后截图的代码,但我需要在每个方法后截图,并用@Given、@When、@Then注释

@After
public void after(Scenario scenario){
    final byte[] screenshot = driver.getScreenshotAs(OutputType.BYTES);
    scenario.embed(screenshot, "image/png");
}

谢谢你的提示。

这篇文章能帮到你吗


Selenium公开了一个称为WebDriverEventListener的接口,您可以实现自己的代码。通常,此接口具有afterFindBy、BeforFindBy等方法,只需实现该方法即可进行屏幕截图

在实现这个方法之后,您需要将这个实现的类注入驱动程序对象,如下所示

EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(driver);
    MyWebDriverListerner handler = new MyWebDriverListerner();
    eventFiringWebDriver.register(handler);
现在,只要驱动程序找到元素,它就会调用相应的注入方法


让我知道它是否解决了您的问题

以下是您问题的答案:

  • 假设您的方法如下所示:

    @Given("^Open$")
    public void Open() throws Throwable 
    {
        //your code
    }
    
    @When("^I$")
    public void I(String uname, String pass) throws Throwable 
    {
        //your code
    }
    
    @Then("^User$")
    public void User() throws Throwable 
    {
        //your code
    }
    
    @Given("^Open$")
    public void Open() throws Throwable 
    {
        //your code
        Utility.screenshot(driver, System.currentTimeMillis());
    }
    
    @When("^I$")
    public void I(String uname, String pass) throws Throwable 
    {
        //your code
        Utility.screenshot(driver, System.currentTimeMillis());
    }
    
    @Then("^User$")
    public void User() throws Throwable 
    {
        //your code
        Utility.screenshot(driver, System.currentTimeMillis());
    }
    
  • 您可以编写一个库来截图,如:

    public static void screenshot(WebDriver driver, long ms)
    {
    
    try {
        TakesScreenshot ts = (TakesScreenshot) driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(source, new File("./ScreenShots/"+ms+"Facebook.png"));
        System.out.println("ScreenShot Taken");
    } 
    catch (Exception e) 
    {
        System.out.println("Exception while taking ScreenShot "+e.getMessage());
    }
    
    
    }
    
  • 现在,您可以在每个方法之后轻松调用库,以获取如下屏幕截图:

    @Given("^Open$")
    public void Open() throws Throwable 
    {
        //your code
    }
    
    @When("^I$")
    public void I(String uname, String pass) throws Throwable 
    {
        //your code
    }
    
    @Then("^User$")
    public void User() throws Throwable 
    {
        //your code
    }
    
    @Given("^Open$")
    public void Open() throws Throwable 
    {
        //your code
        Utility.screenshot(driver, System.currentTimeMillis());
    }
    
    @When("^I$")
    public void I(String uname, String pass) throws Throwable 
    {
        //your code
        Utility.screenshot(driver, System.currentTimeMillis());
    }
    
    @Then("^User$")
    public void User() throws Throwable 
    {
        //your code
        Utility.screenshot(driver, System.currentTimeMillis());
    }
    

  • 如果这能回答您的问题,请告诉我。

    在接受并合并以下合并请求之前,我认为这是不可能的。如果您真的感兴趣,您可以在本地合并它,并拥有自己的定制Jar


    这可能不是你所要求的,但这也可以帮助其他人!(不过我没用黄瓜)

    下面是我的代码,可以截屏并将其添加到PDF文件中(如果您想用它做其他事情)

    只要随时调用
    screenshotPDF(webDriver,testName)
    方法即可

    package com.helper;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.PDPage;
    import org.apache.pdfbox.pdmodel.PDPageContentStream;
    import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
    import org.apache.pdfbox.pdmodel.font.PDFont;
    import org.apache.pdfbox.pdmodel.font.PDType1Font;
    import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
    import org.openqa.selenium.Capabilities;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.remote.server.handler.WebDriverHandler;
    import org.testng.annotations.Test;
    
    public class ScreenshotPDF {
    
        @SuppressWarnings("deprecation")
        @Test
        //public static void screenshotPDF() {
        public static void screenshotPDF(WebDriver webDriver, String testName){
    
                    {
    
                PDDocument doc = null;
                boolean isNewFile = false;
    
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
                String timeStemp = sdf.format(date);
    
    
                try {
                    try {
                        doc = PDDocument.load(new File(
                                "C:/Users/Documents/sample.pdf"));
                    } catch (FileNotFoundException f) {
                        doc = new PDDocument();
                        PDPage p = new PDPage();
                        doc.addPage(p);
                        isNewFile = true;
                    }
    
                    File screenshot = ((TakesScreenshot) webDriver)
                            .getScreenshotAs(OutputType.FILE);
    
                    Integer numberP = doc.getNumberOfPages();
                    PDPage blankPage = new PDPage();
                    PDPage page;
                    if (!isNewFile) {
                        doc.addPage(blankPage);
                        page = doc.getPage(numberP);
                    } else { 
                        page = doc.getPage(numberP - 1);
                    }
                    PDImageXObject pdImage = PDImageXObject
                            .createFromFileByContent(screenshot, doc);
                    PDPageContentStream contentStream = new PDPageContentStream(
                            doc, page, AppendMode.APPEND, true);
                    PDFont font = PDType1Font.HELVETICA_BOLD;
                    contentStream.beginText();
                    contentStream.setFont(font, 12);
                    contentStream.moveTextPositionByAmount(100, 600);
                    contentStream.drawString(testName+"  "+timeStemp);
                    contentStream.endText();
    
                    float scale = 0.4f;
    
                    Capabilities cap = ((RemoteWebDriver) webDriver).getCapabilities();
                    String browserName = cap.getBrowserName().toLowerCase();
                    if (browserName.contains("explorer"))
                        scale = 0.4f;
    
                    contentStream.drawImage(pdImage, 50, 210, pdImage.getWidth() * scale, pdImage.getHeight() * scale);
                    contentStream.close();
                    contentStream.close();
                    doc.save("C:/Users/Documents/sample.pdf");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (doc != null) {
                        try {
                            doc.close();
                        } catch (IOException e) {
    
                            e.printStackTrace();
                        }
                    }
                }
            }
    
        }
    
    }
    

    java中没有afterStep注释。所以你不能直接做。你可以用@DebanjanB-answer中提到的另一种方式来做


    但这可以在cucumber ruby中通过步骤后注释实现

    使用方面解决了这个问题。这相当棘手,请注意注释:

    @After("call(public * cucumber.runtime.StepDefinitionMatch.runStep(..)) && within(cucumber.runtime.Runtime)")
    
    下面是Viviana Cattenazzi编写的完整代码

    pom.xml

     <dependencies>
             <dependency>
                 <groupId>org.aspectj</groupId>
                 <artifactId>aspectjweaver</artifactId>
                 <version>1.8.9</version>
             </dependency>
             <dependency>
                 <groupId>org.aspectj</groupId>
                 <artifactId>aspectjrt</artifactId>
                 <version>1.8.9</version>
             </dependency>
             <dependency>
                 <groupId>org.aspectj</groupId>
                 <artifactId>aspectjtools</artifactId>
                 <version>1.8.9</version>
             </dependency>
             <dependency>
                 <groupId>info.cukes</groupId>
                 <artifactId>cucumber-core</artifactId>
                 <version>1.2.4</version>
             </dependency>
         </dependencies>
    
    ......
    
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>aspectj-maven-plugin</artifactId>
                 <version>1.10</version>
                 <configuration>
                     <weaveDependencies>
                         <weaveDependency>
                             <groupId>info.cukes</groupId>
                             <artifactId>cucumber-core</artifactId>
                         </weaveDependency>
                     </weaveDependencies>
                     <showWeaveInfo>true</showWeaveInfo>
                     <source>1.8</source>
                     <target>1.8</target>
                     <complianceLevel>1.8</complianceLevel>
                 </configuration>
                 <executions>
                     <execution>
                         <phase>process-test-classes</phase>
                         <goals>
                             <goal>compile</goal>
                             <goal>test-compile</goal>
                         </goals>
                     </execution>
                 </executions>
             </plugin>
    

    此时,OP可能已经找到了替代方案。然而,这可能会帮助其他人。最新版本的io.cucumber同时具有@BeforeStep和@AfterStep,因此您可以在@AfterStep函数中的一个位置将截图嵌入其中,从而解决您的问题


    您真正使用的是什么。在你的测试中,你说了以下几点。硒量角器,爪哇,黄瓜,还有你提到的茉莉花。这意味着有两种语言(Java和JS)和两种框架(Cucumber和Jasmine)。如果您指的是CucumberJS和量角器,请更改您的问题,并提供您正在使用的CucumberJS和量角器的版本。然后我可以为您提供答案。@wswebcreation我已经更新了问题。谢谢你的评论,我很困惑。我还更新了标题和标签,因为它是Java我帮不了你,我更喜欢JS;-)。祝你好运it@wswebcreation感谢此hepls,但它只在测试后拍摄屏幕截图。OP需要在每个方法之后拍摄屏幕截图
    ,而不是像
    WebDriverEventListener
    的实现那样在afterFindBy(元素)和beforeFindBy(元素)之前拍摄屏幕截图。谢谢你的回答,但我有600多种方法需要截图。目前正在查看方面。@tetchen9很棒!!!恕我直言,在我没有其他选择的情况下,我认为在Java中调用库没有任何问题。毫无疑问,
    Aspects
    将是一个更好的选择,但在构建问题时,您没有提到您的基本要求。Thanks@GaurangShah你能考虑在注释中解释“代码>重复< /代码>吗?我一直相信
    是为了重用代码而编写的。谢谢,但您建议复制过去的
    Utility.screenshot(driver,System.currentTimeMillis())此代码包含已编写的所有步骤以及将要编写的所有新STPE。这是多余的。应该自动调用它。如前所述,该解决方案已在合并请求中可用。@GaurangShah我认为您缺少某些要点:1。我并不是说我的答案是最好的解决方案。你也可以给出答案。2.我们随时欢迎您从github中提取并创建/构建自己的exe/jar/解决方案,以帮助您自己。3.在这里,OP提出了一个问题,我没有找到一个稳定且行之有效的
    听众
    注释
    标签
    来解决OP的问题。因此,我简单地选择了提供
    的概念。4.请求您维护一个有益于全球社区的建设性环境,retrievePrivateField(..)和RetrieveJavaStepDefinition(..)方法的方法定义在哪里?可以发布RetrieveJavaStepDefinition和retrievePrivateField方法吗codes@tetchen9:我们应该将此代码放置在框架中的何处?