如何在使用selenium/java钩子之前动态地获取正确的页面类?

zaq34kh6  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(374)

我有一个selenium/cucumber基础设施,用于测试至少3页(编辑、概述、发送)的网站。所有my page类都是从abstractpage扩展而来的,abstractpage只有一个linkedhashmap,其中包含所有WebElement和驱动程序。我还有一个页面管理器来设置CurrentPage和getpage。在每个场景之前,我必须启动网站并将页面设置为editpage。我想写一个方法,从我的beforehook步骤中动态获取editpage,因为每个站点都有另一个名称,并且位于另一个包中。
例如:我启动了“website1”的测试,在启动webdriver并准备好url后,我现在需要将“Website1Edit.java”作为abstractpage来设置当前页面。
abstractpage.java:

public abstract class AbstractPage {
public Map<String, AbstractWebElement> webElementMap = new LinkedHashMap<>();

public Map<String, AbstractWebElement> getListOfWebElementType(WebElementType type) {
    Map<String, AbstractWebElement> WebElementMapOfType = new HashMap<>();
    webElementMap.forEach((description, value) -> {
        if(value.getWebElementType() == type)
            WebElementMapOfType.put(description,value);
    });
    return WebElementMapOfType;
}

    public AbstractPage getPage(){
        return this;
    }
}

pagemanager.java

@Component
@Scope(SCOPE_CUCUMBER_GLUE)
@RequiredArgsConstructor
public class PageManager {
   private AbstractPage currentPage;
   private String destination;

   public AbstractPage getPage()  {
       return currentPage;
   }

   public void setCurrentPage(AbstractPage page) {
       currentPage = page;
   }
}

website1editpage.java

@Component
@Scope(SCOPE_CUCUMBER_GLUE)
public class WebSite1EditPage extends AbstractPage {
   public WebSite1EditPage(WebDriver driver) {
      ...All WebElement Objects from webElementMap (webElementMap.put(...))
   }

website1editpagesteps.java

@Slf4j
@RequiredArgsConstructor
public class WebSite1EditPageSteps  {
private final PageManager pageManager;
private final WebSite1EditPage webSite1EditPage;

@Given("Launch the WebSite1")
public void launchTheWebSite1() {
    pageManager.setCurrentPage(webSite1EditPage); //This is the step i want to write in Before-Hook, because i have to do this for every another website, i allways need the EditPage to Start my tests
}

hooks.java

@Before
   public void beforeScenario(Scenario scenario){
       String feature = getFeatureName(scenario.getId());
       endPointManager.setFormToken(feature);
       log.info("This Step open the Browser and launch the " + feature + " Website."); //Feature is the WebSite Name in my case
       String url = connectionProperties.getWebSite().getUrlAsString() + 
       endPointManager.getEndPoint();
       webDriver.get(url);
       assertThat(webDriver.getCurrentUrl()).isEqualTo(url);

       String pageName = getFeatureName(scenario.getId()) + "EditPage"; // getFeatureName(scenario.getId() gives me my WebSite Name in this case here "WebSite1"
       pageManager.getPage(pageName); //Something like this is my idea. But its not working because, String cannot be Converted to AbstractPage. 
}
9lowa7mx

9lowa7mx1#

带有@before-hook的方法可能会在带有@given注解的方法之前运行,因此我认为应该在@before-hook中调用setpage方法,然后在名为launchwebsite1()的方法中调用页面
类似的方法可能会奏效:

@Before
   public void beforeScenario(Scenario scenario){
       String feature = getFeatureName(scenario.getId());
       endPointManager.setFormToken(feature);
       log.info("This Step open the Browser and launch the " + feature + " Website."); //Feature is the WebSite Name in my case
       String url = connectionProperties.getWebSite().getUrlAsString() + 
       endPointManager.getEndPoint();
       webDriver.get(url);
       assertThat(webDriver.getCurrentUrl()).isEqualTo(url);

       String pageName = getFeatureName(scenario.getId()) + "EditPage"; //This may be unnecessary
       WebSite1EditPage webSite1EditPage = new WebSite1EditPage(webDriver);
       pageManager.setCurrentPage(webSite1EditPage);

在@given方法中,您可以调用如下页面:

@Given("Launch the WebSite1")
public void launchTheWebSite1() {
    WebSite1EditPage webSite1EditPage = pageManager.getCurrentPage();
}

我不确定这是否有效,但我希望它能有所帮助。
顺致敬意,

相关问题