Search code examples
javasilktest

Truncate the method in java


I have the following code:

public class Search {

    private Desktop desktop = new Desktop();

    @Before
    public void baseState() {
        BrowserBaseState baseState = new BrowserBaseState("silk4j.settings");
        baseState.execute(desktop);

    }

    @Test
    public void searchNames() {
        desktop.<BrowserApplication>find("//BrowserApplication").<BrowserWindow>find("//BrowserWindow").<DomButton>find("//INPUT[@id='edit-submit']").select();

    }

}

I was able to truncate the Test method to this:

public class Search {

    private Desktop desktop = new Desktop();
    BrowserApplication app;


    @Before
    public void baseState() {
        BrowserBaseState baseState = new BrowserBaseState("silk4j.settings");
        app = baseState.execute(desktop);

    }

    @Test
    public void searchNames() {
        app.<BrowserWindow>find("//BrowserWindow").<DomButton>find("//INPUT[@id='edit-submit']").select();

}

How do I truncate the method even further? I would like to be able to use something like this:

win.<DomButton>find("//INPUT[@id='edit-submit']").select();

instead of this chunky long:

desktop.<BrowserApplication>find("//BrowserApplication").<BrowserWindow>find("//BrowserWindow").<DomButton>find("//INPUT[@id='edit-submit']").select();

Please paste the whole class in your response?


Solution

  • public class Search {
    
        private Desktop desktop = new Desktop();
        BrowserWindow win;
    
        @Before
        public void baseState() {
            BrowserBaseState baseState = new BrowserBaseState("silk4j.settings");
            win = baseState.execute(desktop).find("//BrowserWindow");
        }
    
        @Test
        public void searchNames() {
            win.<DomButton>find("//INPUT[@id='edit-submit']").select();
        }
    }