是否有任何工具可以为测试自动化目的添加 html 属性?

Is there any tool to add html properties for test automation purposes?

有人知道是否有工具可以将 html 属性添加到 DOM 树中的元素,以便使用 Selenium 轻松选择元素进行自动化测试?

该工具需要添加一个新的 属性,这样它就不会干扰现有的 javascript,例如 "data-nav-id" 属性 的值可以是一个散列,一些独特的东西,比如:data-nav-id="XjsaAksah2ma4"。

另一件事是它不应该更改已经存在的 ID,假设我向页面添加了一个新的 div 标记,该工具只会将 属性 插入到新元素。

如果有一种方法可以将该工具添加为 Jenkins 中持续集成的步骤,那就太完美了。

我刚刚编写了一小段代码,可以在 html 文件中给每个标签注入一个唯一的 ID,给定它的路径。我只需要在我的 WEB-INF 目录的每个子目录的每个 html 或动态前端文件(例如 .jsp 或前端相关文件的任何位置创建此 运行这取决于您使用的 Web 应用程序框架。

package org.automation.htmlcompass;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.codec.digest.DigestUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.NodeVisitor;

public class HtmlCompass {

    public static void parseDocument(String path, String outputFile) throws IOException {
        File input = new File(path);
        Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

        doc.traverse(new NodeVisitor() {

            public void tail(Node node, int depth) {
                // TODO Auto-generated method stub
            }

            public void head(Node node, int depth) {
                if (node instanceof Element) {
                    Element e = (Element) node;

                    if(!e.hasAttr("data-nav-id")) {
                        System.out.println("injecting >> tag name "+ e.tagName() + " depth " + depth);
                        injectHash(e);
                    }
                }
            }
        });

        BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(outputFile), "UTF-8"));
        htmlWriter.write(doc.toString());
        htmlWriter.close();
    }

    private static void injectHash(Element element) {
        String seed = element.toString() + System.currentTimeMillis();
        String hash = DigestUtils.sha512Hex(seed).substring(0, 20);
        element.attr("data-nav-id", hash);
    }
}