1

Firstly I am sure there is an answer lurking in this site and I did try and look but all the methods I tried continuously failed. I am still quite new at programming in Java so go easy on me, because what you are about to witness is some incredibly bodged code!

I am trying to learn Selenium, but before I write tests I wanted to make a simple IDE that asks what browser you'd like to run and what test you'd like to run. So far I had it running fine in a pop up for the browser but that wasn't useful if I wanted to add more options. So I am now trying to create a Jframe in my main class containing other classes which contain the content of any buttons I wish to add. Here is where things go wrong.

  • I have a combo box, this takes in a string of possible browsers and you pick one. (That works)
  • There is also a button which can read the current choice in the combo box. However this button does not seem to be passing the information back to my main class. I will post the code below.

CLASS 1 (MAIN)

public class DynamicBrowsers {

public static void main(String[] args) {


    BrowserBox b = new BrowserBox();
    JFrame IDE = new JFrame("IDE");
    IDE.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    BrowserBox newContentPane = new BrowserBox();
    IDE.setContentPane(newContentPane);
    IDE.setPreferredSize(new Dimension(200, 100));
    IDE.pack();
    IDE.setVisible(true);

    WebDriver driver = null;

    if(b.browserValue == 0){
        //driver=new FirefoxDriver();   
        System.out.println("No browser Selected");
    }else if(b.browserValue == 1){
        driver = new ChromeDriver();
        System.out.println("FF!");  
    }else if(b.browserValue == 2){
        driver = new ChromeDriver();
        System.out.println("Chrome!");
    }else if(b.browserValue == 3){
        driver = new InternetExplorerDriver();
        System.out.println("IE!");
    }
    }
}

CLASS 2 (combo box and button)

public class BrowserBox extends JPanel {

public String browserPick;
String[] browsers = {"Please Select a Browser","Mozilla", "Chrome", "IE"};
public int browserValue = 0;
JButton runButton = new JButton("Run Test");
public JComboBox browserPicker = new JComboBox(browsers);     

public BrowserBox() { 


    add(runButton);        
    add(browserPicker);

    ActionListener cbActionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent listChoice) {

            String s = (String) browserPicker.getSelectedItem();//get the selected item

            switch (s) {//check for a match

                case "Please Select a Browser":
                    System.out.println(browserPick);
                    break;                
                case "Mozilla":
                    browserPick = "Mozilla";
                    System.out.println("Could have been a worse choice than " + browserPick);
                    break;
                case "Chrome":
                    browserPick = "Chrome";
                    System.out.println("Good choice picking " + browserPick);
                    break;
                case "IE":
                   browserPick = "IE";
                    System.out.println("For some reason you chose " + browserPick);
                    break;
                default:
                    browserPick= "Please Select a Browser";
                    System.out.println("No match selected, defaulting too " + browserPick);
                    break;
            } 
        }            
    };

    ActionListener bActionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent runClicked) {

            if (browserPick == "Mozilla"){
                browserValue = 1;     
                System.out.println("FF clicked " + browserValue);
            }
            else if (browserPick == "Chrome"){
                browserValue = 2;
                System.out.println("Chrome clicked " + browserValue);
            }
            else if (browserPick == "IE"){
                browserValue = 3;
                System.out.println("IE clicked " + browserValue);
            } 

        }

    };

    browserPicker.addActionListener(cbActionListener);
    runButton.addActionListener(bActionListener);

}
}  

I imagine I am implementing it all wrong. I kind of feel like I should have made the button a seperate class or in the main class, but I'm unsure. If anyone could point me in the right direction, point out what I'm doing wrong and if possible offer a simple fix that would be great.

Thank you, Farrell

2
  • Do everything on a different class. Use the main only to run the JFrame.
    – neoprez
    Jan 19, 2014 at 0:53
  • I had a feeling that was how I should have done it. I think I will have to do a major reshuffle of everything, or possibly just start fresh. Thanks :) Jan 19, 2014 at 1:31

2 Answers 2

1

There are a couple of things wrong here.

You declare two vars of type BrowserBox -- you use one and test the other (b and newContentPane).

You also use "==" to test whether one string is equal to another. That won't work in the general case; you need to use "String".equals(value) or some other form of the equals() method.

Good luck.

5
  • Thanks :) that's a much shorter fix than I was expecting lol. I was ready to start from scratch! Jan 19, 2014 at 1:30
  • 1
    Be sure to 'accept' answers that answer your question, as well as upvoting them -- it's part of how StackOverflow works.
    – arcy
    Jan 19, 2014 at 2:14
  • The advice above (although good advice) has not solved my problem :\ when I run the code I get Exception in thread "main" java.lang.NullPointerException at DynamicBrowsers.main(DynamicBrowsers.java:45) and I am unable to load a webdriver instance. the if statement seems to not be able to get the value of browserValue from the BrowserBox class but I'm still not sure why or if this is even the issue. :s Jan 19, 2014 at 23:37
  • Well after putting in many many println's the problem seems to be that the variable broswerValue isn't changing from 0 regardless of what browser is picked :\ Jan 19, 2014 at 23:47
  • I cannot tell if you posted this comment before or after your 'answer'; it sounds like this might have helped you solve one problem, but now you have another problem. Put that in a different question, make whatever it is you want answered specific, and include enough information for us to help you with it. Good luck.
    – arcy
    Jan 20, 2014 at 2:17
0

Unbelievable, It seems I had forgotten to capitalise the C in chrome at one point in a string compare and that is what cost me 2 hours of my life lol.

Thank you all for the input, now that I've worked out what was wrong I'm going to go back and try make it more object orientated by having only the JFrame in the main class. Ty again

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.