1.研发背景:
web ui 自动化开发脚本的时候,有多少人是盲写?直到写完整个页面后运行调试,然后修改因为哪个元素没有定位到而报错,如何能一次性,高效的完成web ui脚本定位,所以纯手敲一款小工具每一步定位元素验证后写入脚本,方便,高效,提高脚本正确率!
2.启动(java环境下):
同级目录下放一个chromedriver.exe(选择下载一个适合自己本地chrome的驱动,不同版本下载地址参考这篇文章)
3.成品展示:
产品体验:
//启动 双击启动或者命令行启动,命令行启动的话可以查看一些打印的日志,方便查找错误问题
C:\Users\Administrator\Desktop\uitest>java -jar testui.jar
(1)点击执行,打开浏览器,消息弹框,关闭即可,后续可以去掉,先留的吧,方便定位问题
浏览器已经打开了
(2) 网页访问,启个本地自己的项目演示
(3) 账号输入: 选择输入input 定位类型class ,定位值:输入,操作值输入:账号,点击执行(账号输入成功,弹框提示.确定关闭即可)
(4) 密码输入: 选择定位方式为xpath,输入定位值:然后点击执行
(5) 点击登录: 选择click方式,选择自己的定位类型,执行
(6) 关闭浏览器,选择chrome开关,执行即可!
(7) 命令行也可以查看日志
4.暂时就演示这么多, 下面把源码分享给大家,需要的拿走不用谢!
//浏览器操作类
package xiaobing.com.ui;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BaseCase {
private static WebDriver driver = null;
/**
* 根据浏览器类型去初始化驱动对象(启动浏览器)
*
* @param browserType
*/
public WebDriver browserInit(String browserType,String driverPath) {
//声明驱动对象
System.out.println("***************Chrome浏览器初始化开始**************");
if ("firefox".equalsIgnoreCase(browserType)) {
//firefox
//声明火狐安装路径,用于selenium去启动火狐
System.setProperty("webdriver.firefox.bin", "D:\\firefox\\firefox.exe");
//声明可执行驱动的路径,用于selenium加载启动
System.setProperty("webdriver.gecko.driver", "src/main/resources/driver/geckodriver.exe");
//打开浏览器,启动驱动实例+浏览器实例
driver = new FirefoxDriver();
System.out.println("创建FirefoxDriver对象成功");
return driver;
} else if ("chrome".equalsIgnoreCase(browserType)) {
//chrome
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
System.out.println("创建ChromeDriver对象成功");
return driver;
} else if ("ie".equalsIgnoreCase(browserType)) {
//ie
System.setProperty("webdriver.ie.driver", "src/main/resources/driver/IEDriverServer.exe");
//创建Capabilities对象
DesiredCapabilities capabilities = new DesiredCapabilities();
//浏览器安全模式保持一致
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//浏览器缩放比例保持正常
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
driver = new InternetExplorerDriver(capabilities);
System.out.println("创建InternetExplorerDriver对象成功");
return driver;
}
return driver;
}
public static WebElement getElement(String type, String byType,String byValue) {
By by = null;
if ("id".equalsIgnoreCase(byType)) {
by = By.id(byValue);
} else if ("name".equalsIgnoreCase(byType)) {
by = By.name(byValue);
} else if ("class".equalsIgnoreCase(byType)) {
by = By.className(byValue);
} else if ("tagname".equalsIgnoreCase(byType)) {
by = By.tagName(byValue);
} else if ("linktext".equalsIgnoreCase(byType)) {
by = By.linkText(byValue);
} else if ("partiallinktext".equalsIgnoreCase(byType)) {
by = By.partialLinkText(byValue);
} else if ("cssselector".equalsIgnoreCase(byType)) {
by = By.cssSelector(byValue);
} else if ("xpath".equalsIgnoreCase(byType)) {
by = By.xpath(byValue);
} else {
String tips = "sorry,请重新选择,暂不支持【" + byType + "】定位方式";
System.out.println(tips);
}
WebElement webElement = null;
try {
WebDriverWait wait = new WebDriverWait(driver, 10);
webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
//记录下当前定位元素,便于查看元素无法操作抛出的问题
return webElement;
} catch (Exception e) {
String tips = "【定位元素失败,请检查】【" + type + "】【" + byType + " : " + byValue + "】";
System.out.println(tips);
}
return webElement;
}
/**
* 关闭浏览器
*
* @param driver
* @throws InterruptedException
*/
public void browserDown(WebDriver driver) {
//关闭浏览器,关闭可执行驱动
driver.quit();
System.out.println("***************测试场景执行结束**************");
}
}
工具类:
package xiaobing.com.ui;
import org.openqa.selenium.WebDriver;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestUiElement {
public static void main(String[] args) {
new xiaobing.com.ui.Window2().setVisible(true);
}
}
class Window2 extends JFrame {
static private JTextField value1;
static private JTextField value2;
static private String type = "click";
static private String by = "id,class,name";
static private JPanel panel;
static private JComboBox jcbox;
static private JComboBox byTypejcbox;
static private WebDriver driver = null;
static private BaseCase baseCase = new BaseCase();
static private boolean chromeStatus = true;
public Window2() {
//设置窗口属性
super("Ui脚本调试(Chrome)");
this.setSize(300, 250);
this.setLayout(null);
this.setLocation(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
// 创建 JLabel
JLabel phoneLabel = new JLabel("操作值:");
/* 这个方法定义了组件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
phoneLabel.setBounds(10, 20, 80, 25);
// panel.setLayout(new GridLayout(0,1));
panel.add(phoneLabel);
/*
* 创建文本域用于用户输入
*/
value1 = new JTextField(20);
value1.setBounds(100, 20, 165, 25);
panel.add(value1);
// 输入Channel的文本域
JLabel channelLabel = new JLabel("调试值:");
channelLabel.setBounds(20, 50, 10, 25);
panel.add(channelLabel);
//输入的信息会以点号代替,密码的安全性
// channelText = new JPasswordField(20);
value2 = new JTextField(20);
value2.setBounds(100, 50, 30, 25);
panel.add(value2);
JLabel choiceLabel = new JLabel("请选择:");
choiceLabel.setBounds(20, 50, 30, 25);
panel.add(choiceLabel);
//下拉框---操作
String[] behavior = new String[]{"open", "click", "input", "chrome开关"};
jcbox = new JComboBox(behavior);
jcbox.setBounds(50, 50, 600, 30);
panel.add(jcbox);
//下拉框---渠道
String[] byType = new String[]{"id", "xpath", "name", "class", "tagname", "linktext", "partiallinktext", "cssselector"};
byTypejcbox = new JComboBox(byType);
byTypejcbox.setBounds(50, 50, 30, 30);
panel.add(byTypejcbox);
//渠道--下拉选触发事件
byTypejcbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
by = (String) byTypejcbox.getSelectedItem();
System.out.println(by + "被选中2222");
}
});
//操作--下拉选触发事件
jcbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
type = (String) jcbox.getSelectedItem();
System.out.println(type + "被选中");
if (type.equalsIgnoreCase("chrome开关")) {
//打开浏览器赋值驱动路径
value1.setText(System.getProperty("user.dir") + "\\chromedriver.exe");
}
}
});
// 创建执行按钮
JButton button = new JButton("执行");
button.setBounds(30, 80, 80, 25);
panel.add(button);
//添加面板
setContentPane(panel);
//按钮触发事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String result = "";
try {
if (type.equalsIgnoreCase("chrome开关")) {
if (chromeStatus) {
//浏览器初始化
driver = baseCase.browserInit("chrome", value1.getText());
chromeStatus = false;
result = "【chrome】初始化,【" + value1.getText() + "】";
} else {
baseCase.browserDown(driver);
chromeStatus = true;
result = "【chrome】关闭";
}
}
if (type.equalsIgnoreCase("open")) {
driver.get(value1.getText());
result = type + "【" + value1.getText() + "】";
}
if (type.equalsIgnoreCase("click")) {
baseCase.getElement(type, by, value2.getText()).click();
result = "【" + type + "】【" + by + "】【" + value2.getText() + "】";
}
if (type.equalsIgnoreCase("input")) {
baseCase.getElement(type, by, value2.getText()).sendKeys(value1.getText());
result = "【" + type + "】【" + by + "】【" + value2.getText() + "】【" + value1.getText() + "】";
}
} catch (Exception ee) {
result = ee.getMessage();
}
System.out.println(result);
JOptionPane.showMessageDialog(null, result);
}
});
}
}
//pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xiaobing.com</groupId>
<artifactId>uitest_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>uitest_demo</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>xiaobing.com.ui.TestUiElement</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>