問題描述
我想知道我的 Selenium 框架如何使位于消息隊列中的消息出列.我已經構建了一個應用程序來將包含 k/v 對的 JSON 字符串發送到消息隊列.
我的架構如下和單獨的應用程序:
- 存在一個 JSP Web 應用程序,它接受導致 JSON 字符串的參數
- 存在消息發送者并獲取 JSON 字符串并將其發布到隊列
- 存在消息消費者并使用消息.它基本上只是坐在這里
- 存在 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:
- A JSP Web Application exists accepting parameters resulting in a JSON string
- A message sender exists and takes the JSON string and publishes it to a Queue
- A message consumer exists and consumes the Messages. Its basically just sitting here
- 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");
}
}
這篇關于我的 selenium 框架可以使用傳入的消息嗎的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!