I apologize for my bad English. I use google translate to post this question. I also searched many of my problems on the internet that did not resolve mine.
My problem is as follows:
I have an excel file that allows the manual to import data. Each row in this excel file I use to create a new data (eg it looks like an account). It is not difficult.
Since each row corresponds to a different data to create a new data, be sure to use a loop to get the data inside the file. For example as follows:
| Name | Email | Tel | .... | Result |
| A | abc@email | 123456 | .... | accA |
| B | abc@email | 123456 | .... | accB |
Result column is generated based on the data of the other columns.
I don't use the Scenario Template because there are many fields. And using excel to make the manual easier to edit and read.
So I use the loop to repeat the step can mine the number of rows of data from excel files.
My code:
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 file
@feature=searchGoogle
Feature: A description
@testSearch
Scenario: A scenario
When Read excel
In automation, every time there is a failure it throws an exception why the failure happens, and it stops the program.
With original selenium, to continue running the code below I use try catch.
But when using Serenity, when there is a failure, it jumps to catch and continues the loop, but the following steps are skipped. I don't understand how this framework works yet. I have tried calling ThucydidesWebDriverSupport.initialize(); again; But it still doesn't execute the steps. (It doesn't open the browser).
Can anyone help me? I want when it has a failure on any line of code, it must keep running again from the first step until the loop ends. It is not that it skips all steps.
Thanks all!!!
Result:
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
Edit
After 2 days I still could not find a way to initialize a new driver. But I have a way to fix my problem.
That combines Scenario Outline, like this:
Feature
Scenario Outline: A scenario
When Read excel "test.xlsx" by <index>
Examples:
| index |
| 1 |
| 2 |
| 3 |
|... |
Edit code:
@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();
}