java&selenium菜鸟笔记

这篇博客介绍了如何在Windows和Mac上结束浏览器进程,以及在Java中判断元素是否存在和进行界面元素操作的方法封装。此外,还展示了如何将页面元素管理和界面XPath信息用XML文件进行管理,以及如何从XML配置文件中读取信息。内容涵盖了自动化测试中的实用技巧和代码实现。
摘要由CSDN通过智能技术生成

一、结束浏览器进程

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;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值