groovy 如何使用Gmail插件/GMail API for Java/Jakarta Mail API/从电子邮件中检索链接?

mnemlml8  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(239)

我正在使用Katalon Studio,需要从测试电子邮件收件箱中检索一些注册链接。我找到了一些API/服务来访问测试电子邮件收件箱,可以从中获取我需要的消息,它是一个看起来像HTML的字符串。
我不关心HTML,我只想“点击”电子邮件中的链接!
我该怎么做!?

k0pti3hp

k0pti3hp1#

假设您成功获得了消息字符串here's how you can retrieve the link from it,并假设您的电子邮件消息检索方法调用返回HTML字符串。
为了保存您的点击次数:

import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPathFactory

import org.w3c.dom.Element

// feel free to make this your own :)
public final class EmailUtils {
/**
     * **NOTE**: forked from https://stackoverflow.com/a/2269464/2027839 , and then refactored
     * 
     * Processes HTML, using XPath
     * 
     * @param html
     * @param xpath
     * @return the result 
     */
    public static String ProcessHTML(String html, String xpath) {

        final String properHTML = this.ToProperHTML(html);

        final Element document = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder()
                .parse(new ByteArrayInputStream( properHTML.bytes ))
                .documentElement;
        return XPathFactory.newInstance()
                .newXPath()
                .evaluate( xpath, document );
    }

    private static String ToProperHTML(String html) {
        // SOURCE: https://stackoverflow.com/a/19125599/2027839
        String properHTML = html.replaceAll( "(&(?!amp;))", "&" );

        if (properHTML.contains('<!DOCTYPE html'))
            return properHTML;

        return """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head></head>
    <body>
        ${properHTML}
    </body>
</html>
""";
    }
}

从那里,你必须注销你的HTML消息字符串(或者在你的方法调用周围放置调试断点,并从调试器中提取它),pretty print it,然后,你可以使用你的web测试技能为实际的链接创建一些xpath选择器字符串。
然后,你使用我的代码:

WebUI.navigateToUrl(yourEmailMessageContent, "${yourLinkXPath}/@href");

公平地说,电子邮件信息到达收件箱需要一些时间。因此,你可能还需要一些重试逻辑。下面是我真实的项目代码库中的示例:

import java.util.concurrent.TimeUnit
// ...rest of imports

public final class EmailUtils { 

    //...rest of code base

    public static String ExtractSignUpLink() {
        String link;

        int retryAttempts;

        ActionHandler.Handle({
            link = this.ProcessHTML(this.GetLatestMessageBody(30),
                    "//a[.//div[@class = 'sign-mail-btn-text']]/@href");
        }, { boolean success, ex ->
            if (success)
                return;
            // handle ex
            if (((GoogleJsonResponseException)ex).getDetails().getCode() >= 400)
                throw ex;
            sleep(1000 * 2**retryAttempts++);
        }, TimeUnit.MINUTES.toSeconds(15))

        return link;
    }

    //...rest of code base
}

public final class ActionHandler {
    public static void Handle(Closure onAction, Closure onDone, long timeOut) {
        long startTime = System.currentTimeSeconds();
        while (System.currentTimeSeconds() < startTime + timeOut) {
            try {
                onDone(true, onAction());
                return;
            } catch (Exception ex) {
                onDone(false, ex);
            }
        }
    }
}

相关问题