【发布时间】:2020-09-12 11:26:49
【问题描述】:
我为我糟糕的英语道歉。我使用谷歌翻译来发布这个问题。我也在网上搜索了很多我的问题都没有解决。
我的问题如下:
我有一个允许手册导入数据的 excel 文件。这个excel文件中的每一行我用来创建一个新数据(例如它看起来像一个帐户)。这并不难。
由于每一行对应不同的数据来创建新数据,所以一定要使用循环来获取文件内部的数据。例如如下:
|姓名 |电子邮件 |电话 | .... |结果|
|一个 | abc@电子邮件 | 123456 | .... | ACCA |
|乙| abc@电子邮件 | 123456 | .... |记账本|
结果列是根据其他列的数据生成的。
我不使用场景模板,因为有很多字段。并使用 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
在自动化中,每次出现故障时,它都会抛出异常,为什么会发生故障,并停止程序。
使用原始 selenium,为了继续运行下面的代码,我使用 try catch。
但是在使用Serenity的时候,当有失败的时候,会跳转到catch,继续循环,但是后面的步骤跳过了。我还不明白这个框架是如何工作的。我尝试再次调用 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
编辑
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();
}
【问题讨论】:
标签: java selenium serenity-bdd cucumber-serenity