将鼠标从当前位置移动到JComponent位置

时间:2019-07-25 08:19:26

标签: java swing awtrobot gui-testing

我想将鼠标指针从当前位置自动移动到指定的Jcomponent进行GUI测试(方法中没有数字)

click(JComponent comp){
        robot.mouseMove(200, 50);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

1 个答案:

答案 0 :(得分:1)

如果要测试软件,则可以使用AssertJ Swing

如果只想确定组件位置,则需要使用方法// server.ts app.get('/setcookie', function (req, res) { axios.get('https://some_webservice') .then(response => { console.log(response); res.cookie('local', JSON.stringify(response.data)); res.send('Cookie has been set'); }) .catch(error => { console.error(error); res.send('Failed'); }) });

getLocationOnScreen

这是完整的示例:

click(JComponent comp){
    try {
        Point loc = comp.getLocationOnScreen();
        Dimension size = comp.getSize();
        // robot.mouseMove(loc.x, loc.y);
        // move to the center of component
        robot.mouseMove(loc.x + size.width / 2, loc.y + size.height / 2);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    } catch (Exception e) {
        // if component is not on the screen, an exception be thrown.
        e.printStackTrace();
    }
}