一、结束浏览器进程
1、windows
String [] cmd={"cmd","/C","tskill chrome"}; Runtime.getRuntime().exec(cmd);
2、mac
String cmds[] = {"killall", "Google Chrome"}; Runtime.getRuntime().exec(cmds);
二、判断元素是否存在
public static boolean isElementExist(WebDriver driver, By locator, int timeoutSeconds) { driver.manage().timeouts().implicitlyWait(timeoutSeconds, TimeUnit.SECONDS); try { driver.findElement(locator); return true; } catch (Exception e) { return false; } }
public boolean isExistElement(WebDriver driver,String by) { List<WebElement> list = driver.findElements(By.xpath(by)); if(!list.isEmpty()){ return true; }else { return false; } }
三、界面元素控件操作方法封装
// 点击 public static void click(WebElement element){ element.click(); } // 输入 public static void clearAndSendkeys(WebElement element,String value) throws Exception { element.clear(); sleep(1000); element.sendKeys(value); } // element方法 public static WebElement getElement (String xpath, WebDriver driver){ WebElement webElement = driver.findElement (By.xpath(xpath)); return webElement; }
四、把某个页面的操作元素管理在一起
可以单独定义一个类
public class element { /** * 定义抖音登录界面元素element * @param driver * @return */ public WebElement loginbtn(WebDriver driver) { WebElement login = otherOperate.getElement("//*[@class=\"web-login-button\"][text()='登录']", driver); return login; } public WebElement mima(WebDriver driver) { WebElement login2 = otherOperate.getElement("//*[@class=\"web-login-button\"][text()='登录']", driver); return mima; } }
五、把界面xpath信息用xml文件管理
<?xml version="1.0" encoding="UTF-8"?> <mybatisGroup page="抖音登录界面"> <login id="login" type="xpath"> //*[@class="web-login-button"][text()='登录'] </login> </mybatisGroup>
六、获取配置文件信息
package AutoTest001; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; public class CommonUtils { /** * properties配置文件信息获取 */ public String getProperties(String file, String key) { String info = ResourceBundle.getBundle(file).getString(key); return info; } /** * xml配置文件信息获取 */ public static Document readXml(String filePath) throws DocumentException { return readXml(new File(filePath)); } public static Document readXml(File file) throws DocumentException { SAXReader reader = new SAXReader(); return reader.read(file); } public static Document readXml(InputStream inputStream) throws DocumentException { SAXReader reader = new SAXReader(); return reader.read(inputStream); } public static Document readXmlFromResources(String resource) throws DocumentException, IOException, URISyntaxException { File file = new File(resource); SAXReader reader = new SAXReader(); return reader.read(file); } public static String getAttribute(Element element, String attrName) { return element.attributeValue(attrName); } public static String getText(Element element) { return element.getText(); } public static List<Element> getElements(Element element, String tagName) { return element.elements(tagName); } /** * 根据节点名称获取子节点数据 */ public static String getElementText(String resource, String qName) throws Exception { Element root = readXmlFromResources(resource).getRootElement(); String text = root.elementText(qName); return text.trim(); } /** * 根据节点名称获取子节点的子节点数据 */ public static String getElementText(String resource, String qName, String SubQName) throws Exception { Element root = readXmlFromResources(resource).getRootElement(); String text = root.element(qName).elementText(SubQName); return text.trim(); } /** * 根据节点名称获取子节点的子节点的数据列表 */ public static List<String> getElementTextList(String resource, String qName) throws Exception { List<Element> elementList = readXmlFromResources(resource).getRootElement().element(qName).elements(); List<String> list = new ArrayList<>(); for (Element e : elementList) { list.add(e.getTextTrim()); } return list; } // 返回xml文件信息 public String getXmlInfo(String type) throws Exception { String path2 = "/Users/xlee/IdeaProjects/AutoTest/src/douyinLogin.xml"; String info = getElementText(path2, type); return info; } }