栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

启动Chromium,然后等待其关闭

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

启动Chromium,然后等待其关闭

这是一个示例程序,可以通过某种方式设法证明硒库满足您所需的功能。您需要先下载硒库并将其设置为IDE,然后才能运行此程序。

该程序允许您单击一个按钮。然后,Firefox浏览器会自动打开并在几秒钟内启动一个网站。网站正在加载中,请稍候。之后,您可以关闭Firefox浏览器。该程序还将在2秒后自动关闭。

import java.awt.BorderLayout;import java.awt.FlowLayout;import java.net.ConnectException;import javax.swing.*;import org.openqa.selenium.NoSuchWindowException;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class AnotherTest extends Jframe {    WebDriver driver;    JLabel label;    public AnotherTest() {        super("Test");        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();        setBounds((screenSize.width - 400) / 2, (screenSize.height - 100) / 2, 400, 100);        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setLayout(new BorderLayout());        addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) {     quitApplication(); }        });        JButton jButton1 = new javax.swing.JButton();        label = new JLabel("");        JPanel panel = new JPanel(new FlowLayout());        panel.add(jButton1);        add(panel, BorderLayout.CENTER);        add(label, BorderLayout.SOUTH);        jButton1.setText("Open Microsoft");        jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {     label.setText("Loading browser. Please wait..");     java.util.Timer t = new java.util.Timer();     t.schedule(new java.util.TimerTask() {         @Override         public void run() {  openBrowserAndWait();         }     }, 10); }        });    }    private void openBrowserAndWait() {        driver = new FirefoxDriver();        String baseUrl = "https://www.microsoft.com";        driver.get(baseUrl);        java.util.Timer monitorTimer = new java.util.Timer();        monitorTimer.schedule(new java.util.TimerTask() { @Override public void run() {     while (true) {         checkDriver();         try {  Thread.sleep(2000);         } catch (InterruptedException ex) {         }     } }        }, 10);    }    private void checkDriver() {        if (driver == null) { return;        }        boolean shouldExit = false;        try { label.setText(driver.getTitle());        } catch (NoSuchWindowException e) { System.out.println("Browser has been closed. Exiting Program"); shouldExit = true;        } catch (Exception e) { System.out.println("Browser has been closed. Exiting Program"); shouldExit = true;        }        if (shouldExit) { this.quitApplication();        }    }    private void quitApplication() {        // attempt to close gracefully        if (driver != null) { try {     driver.quit(); } catch (Exception e) { }        }        System.exit(0);    }    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() { public void run() {     new AnotherTest().setVisible(true); }        });    }}

Selenium主要用于测试Web应用程序的自动化。它可以直接打开浏览器并读取其中的html内容。有关其他信息,请参见http://www.seleniumhq.org/。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419216.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号