gfk-ba / senbot

Cucumber / Selenium framework in Java
MIT License
10 stars 11 forks source link

Cannot add ElementService as a Resource to abstract class #19

Closed dadamschi closed 10 years ago

dadamschi commented 10 years ago

I am trying to expand on the Demo for a proof of concept.

My ideal situation was to put a reference from to the ElementService in an abstract class and then use it in children classes. However, when instantiating the child class the ElementService I am trying to access is null.

My abstract class looks like this: public abstract class AbstractCappexPage extends BaseServiceHub { @Resource protected ElementService seleniumElementService;

@Resource
protected WebDriver seleniumWebDriver;

public AbstractCappexPage() {

}

}

My child class (a view) looks like this (abbreviated): public class CappexLoginPage extends AbstractCappexPage {

// private WebElement switchToSSL; // private WebElement switchFromSSL; @FindBy(how = How.CSS, using = "input[name=email]") private WebElement userEmailAddress; @FindBy(how = How.CSS, using = "input[name=password]") private WebElement userPassword; @FindBy(how = How.CSS, using = "button[type=submit]") private WebElement loginButton; @FindBy(how = How.CSS, using = "a.fbLogInBtn.fbLoginLink") private WebElement faceBookSignUpLink; @FindBy(how = How.LINK_TEXT, using = "Forgotten Password?") private WebElement forgotPasswordLink;

private String LOCATOR_CSS_USER_EMAIL = "input[name=email]";
private String LOCATOR_CSS_USER_PASS1 = "input[name=password]";
private String LOCATOR_CSS_LOGIN_BUTTON = "button[type=submit]";
private String LOCATOR_CSS_SWITCH_TO_SSL = "a.miniBtn.switchToHTTPS";
private String LOCATOR_CSS_SWITCH_FROM_SSL = "a.miniBtn.switchFromHTTPS";

public CappexLoginPage()
{
    userEmailAddress = seleniumElementService.findExpectedElement(By.cssSelector(LOCATOR_CSS_USER_EMAIL));
    userPassword = seleniumElementService.findExpectedElement(By.cssSelector(LOCATOR_CSS_USER_PASS1));
    loginButton = seleniumElementService.findExpectedElement(By.cssSelector(LOCATOR_CSS_LOGIN_BUTTON));
}

public void submitLoginForm(String email, String password) {
    userEmailAddress.clear();
    userEmailAddress.sendKeys(email);
    userPassword.clear();
    userPassword.sendKeys(password);
    loginButton.click();
}

public void clickFacebookLink() {
    faceBookSignUpLink.click();
}

public void switchToSSL()
{
    seleniumElementService.findExpectedElement(By.cssSelector(LOCATOR_CSS_SWITCH_TO_SSL)).click();
}

}

I have added a reference in the modules ReferenceDataPopulator (renamed): public class CappexReferenceDataPopulator implements ReferenceServicePopulator {

public void populate(SenBotReferenceService referenceService) {

    /**
     * Setup all users references for testing
     */
    referenceService.addUser("admin", new GenericUser("1", "admin", "12345_password"));
    referenceService.addUser("regular user", new GenericUser(null, "regular", "6789_password"));
    referenceService.addUser("student", new GenericUser(null, "dadams.qa+cappexstu2@gmailtest.com", "password"));

    /**
     * Define the views
     */
    referenceService.addPageRepresentationReference("Cappex Abstract Page", AbstractCappexPage.class);
    referenceService.addPageRepresentationReference("Test page1", TestPage1.class);
    referenceService.addPageRepresentationReference("Cappex Login Page", CappexLoginPage.class);
    referenceService.addPageRepresentationReference("Cappex Home Page", CappexHomePage.class);

    /**
     * setup all page references
     */
    referenceService.addPageReference("Some page name", "/path/to/page.html");
    referenceService.addPageReference("Some other page name", "/path/to/other/page.html");

    /**
     * Reference locators
     */
    referenceService.addLocatorReference("Logical table name", By.xpath("//div[@id='containerId']//table"));
    referenceService.addLocatorReference("Logical header name", By.id("header"));

}

}

Am I making the correct assumption that I can do this?

joostschouten commented 10 years ago

@Resource's will only work in Springified classes. You need to make sure that Spring instantiates your services extending AbstractCappexPage. You should be able to find many tutorials online on how to configure this.