如果使用Serenity发生故障,如何继续执行其他步骤?

问题描述

我为我的英语不好对不起。我用谷歌翻译来发布这个问题。我还在互联网上搜索了许多无法解决我问题的问题。

我的问题如下:
我有一个Excel文件,允许手册导入数据。我使用此excel文件中的每一行来创建新数据(例如,看起来像一个帐户)。这并不困难。 由于每一行对应于不同的数据以创建新数据,因此请确保使用循环将数据获取到文件内部。例如如下:

|姓名|电邮|电话| .... |结果|
| A | abc @ email | 123456 | .... | accA |
| B | abc @ email | 123456 | .... | accB |

结果列是根据其他列的数据生成的。
我没有使用方案模板,因为有很多字段。并使用excel使手册更易于编辑和阅读。
因此,我使用循环重复此步骤可以挖掘excel文件中数据的行数。

我的代码:

public class TestClass {

    @Before
    public void set_the_stage() {
        OnStage.setTheStage(new OnlineCast());
    }

    @Given("{word} access Google")
    public void accessGoogle(String actor) {
        theActorCalled(actor).attemptsTo(Open.url("https://google.com"));
    }

    @When("Input {word}")
    public void inputSearch(String value) {
        Target input_search = Target.the("Input Search").locatedBy("//input[@title='Tìm kiếm']");
        Target click_search = Target.the("Click Search").locatedBy("//*[@class='FPdoLc tfB0Bf']//*[ @value='Tìm với Google'] ");

        theActorInTheSpotlight().attemptsTo(WaitUntil.the(input_search,WebElementStateMatchers.isVisible()));
        theActorInTheSpotlight().attemptsTo(Enter.theValue(value).into(input_search));
        // Failure step
        theActorInTheSpotlight().attemptsTo(WaitUntil.the(click_search,WebElementStateMatchers.isVisible()),Click.on(click_search));
    }

    @Then("Search successfully")
    public void searchSucess() {
        Target stackOverflow = Target.the("Stack Overflow").locatedBy("//*[contains(text(),'Stack Overflow')]");
        theActorInTheSpotlight().attemptsTo(Ensure.that(stackOverflow).isDisplayed());
    }

    @When("Read excel")
    public void readExcel() {
        int number = 5; // the number of records in the excel file

        for (int i = 0; i < number; i++) {
            try {
                System.out.println("Start Try: " + i);
                accessGoogle("Minato");
                inputSearch("stackoverflow.com");
                searchSucess();
                System.out.println("End Try: " + i);
            } catch (Exception e) {
                System.out.println("Catch: " + i);
                ThucydidesWebDriverSupport.getDriver().quit();
                ThucydidesWebDriverSupport.initialize();
                set_the_stage();
                e.getStackTrace();
            }
        }
    }
}

功能文件

@feature=searchGoogle
Feature: A description

  @testSearch
  Scenario: A scenario
    When Read excel

在自动化中,每次发生故障时,都会引发异常原因,并终止程序。 对于原始硒,要继续运行下面的代码,请使用try catch。
但是,当使用 Serenity 时,发生故障时,它会跳转以捕获并继续循环,但是跳过了以下步骤。我不了解这个框架如何运作。我试过再次调用 ThucydidesWebDriverSupport.initialize(); ;但是它仍然不执行这些步骤。 (它不会打开浏览器)。

有人可以帮助我吗?我希望当任何代码行出现故障时,它必须从第一步重新运行直到循环结束。不是跳过所有步骤。
谢谢大家!
结果:

Start Try: 0

Catch: 0
Start Try: 1
End Try: 1
Start Try: 2
End Try: 2
Start Try: 3
End Try: 3
Start Try: 4
End Try: 4
18:07:20.542 [main] ERROR n.t.c.steps.ConsoleLoggingListener 

图片报告:

Report

修改
2天后,我仍然找不到初始化新驱动程序的方法。但是我有办法解决我的问题。
结合了方案大纲,如下所示:

功能

Scenario Outline: A scenario
    When Read excel "test.xlsx" by <index>
    Examples:
      | index |
      | 1     |
      | 2     |
      | 3     |
      |...    |

修改代码:

@When("Read excel {string} by {word}")
    public void readExcel(String fileName,String index) {
        Iterator<Row> iterator = getSheetExcel(fileName);
        List<Row> listRow = new ArrayList<>();
        int colName = 0;
        int colEmail = 1;
        // ....
        while (iterator.hasNext()) {
            listRow.add(iterator.next());
        }
        // example
        String name = listRow.get(Integer.parseInt(index)).getCell(colName).getStringCellValue();
        String email = listRow.get(Integer.parseInt(index)).getCell(colEmail).getStringCellValue();

        accessGoogle(name);
        inputSearch(email);
        inputSearch("stackoverflow.com");
        searchSucess();
    }

    public Iterator<Row> getSheetExcel(String fileName) {
        String path = System.getProperty("user.dir") + "/src/test/resources/data/excel/" + fileName;
        File file = new File(path);   //creating a new file instance
        FileInputStream fis;          //obtaining bytes from the file
        XSSFWorkbook wb;              //creating Workbook instance that refers to .xlsx file
        XSSFSheet sheet = null;

        try {
            fis = new FileInputStream(file);
            wb = new XSSFWorkbook(fis);
            sheet = wb.getSheetAt(0);   //creating a Sheet object to retrieve object
        } catch (IOException e) {
            e.printStackTrace();
        }

        assert sheet != null;
        return sheet.iterator();
    }

结果:

enter image description here

解决方法

从表中读取数据通常是参数化测试的用例。对于黄瓜,您需要将数据表放入功能文件https://webkul.com/blog/parametrization-in-cucumber/

Scenario Outline: Test user Login with different credentials
    Given Open Firefox and navigate to Login page
    When valid "<username>" and "<password>" is entered
    Then User should be logged in successfully
 
Examples:
    | username   | password |
    | rahul@gmail.com | Test@123 |
    | xyz@gmail.com| Testtest |

请参阅Cucumber : How to read examples from external excel file for Scenarios Outline,如何将数据从excel中获取到功能文件中。

在我看来,您对所使用的框架不是很有经验,您真的需要它们吗?也许我可以建议从一个简单的JUnit参数化测试开始?示例:https://www.lambdatest.com/blog/junit-parameterized-test-selenium/

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...