我的 selenium 框架可以使用传入的消息吗

2022-01-11 00:00:00 selenium rabbitmq message-queue java jsp

我想知道我的 Selenium 框架如何使位于消息队列中的消息出列.我已经构建了一个应用程序来将包含 k/v 对的 JSON 字符串发送到消息队列.

我的架构如下和单独的应用程序:

  1. 存在一个 JSP Web 应用程序,它接受导致 JSON 字符串的参数
  2. 存在消息发送者并获取 JSON 字符串并将其发布到队列
  3. 存在消息消费者并使用消息.它基本上只是坐在这里
  4. 存在 Selenium Java 框架,但我想处理消息,并且对于每条消息,它将解释 k/v 对并启动脚本.

我想使用已经在队列中的消息,并在 selenium 框架中处理这些消息,我该如何实现呢?

我将不胜感激.我已经用代码编辑了问题

<块引用>

这是发送 JSON 消息的代码片段

公共类 MessageSender {公共静态 void main(String[] args) 抛出 IOException {SingleNumberLogin generateLogin = new SingleNumberLogin();//用于构建JSON对象的函数调用字符串 jsonQueue = generateLogin.buildJASONObject();ConnectionFactory conFactory = new ConnectionFactory();尝试 {连接 connInterface = conFactory.newConnection();频道 mqChannel = connInterface.createChannel();mqChannel.queueDeclare("MyQ​​ueue",false,false,false,null);//只是将json分配给另一个字符串,然后发布消息字符串 myMessage = jsonQueue;mqChannel.basicPublish(",MyQueue",false ,false, null,myMessage.getBytes());}抓住 (异常 |超时异常 e){System.out.println(e.getStackTrace());}conFactory.setUsername("guest");conFactory.setPassword("guest");conFactory.setVirtualHost("/");conFactory.setHost(本地主机");conFactory.setPort(5672);}

}

<块引用>

我已插入到自动化脚本的启动函数中的消费者代码的代码片段,因此如果消息到达,则执行单个测试用例

 @BeforeTestpublic static void initializeTestBaseSetup() 抛出异常,IOException,TimeoutException {ConnectionFactory conFactory = new ConnectionFactory();连接 connInterface = conFactory.newConnection();频道 mqChannel = connInterface.createChannel();mqChannel.queueDeclare("MyQ​​ueue",false,false,false,null);mqChannel.basicConsume("MyQ​​ueue", true, (consumerTag, message) -> {//转换为字节数组String m = new String (message.getBody(), "UTF-8");System.out.println(收到消息"+ m);},消费者标签->{});}

<块引用>

输出 JSON

收到的 JSON 消息 2020-08-28T20:39:30.845{

 "NUMBER": "0000011111",类型":BAU",用户":我的用户",电子邮件":riidonesh@gmail.com",}

当单独测试时,它工作得非常好,我的意思是我发送消息并检查消费者是否收到它,将消费者代码添加到我的框架是我卡住的地方.

解决方案

我建议你不要考虑你有什么作为selenium 框架";- 将其视为java 框架".

Selenium 是一组库,可让您在 GUI 级别自动化 Web 浏览器.该框架是有助于创建和管理测试套件的编码解决方案 - 它不必局限于 selenium,而且很有可能这只是它的组件之一.

尝试直接回答您的问题:

  • SELENIUM 无法读取消息
  • JAVA 可以阅读消息

如果你的 rabbitmq 有一个 web 前端,那么你也许可以使用 selenium,但这不是一个非常有效或合乎逻辑的解决方案.

您可能想要考虑的以及我会做的事情是扩展您的框架以使用 rabbitmq 库来处理您需要的消息.这些库是为此任务而设计的.

你说:

<块引用>

我想处理这些消息,并且对于每条消息它都会解释 k/v 对并启动脚本.

我理解这意味着消息是测试的 pre-req 数据.如果您想在测试前读取消息的值,您可以:

  • 将 get/read 放在通用 @Before 方法中
  • 或者,如果它是每个测试用例的特定消息,请将其添加到测试的开头.

你正在使用java,所以你可以做任何你想做的事情.

为了帮助您入门,rabbitmq 教程从这里开始.

这是从队列中读取消息的 hello world 示例:

公共类 Recv {私有最终静态字符串 QUEUE_NAME = 你好";公共静态 void main(String[] argv) 抛出异常 {ConnectionFactory 工厂 = new ConnectionFactory();factory.setHost("localhost");连接连接 = factory.newConnection();频道 channel = connection.createChannel();channel.queueDeclare(QUEUE_NAME, false, false, false, null);System.out.println("[*] 等待消息.退出按 CTRL+C");}}

I would like to know how my Selenium framework can dequeue a message sitting in a message queue. I have built an application to send a JSON string containing k/v pairs to a message queue.

My architecture is as follows and separate apps:

  1. A JSP Web Application exists accepting parameters resulting in a JSON string
  2. A message sender exists and takes the JSON string and publishes it to a Queue
  3. A message consumer exists and consumes the Messages. Its basically just sitting here
  4. A Selenium Java Framework exists, but I would like to process the messages and for each message it will interpret the k/v pairs and kicks off the script.

I would like to use the messages already in the queue and process these messages within the selenium framework, how can I achieve this?

I will appreciate the help. I have edited the question with the code

This is the code snippet to send the JSON Message

public class MessageSender {
public static void main(String[] args) throws IOException {

    SingleNumberLogin generateLogin = new SingleNumberLogin();
    //function call to build the JSON object
    String jsonQueue = generateLogin.buildJASONObject();

    ConnectionFactory conFactory = new ConnectionFactory();
    try {
        Connection connInterface =  conFactory.newConnection();
        Channel mqChannel = connInterface.createChannel();
        mqChannel.queueDeclare("MyQueue",false,false,false,null);
        //Just assigning json to another string, then publish the message      
        String myMessage = jsonQueue;

        mqChannel.basicPublish("","MyQueue",false ,false, null,myMessage.getBytes());
    }catch (
            IOException | TimeoutException e)
    {
        System.out.println(e.getStackTrace());
    }
    conFactory.setUsername("guest");
    conFactory.setPassword("guest");
    conFactory.setVirtualHost("/");
    conFactory.setHost("localhost");
    conFactory.setPort(5672);
}

}

code snippet for consumer code that I have inserted into the startup function of the automation script, so if a message arrives a single test case is executed

    @BeforeTest
public static void initializeTestBaseSetup() throws Exception, IOException, TimeoutException {
    ConnectionFactory conFactory = new ConnectionFactory();
    Connection connInterface =  conFactory.newConnection();
    Channel mqChannel = connInterface.createChannel();
    mqChannel.queueDeclare("MyQueue",false,false,false,null);
    mqChannel.basicConsume("MyQueue", true, (consumerTag, message) -> {
        //convert to byte array
        String m = new String (message.getBody(), "UTF-8");
        System.out.println("Message received" + m);
    }, consumerTag -> {

    });
}

Output JSON

JSON Message received 2020-08-28T20:39:30.845{

  "NUMBER": "0000011111",
  "Type": "BAU",
  "User": "MyUser ",
  "Email": "riidonesh@gmail.com",
}

When tested in isolation, it works perfectly fine, what I mean is that I send the message and check that the consumer receives it, adding the consumer code to my framework is where i am stuck.

解决方案

I would suggest you don't think about what you have as a "selenium framework" - think of it as a "java framework".

Selenium is a set of libraries that allow you automate the web browser at a GUI level. The framework is the coded solution to facilitate creation and management of your test suite - it doesn't have to be limited to selenium and chances that's already just one of its components.

Trying to answer your question directly:

  • SELENIUM cannot read messages
  • JAVA can read messages

If your rabbitmq has a web front end then you may be able to use selenium for it, but this isn't a very efficient or a logical solution.

What you might want to consider, and what i would do, is extending your framework to use the rabbitmq libraries to process messages as you need. These libraries are designed for this task.

You say:

I would like to process the messages and for each message it will interpret the k/v pairs and kicks off the script.

I understand this to mean that the messages are the pre-req data for the tests. If you want to read the values of a message before the test you can either:

  • Place the get/read in a generic @Before method
  • or if it's a specific message per test case, add it into the start of the test.

You're working in java so you can do whatever you want really.

To get you started, the rabbitmq tutorial starts here.

This is there hello world example for reading messages from the queue:

public class Recv {

  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

  }
}

相关文章