java做一个toB的客户端操作系统,客户端和web的结合;
主要是使用java编写客户端代码,采用selenium控制浏览器,主要是用到selenium自动化测试的功能;
javaEE 项目调用 selenium使用谷歌控件chromedriver.exe控制浏览器打开web网页自动登录后,采用socket获取服务端接口,自动填写form表单的操作;
1、首先新建一个maven项目,不需要是springboot项目
引入selenium jar包
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.7.1</version>
</dependency>
2、引入浏览器驱动
谷歌浏览器驱动下载地址
https://chromedriver.storage.googleapis.com/index.html
对应下载版本,查看谷歌版本
下载对应版本的驱动放到项目可加载目录下片段
maven打包jar配置
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>1.3.5.RELEASE</version><configuration><!-- 指定启动类 --><mainClass>com.*.*.*.*.Main</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins><resources><resource><!--源文件--><directory>src/main/brdriver/</directory><!--指定编译到META-INF/resources--><targetPath>brdriver/</targetPath><!--指定源文件夹中的哪个资源要编译进行--><includes><include>**/*</include></includes></resource><resource><!--源文件--><directory>image</directory><!--指定编译到META-INF/resources--><targetPath>image/</targetPath><!--指定源文件夹中的哪个资源要编译进行--><includes><include>**/*</include></includes></resource></resources></build>
加载谷歌浏览器驱动代码片段
import java.net.URL;
import java.net.URLDecoder;import javax.swing.JOptionPane;import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;public class SWBInfoImport {public static WebDriver driver;public static boolean flag = true;static {URL url = SWBInfoImport.class.getProtectionDomain().getCodeSource().getLocation(); String filePath = null; try { filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码 filePath=filePath.substring(0,filePath.lastIndexOf("/")+1);} catch (Exception e) { e.printStackTrace(); }System.out.println(filePath);System.setProperty("webdriver.chrome.driver", "./brdriver/chromedriver.exe");driver = new ChromeDriver();driver.manage().window().maximize();}public void queryIsIf() {}}
javax.swing启动客户端代码片段,使用java编辑一个简单的图形界面吧
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;import com.sound.ezaisheng.erp.utils.Tips;public class Major extends JFrame implements ActionListener {/*** */private static final long serialVersionUID = -5878629686730451031L;private static final int width = 600;private static final int height = 300;private JPanel loginPanel;@SuppressWarnings("rawtypes")private JComboBox userBox;private JTextField userNameField;private JPasswordField passwordField;private JButton loginButton;private JButton exitButton;public Major(String username) {this.setLayOut(username);this.setSize(Major.width, Major.height);this.setResizable(false);this.setUndecorated(true);this.setVisible(true);int width = Toolkit.getDefaultToolkit().getScreenSize().width;int height = Toolkit.getDefaultToolkit().getScreenSize().height;this.setLocation((width - Major.width) >> 1, (height - Major.height) >> 1);}@SuppressWarnings({ "serial", "rawtypes", "unchecked" })private void setLayOut(String username) {loginPanel = new JPanel(){@Overridepublic void paintComponent(Graphics g) {// 双缓冲setDoubleBuffered(true);Image loginImg = new ImageIcon("image/major.png").getImage();g.drawImage(loginImg, 0, 0, null);}};loginPanel.setLayout(null);/*userNameField = new JTextField();userNameField.setBounds(150, 148, 260, 70);userNameField.setText("您好:"+username);userNameField.setBackground(new Color(34, 41, 56));userNameField.setFont(new Font("宋体", Font.PLAIN, 30) );loginPanel.add(userNameField);*/exitButton = new JButton("退出");exitButton.setBounds(445, 242, 100, 32);exitButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Tips.getTips().setLayOut("您真的要退出系统吗?", true);}});loginPanel.add(exitButton);this.setContentPane(loginPanel);}/** * 登录按钮监听事件*/public void actionPerformed(ActionEvent e) {}}