Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何基于用户';谁的输入?_Java_Selenium_Jframe_Testng_Selenium Grid - Fatal编程技术网

Java 如何基于用户';谁的输入?

Java 如何基于用户';谁的输入?,java,selenium,jframe,testng,selenium-grid,Java,Selenium,Jframe,Testng,Selenium Grid,我正在Selenium上运行一个测试,它将根据用户的输入访问一个网站 在JFrame上,用户可以输入网站地址并按“run”。这将调用Firefox浏览器实例并导航到URL 但是,我希望能够根据用户的输入同时启动多个浏览器实例 因此,当当前测试仍在运行时,用户可以通过JFrame输入不同的URL链接,然后按“run”,这将显示另一个Firefox浏览器实例,导航到输入的地址 有谁能告诉我怎样才能做到这一点 这就是我目前所拥有的 public class TestFrame { stati

我正在Selenium上运行一个测试,它将根据用户的输入访问一个网站

在JFrame上,用户可以输入网站地址并按“run”。这将调用Firefox浏览器实例并导航到URL

但是,我希望能够根据用户的输入同时启动多个浏览器实例

因此,当当前测试仍在运行时,用户可以通过JFrame输入不同的URL链接,然后按“run”,这将显示另一个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();        
    }    
}
我有一个以编程方式设置的中心和节点,以及一个线程本地远程web驱动程序

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.");
        }
    }
}
最后,这是一个以编程方式编写的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();

    }   
}
公共类Testng{
公共void testRun(){
XmlSuite=newxmlsuite();
套件名称(“项目”);
XmlTest=新的XmlTest(套件);
test.setName(“下载程序”);
列表类=新的ArrayList();
add(新的XmlClass(“example.testing.TestRun”);
test.setXmlClasses(类);
列表套件=新的ArrayList();
套房。添加(套房);
TestNG tng=新的TestNG();
tng.setXmlSuites(套房);
tng.run();
}   
}

我假设您正在使用Swigs,并将尝试从这一点开始回答。 用在秋千上

有两点你们都应该担心:

1。使用

当单击runjbutton时,运行测试的较长过程应作为一个窗口处理,否则GUI可能会在测试运行时冻结

2。使用并发运行浏览器实例

单击两次按钮可能会在后台运行测试,但它似乎允许运行单个浏览器实例。它支持并发,而不是多线程()。 因此,您需要使用执行器或线程运行浏览器

如果这对你有帮助,请告诉我。此外,如果我遗漏了什么,请随意编辑

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

    }   
}