patternjavaMinor
GUI for Selenium web testing
Viewed 0 times
seleniumtestingforwebgui
Problem
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();
}
}
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();
}
}
Solution
Naming Convention
Your class name does not conform to the Java convention for naming. Normally,
Java 8 Lambda
If you're using Java8, you can use the lambda style to declare the
It's a bit more concise.
Private modifier bug
There is small "bug" here with the fact that
In your
You have some choice here. You can make the frame visible in your
Logging
You can't go really far by outputting everything with
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
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
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 :
And you use it like :
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
RunnableEventQueue.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));Code Snippets
EventQueue.invokeLater(()-> {
try {
Dynamic_Browser_and_Site window = new Dynamic_Browser_and_Site();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
);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");
}
}
}// 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));Context
StackExchange Code Review Q#98365, answer score: 2
Revisions (0)
No revisions yet.