Problem binding Base abstract class

43 views
Skip to first unread message

Nandish MC

unread,
Feb 26, 2017, 9:45:39 PM2/26/17
to google-guice

I have multiple hierarchical class like below

public abstract class Page extends SelPage
{
public method 1{}
public method 2{}
public method 3{}
}

Class DashboardPage extens Class Page{
{ }

Here I need to bind new Page class
Class ProjPage extends Page{
public method1{} /* change method implementation /
public method 2{} /
 change method implementation /
public method 3{} /
 change method implementation */
}

public class ProjSeleniumSuite extends SeleniumSuite {

private static Injector injector;

@BeforeClass
public static void setUp() throws Exception {
    injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            System.out.println("configuriung");
            bind(Page.class).to(ProjPage.class);
        }
    });
}
@AfterClass
public static void tearDown() throws Exception {
    injector = null;
}

}

The above code runs fine..but it is not binding the Page class method implementation with ProjPage class methods
Can i achieve this using Guice binder like above?

The thing i need to achieve is, when i instantiate DashboardPage , i want ProjPage methods to be called instead of Page class methods.

Please help

Stephan Classen

unread,
Feb 27, 2017, 1:01:58 AM2/27/17
to google...@googlegroups.com
So far this code looks correct. Can you also provide some of the test methods. Maybe something is hiding there.

Nandish MC

unread,
Feb 27, 2017, 3:46:40 PM2/27/17
to google-guice
public abstract class Page extends SelPage
{
   public void logOut() {
        System.out.println("inside Product");
        WebDriverWait wait = new WebDriverWait(getDriver(), getWaitTime());
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("logout")));
        element.click();

        // this is typically not recommended, but logout is an oddball; because
        // we don't know where we're going after logout, we can't do a "wait"
        // for something on that page
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // eat it
        }
    }
}


Class ProjPage extends Page{

 @Override
    public void logOut() {
        System.out.println("Inside Project");
        WebDriverWait wait = new WebDriverWait(getDriver(), getWaitTime());
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("logout")));
        element.click();

        // this is typically not recommended, but logout is an oddball; because
        // we don't know where we're going after logout, we can't do a "wait"
        // for something on that page
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // eat it
        }
    }
}

@RunWith(Suite.class)
@Suite.SuiteClasses({ Login.class, 
    ProjectLogout.class, 
})
public class ProjSeleniumSuite  extends SeleniumSuite {
    
    private static Injector injector;
    /*
     * No code is needed in here. Add new tests to the suite by putting them in
     * the @Suite.SuiteClasses() declaration above.
     */

    @BeforeClass
    public static void setUp() throws Exception {
        System.out.println("setting up");
        injector = Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                System.out.println("configuriung");
                bind(Page.class).to(ProjPage.class);
            }
        });
    }

    @AfterClass
    public static void tearDown() throws Exception {
        System.out.println("tearing down");
        injector = null;
    }
}

public class ProjLogout extends ProjTest {

        /**
         * Log out of the application.
         */
        @Test
        public void logout() {
            System.out.println("inside CBC logout............");
            getCurrentPage().logout();
        }
 }

Class DashboardPage extends Class Page{
{
//inherits Page logout method
methods 1
methods 2
methods 3
 }

public abstract class ProjTest extends SeleniumTest{
       public Page getCurrentPage() {
       try{
            Page dashBoardPage =(Page) super.getCurrentPage(); // This gets the Dashboard page instance
        }catch(Exception e){
            setCurrentPage(null);
        }
        return (Page) super.getCurrentPage();
      }
}


Above are the set of classes used, here I am trying to get ProjPage logout method to be executed instead of Page.logout() method.

This method is an inherited method for Dashboard Page.

currently, even after binding I see Page.logout() method is executed

Stephan Classen

unread,
Feb 27, 2017, 5:17:48 PM2/27/17
to google...@googlegroups.com

I see in you code how you construct the injector. But I never see you making use of it.
I am not familiar with the Selenium library. Does it make use of the injector in the SeleniumTest.getCurrentPage() method?
I rather doubt that Selenium will use you injector. So where do you use the injector?

-- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com. To post to this group, send email to google...@googlegroups.com. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/43253ec2-1e7e-401d-99dd-d429f2d14a0d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

Nandish Chikkabasavaraju

unread,
Feb 27, 2017, 6:38:42 PM2/27/17
to google...@googlegroups.com
No Selenium test is not using injector.

I am in assumption when suite @beforeClass method is executed to bind, it will have Page bound to ProjPage.

How do I need to use the injector in this case?

Between I am new to Guice.
You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/Kema1MvYHtg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-guice...@googlegroups.com.

To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.

Stephan Classen

unread,
Feb 27, 2017, 6:55:58 PM2/27/17
to google...@googlegroups.com

I guess you could do i the following way:

1. make the static injector field on SeleniumSuite public (or package private if all classes reside in the same package)

2. change the following implementation

public abstract class ProjTest extends SeleniumTest {
       @Override
       public Page getCurrentPage() {
          return SeleniumSuite.injector.get(Page.class)
      }
}


But I am not sure as I couldn't find the implementations of SeleniumSuite and SeleniumTest on the Internet. If theses are not classes from the selenium library then you should have provided them in your code snippets for completeness...
-- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com. To post to this group, send email to google...@googlegroups.com. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/4613CA3C-987E-4252-A597-141D2AB0D827%40gmail.com. For more options, visit https://groups.google.com/d/optout.

Nandish Chikkabasavaraju

unread,
Feb 28, 2017, 11:23:52 PM2/28/17
to google...@googlegroups.com
Thanks Stephen, that worked. Appreciate your help.

- Nandish

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages