4
\$\begingroup\$

I am wanting to build web-based testing tools for test website environments utilizing the Selenium Web Driver. I am building a GUI which will contain the following: Drop-down for selecting the website, toggle button for selecting the testing environment and a button for launching the specified browser you wish to test the previous criteria in. The drop-down for the website and the toggles will dynamically build a URL based off of the choices (not currently in this code).

Example: I pick Google, then toggle I want to test on TestEnv1 and I want the program to launch the website on env1 using Firefox.

Future goals:

Drop-down implementation that can read the tests that are to be auto-ran which will have selenium tests created for them. This drop-down would be incorporated to allow nearly full-automation for testing.

After creating my mock-up GUI and putting in some sample code my current version looks something similar to:

package automationFramework;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.DefaultButtonModel;
import javax.swing.DefaultComboBoxModel;

public class Dynamic_Browser_and_Site {

    private JFrame frame;
    public int n;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Dynamic_Browser_and_Site window = new Dynamic_Browser_and_Site();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Dynamic_Browser_and_Site() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 500, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        // Site Drop-Down
        JComboBox siteList = new JComboBox();
        siteList.setToolTipText("Choose the website you would like to begin testing on.");
        siteList.setModel(new DefaultComboBoxModel(new String[] {"\"Choose your site...\"", "Google", "Yahoo", "Microsoft", "Apple"}));
        siteList.setMaximumRowCount(5);
        siteList.setBounds(0, 0, 484, 20);
        frame.getContentPane().add(siteList);

        // Browser Buttons
        JButton btnFF = new JButton("Firefox");
        btnFF.setBounds(10, 146, 125, 55);
        frame.getContentPane().add(btnFF);
        btnFF.setEnabled(false);

        JButton btnIE = new JButton("Internet Explorer");
        btnIE.setBounds(349, 146, 125, 55);
        frame.getContentPane().add(btnIE);
        btnIE.setEnabled(false);

        JButton btnCH = new JButton("Chrome");
        btnCH.setBounds(180, 146, 125, 55);
        frame.getContentPane().add(btnCH);
        btnCH.setEnabled(false);

        // Toggle Buttons
        JToggleButton tglbtnEnv1 = new JToggleButton("TestEnv1");
        tglbtnEnv1.setBounds(10, 65, 90, 55);
        frame.getContentPane().add(tglbtnEnv1);

        JToggleButton tglbtnEnv2 = new JToggleButton("TestEnv2");
        tglbtnEnv2.setBounds(135, 65, 90, 55);
        frame.getContentPane().add(tglbtnEnv2);

        JToggleButton tglbtnEnv3 = new JToggleButton("TestEnv3");
        tglbtnEnv3.setBounds(260, 65, 90, 55);
        frame.getContentPane().add(tglbtnEnv3);

        JToggleButton tglbtnEnv4 = new JToggleButton("TestEnv4");
        tglbtnEnv4.setBounds(384, 65, 90, 55);
        frame.getContentPane().add(tglbtnEnv4);

        // Button Group Array for toggling environments
        ButtonGroup group = new ButtonGroup();
        group.add(tglbtnEnv1);
        group.add(tglbtnEnv2);
        group.add(tglbtnEnv3);
        group.add(tglbtnEnv4);
        // System.out.println(tglbtnEnv1.getAction());


        // TestEnv1 toggle prompt
        tglbtnEnv1.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent ev) {
              if(ev.getStateChange()==ItemEvent.SELECTED){
                  System.out.println(tglbtnEnv1.getActionCommand( ));
                  System.out.println("button is selected");

                  // Toggle browser buttons
                  btnFF.setEnabled(true);
                  btnCH.setEnabled(true);
                  btnIE.setEnabled(true);
                  System.out.println("Firefox button enabled");
                  System.out.println("Chrome button enabled");
                  System.out.println("Internet Explorer button enabled");
              } else{
                System.out.println("button is not selected");
              }
           }
        });

        // TestEnv2 toggle prompt
        tglbtnEnv2.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent ev) {
              if(ev.getStateChange()==ItemEvent.SELECTED){
                  System.out.println(tglbtnEnv2.getActionCommand( ));
                  System.out.println("button is selected");

                  // Toggle browser buttons
                  btnFF.setEnabled(true);
                  btnCH.setEnabled(true);
                  btnIE.setEnabled(true);
                  System.out.println("Firefox button enabled");
                  System.out.println("Chrome button enabled");
                  System.out.println("Internet Explorer button enabled");
              } else{
                System.out.println("button is not selected");
              }
           }
        });

        // TestEnv3 toggle prompt
        tglbtnEnv3.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent ev) {
              if(ev.getStateChange()==ItemEvent.SELECTED){
                  System.out.println(tglbtnEnv3.getActionCommand( ));
                  System.out.println("button is selected");

                  // Toggle browser buttons
                  btnFF.setEnabled(true);
                  btnCH.setEnabled(true);
                  btnIE.setEnabled(true);
                  System.out.println("Firefox button enabled");
                  System.out.println("Chrome button enabled");
                  System.out.println("Internet Explorer button enabled");
              } else{
                System.out.println("button is not selected");
              }
           }
        });

        // TestEnv4 toggle prompt
        tglbtnEnv4.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent ev) {
              if(ev.getStateChange()==ItemEvent.SELECTED){
                  System.out.println(tglbtnEnv4.getActionCommand( ));
                  System.out.println("button is selected");

                  // Toggle browser buttons
                  btnFF.setEnabled(true);
                  btnCH.setEnabled(true);
                  btnIE.setEnabled(true);
                  System.out.println("Firefox button enabled");
                  System.out.println("Chrome button enabled");
                  System.out.println("Internet Explorer button enabled");
              } else{
                System.out.println("button is not selected");
              }
           }
        });     
    }
}

I am wanting to know of any concerns or tips to further improve and optimize this code.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Naming Convention

Your class name does not conform to the Java convention for naming. Normally, Dynamic_Browser_and_Site would be DynamicBrowserAndSite. The normal name class use PascalCase, you can read more in the Google style guide.

Java 8 Lambda

If you're using Java8, you can use the lambda style to declare the Runnable

EventQueue.invokeLater(()-> {
                try {
                    Dynamic_Browser_and_Site window = new Dynamic_Browser_and_Site();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        );

It's a bit more concise.

Private modifier bug

There is small "bug" here with the fact that private JFrame frame; is private.

In your main you're doing window.frame.setVisible(true); which only works because your main is in the same class that the frame is declared in. If I would want to use your class in another context, I would be stuck with not being able to declare the frame visible.

You have some choice here. You can make the frame visible in your initialize method or you can also provide a way to "toggle" the visibility.

Logging

You can't go really far by outputting everything with System.out.println(). At some point, you need a real logging framework, where you can log in a console, a file and have a real way to deal with this. There are lots of good framework out there.

Naming

Namoing is hard to get right! It's always something I find is easy to miss, but there are some basic rules you need to follow.

Don't use the class type in the name of the variable. Nowadays, everybody use IDE and have access to type information or the type is really non-relevant to know exactly what the variable is used for.

Let's take JToggleButton tglbtnEnv1 = new JToggleButton("TestEnv1");

First you're using abreviation which make it less readable IMO. Second, I don't need to know in the name that's a toggle buton, the declaration and type of the variable already provide me with that information. So let's just called it testEnv1!

Code Repetition

Your item listener all does the same thing, re-enabled all the browser buttons. Why do you need to declare it four times? That's a lot on code duplication.

To remedy to this, you can create an inner class and use that one in the itemListener :

private static class EnvListener implements ItemListener {
        private final JButton btnFF;
        private final JButton btnCH;
        private final JButton btnIE;

        public EnvListener(JButton btnFF, JButton btnCH, JButton btnIE) {
            this.btnFF = btnFF;
            this.btnCH = btnCH;
            this.btnIE = btnIE;
        }

        public void itemStateChanged(ItemEvent ev) {
            if(ev.getStateChange()==ItemEvent.SELECTED){
                JToggleButton source = (JToggleButton) ev.getItem();
                System.out.println(source.getActionCommand());
                System.out.println("button is selected");

                // Toggle browser buttons
                btnFF.setEnabled(true);
                btnCH.setEnabled(true);
                btnIE.setEnabled(true);
                System.out.println("Firefox button enabled");
                System.out.println("Chrome button enabled");
                System.out.println("Internet Explorer button enabled");
            } else{
                System.out.println("button is not selected");
            }
        }
    }

And you use it like :

    // TestEnv1 toggle prompt
    tglbtnEnv1.addItemListener(new EnvListener(btnFF, btnCH, btnIE));

    // TestEnv2 toggle prompt
    tglbtnEnv2.addItemListener(new EnvListener(btnFF, btnCH, btnIE));

    // TestEnv3 toggle prompt
    tglbtnEnv3.addItemListener(new EnvListener(btnFF, btnCH, btnIE));

    // TestEnv4 toggle prompt
    tglbtnEnv4.addItemListener(new EnvListener(btnFF, btnCH, btnIE));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.