我最近切换到 Google Guice,现在我的结果被每个测试覆盖,而不是添加所有测试。我正在使用哈希图来收集每个测试的通过和失败,但不是为每个测试收集数据,而是覆盖前一个测试。我在我的构造函数中使用我的注入。使用 Selenium、Cucumber、Java 和 Eclipse。
抽象驱动程序
public abstract class AbstractDriver {
protected abstract WebDriver createDriver();
public abstract WebDriver getDriver(); }
司机
public class Driver extends AbstractDriver {
protected WebDriver driver;
private ChromeDriverService ChromeService;
protected Messages msg = new Messages();
@Override
protected WebDriver createDriver() {
try {
System.setProperty(CHROME.getEXE(), CHROME.getPath());
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
ChromeService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(CHROME.getPath()))
.usingPort(CHROME.getNodes())
.build();
}catch(IllegalStateException e) {
msg.switchMessages("DriverErrorMessage");
}
return driver = new ChromeDriver(ChromeService,CHROME.getCHROME_DC());
}
@Override
public WebDriver getDriver() {
if (driver == null) {
this.createDriver();
}
return driver;
}
}
驱动程序基地
@ScenarioScoped
public class DriverBase {
public WebDriver driver = new Driver().getDriver();
}
页面库
public class PageBase {
protected DriverBase web;
public PageBase(DriverBase web) {
this.web = web;
PageFactory.initElements(web.driver, this);
}
页面对象
public class PageObject extends PageBase {
/** NOTES -> Constructor*/
@Inject
public PageObject(DriverBase web) {
super(web);
}
public String getTitle() {
web.driver.get("https://stackoverflow.com/");
String title = web.driver.getTitle();
return title;
}
public String getGoogleTitle() {
web.driver.get("https://google.com/");
String title = web.driver.getTitle();
return title;
}
}
脚步
public class Steps {
private DriverBase web;
PageObject pageobj;
Results result;
String results = null;
@Inject
Steps( DriverBase web,PageObject pageobj){
this.web = web;
this.pageobj= pageobj;
}
@Then("Check title")
public void test() throws IOException, ParseException {
String title = pageobj.getTitle();
if(title.equals("stackoverflow"))
{
results = "pass";
}else if(!title.equals("stackoverflow")) {
results = "fail";
}
result.createResult(results);
}
@Then("Check Google title")
public void test2() throws IOException, ParseException {
String title = pageobj.getGoogleTitle();
if(title.equals("google"))
{
results = "pass";
}else if(!title.equals("google")) {
results = "fail";
}
result.createResult(results);
}
结果
public class Results {
ArrayList<Map<String, String>> PlanResult = new ArrayList<>();
public void createResult(String result ) throws IOException,ParseException {
Map<String, String> CaseResult= new HashMap<String, String>(){{put("result", result) ;}};;
PlanResult.add(CaseResult);
System.out.println("Case Result: " + PlanResult);
}
}
收集通过或失败并将它们存储在数组中的方法。hashmap 字段是PlanResult。
ArrayList> PlanResult = new ArrayList<>();
也试过@Singleton