2015-11-30 72 views
2

我對Selenium進行測試,根據用戶的輸入訪問網站。如何根據用戶的輸入在JFrame中同時啓動瀏覽器實例?

在JFrame上,用戶可以輸入網站的地址,然後按'運行'。這將調用Firefox瀏覽器實例並導航到URL。

但是,我希望能夠根據用戶的輸入同時啓動多個瀏覽器實例。

因此,當前測試仍在運行時,用戶可以通過JFrame輸入不同的URL鏈接,然後按'運行',這將啓動導航到輸入地址的另一個Firefox瀏覽器實例。

任何人都可以給我一個想法,我怎麼能做到這一點?

這是我到目前爲止。

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();   
    }  
} 

我有一個集線器和節點以編程方式設置,並有一個線程本地遠程網絡驅動程序。

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."); 
     } 
    } 
} 

這是一個示例測試。我曾嘗試使用@DataProvider,但需要一個數組,而不是我正在尋找的。

public class TestRun extends TestConfig { 

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

} 

最後,這是一個編程編寫的testng類。

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(); 

    } 
} 

回答

0

我假設您使用的是Swings,並會嘗試從這一點回答。 在Swing中使用Concurrency

有兩點,你都應該理會:對於JFrame組件行動

1.運行後臺進程使用Swingworker

當運行JButton的點擊,運行測試的較長的過程應作爲background tasks進行處理,否則GUI可能會在測試運行時凍結。同時使用Executor

當點擊了兩次可以在後臺運行的測試按鈕,但似乎鞦韆

2.運行瀏覽器實例允許來運行瀏覽器的一個實例。它支持併發性,不完全是多線程(What is the difference between concurrent programming and parallel programming?)。 因此,您需要使用Executor或Thread運行瀏覽器。

讓我知道,如果這可以幫助你。另外,如果我錯過了某些東西,請隨時編輯。

相關問題