2

I am running a test on Selenium which will visit a website according to the user's input.

On the JFrame, the user can enter the address of a website and press 'run'. This will call a Firefox browser instance and navigate to the URL.

However, I want to be able to start multiple browser instances concurrently based on the user's input.

So while the current test is still running, the user can enter a different URL link through the JFrame then press 'run', which will bring up another Firefox browser instance navigating to the entered address.

Can anyone give me an idea of how I can achieve this?

This is what I have so far.

public class TestFrame {

    static JFrame frame;
    static String url;

    public static void frame() {
        frame = new JFrame();
        frame.setSize(350, 100);
        frame.setLocationRelativeTo(null);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JTextField text = new JTextField(20);
        JButton button = new JButton("Run");
        panel.add(text);
        panel.add(button);

        frame.add(panel);
        button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            url = text.getText();
            Testng func = new Testng();
            func.testRun();
        }
      });         
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        frame();        
    }    
}

I have a hub and node programmatically setup, and a thread local remote web driver.

public class TestConfig {

    ThreadLocal<RemoteWebDriver> driver;
    Hub hub;
    SelfRegisteringRemote remote;

    @BeforeClass
    public void setUp() throws Exception {

        // Start hub
        GridHubConfiguration config = new GridHubConfiguration();
        config.setHost("localhost");
        config.setPort(4444);
        hub = new Hub(config);
        hub.start();

        SelfRegisteringRemote node = null;
        RegistrationRequest req = new RegistrationRequest();

        // Create capabilities instance
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
        capabilities.setCapability(RegistrationRequest.MAX_INSTANCES,5);

        // Set configurations for registration request
        Map<String, Object> nodeConfig = new HashMap<String, Object>();
        nodeConfig.put(RegistrationRequest.AUTO_REGISTER, true);
        nodeConfig.put(RegistrationRequest.HUB_HOST, hub.getHost());
        nodeConfig.put(RegistrationRequest.HUB_PORT, hub.getPort());
        nodeConfig.put(RegistrationRequest.PORT, 5555);
        URL remoteURL = new URL("http://" + hub.getHost() + ":" + 5555);
        nodeConfig.put(RegistrationRequest.PROXY_CLASS,
                "org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
        nodeConfig.put(RegistrationRequest.MAX_SESSION, 5);
        nodeConfig.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
        nodeConfig.put(RegistrationRequest.REMOTE_HOST, remoteURL);
        nodeConfig.put(RegistrationRequest.MAX_INSTANCES, 5);

        // Registration request
        req.addDesiredCapability(capabilities);
        req.setRole(GridRole.NODE);
        req.setConfiguration(nodeConfig);

        // Register node 
        node = new SelfRegisteringRemote(req);
        node.startRemoteServer();
        node.startRegistrationProcess();        

    }

    @BeforeMethod
    public void start() throws MalformedURLException, IOException, Exception {
        driver = new ThreadLocal<RemoteWebDriver>();

        // Set capabilities
        DesiredCapabilities dc = new DesiredCapabilities();
        FirefoxProfile profile = new FirefoxProfile();        
        dc.setCapability(FirefoxDriver.PROFILE, profile);
        dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());

        URL remoteURL = new URL("http://" + hub.getHost() + ":" + hub.getPort() 
                + "/wd/hub");
        RemoteWebDriver remoteDriver = new RemoteWebDriver(remoteURL, dc);
        driver.set(remoteDriver);
    }

    public WebDriver getDriver() {
        return driver.get();
    }

    @AfterClass
    public void shutdown() throws Exception {
        if (remote != null) {
            remote.stopRemoteServer();
            System.out.println("Node stopped.");
        }

        if (hub != null) {
            hub.stop();
            System.out.println("Hub stopped.");
        }
    }
}

This is an example test. I have tried using @DataProvider but that takes in an array, not quite what I'm looking for.

public class TestRun extends TestConfig {

    @Test 
    public void test() {
        getDriver().get(url);
        getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        getDriver().manage().window().maximize();
    }

}

Finally, this is a programmatically written testng class.

public class Testng {

    public void testRun() {

        XmlSuite suite = new XmlSuite();
        suite.setName("Project");

        XmlTest test = new XmlTest(suite);
        test.setName("Downloader");

        List<XmlClass> classes = new ArrayList<XmlClass>();
        classes.add(new XmlClass("example.testing.TestRun"));
        test.setXmlClasses(classes);

        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        TestNG tng = new TestNG();
        tng.setXmlSuites(suites);
        tng.run();

    }   
}
Manu
  • 2,251
  • 19
  • 30
Csh
  • 103
  • 1
  • 11

1 Answers1

0

I assume you are using Swings and will try to answer from that point. Use Concurrency in Swing for that.

There are two points that you should both bother about:

1. Run background process for JFrame Component actions using Swingworker

When the Run JButton is clicked, the longer process of running the tests should be handled as a background tasks, else the GUI may get freeze for the time the test runs.

2. Run browser instance concurrently using Executor

The button when clicked twice may run the test in background, but it seems Swings allows for single instance of browser to be run. It supports concurrency, not exactly multithreading (What is the difference between concurrent programming and parallel programming?). So, you need to run browsers using the Executor or Thread.

Let me know if that helps you. Also, feel free to edit if I miss something.

Community
  • 1
  • 1
Manu
  • 2,251
  • 19
  • 30