使用 JUnit 运行 测试用例,其中涉及接受用户输入的对话框

Using JUnit to run testcases which involve dialog boxes which take in user input

我正在构建一个应用程序,它通过提供分步说明来帮助用户浏览网站。 说明以对话框的形式给出。我正在使用 Java Swing 创建 GUI 对话框。

这是启动网络驱动程序的主要 class。

public class initialDriver{

    public static WebDriver driver;

    public static void main(String[] args) throws IOException, InterruptedException{

        testCases.simpleGoogle();
        testCases.wiki();


}

}

控制权传递给此 class。这由单独的测试用例组成。每个测试用例都为 GUI 发送参数。

public class testCases{


    public static void simpleGoogle() throws IOException, InterruptedException
    {

        initialDriver.driver = new FirefoxDriver();  
        initialDriver.driver.get("https://www.google.com"); 

        String message = "Enter search term,Click search button,Check the results";

        new StepMessage("GoogleSearch",message);

    }


    public static void wiki() throws IOException, InterruptedException
    {

        initialDriver.driver = new FirefoxDriver();  
        initialDriver.driver.get("https://www.wikipedia.org"); 

        String message = "Enter 'Solar Energy',Click search,Check the page";

        new StepMessage("WikipediaSearch",message);     

    }

}

这是 GUI class。单击 'OK' 时,它会打开另一个 GUI 框架,该框架接收 'Testcase - Pass or Fail'.

的用户输入
public class StepMessage extends JFrame {

    private JPanel contentPane;

    public static javax.swing.JTextArea msgArea;

        public String message;
        public String nameoftheTest;

    public static javax.swing.JButton btnOk;



    /**
     * Create the frame.
     */
    public StepMessage(String Testname,String message) {

        nameoftheTest = Testname;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 226);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);


        msgArea = new JTextArea();
        msgArea.setBounds(12, 13, 397, 68);
        contentPane.add(msgArea);
        msgArea.setEditable(false);
        StepMessage.msgArea.setText(message);


        btnOk = new JButton("OK");
        btnOk.setBounds(175, 135, 97, 30);
        contentPane.add(btnOk);
        btnOk.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {
                OkBtnActionPerformed(evt);
            }
        });



    }



    public void OkBtnActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.dispose();
        PassFail frame2 = new PassFail(nameoftheTest);
        frame2.setTitle("Test Pass or Fail");
        frame2.setVisible(true);

    }

}

我想 运行 使用 JUnit 进行多个此类测试。每个测试都必须导航到网站,打开 GUI 并等待用户输入。然后控件应该传递回 testCases class 并执行下一个测试。

如何做到这一点?

这可以通过 CompletableFuture (or if not available, Guava's SettableFuture) 来完成。像这样编写测试:

@Test
public void testGoogle() throws Exception {
    CompletableFuture<Void> future = new CompletableFuture<>();
    testCases.simpleGoogle(future);
    future.get();
}

并通过 UI 传递未来,以便它在事件处理程序中可用。然后根据测试是通过还是失败调用 future.complete(null)future.completeExceptionally(new AssertionError("Failed"))