简体   繁体   English

如何使用Selenium / Webdriver自动执行聊天应用程序?

[英]How to automate a chat application using Selenium/Webdriver?

I have a task for automating a chat application.I am using Webdriver along with Java. 我有一个自动化聊天应用程序的任务。我正在将Webdriver与Java一起使用。 Two different users will login simultaneously on two different browsers and initiate chat. 两个不同的用户将同时在两个不同的浏览器上登录并发起聊天。 I would appreciate if anybody can provide me some suggestions. 如果有人可以给我一些建议,我将不胜感激。

You've got two concerns to worry about. 您有两个要担心的问题。 The first is making sure you've different browsers for each user, and the second is running the two users concurrently. 第一个是确保每个用户使用不同的浏览器,第二个是同时运行两个用户。 There are a couple of different ways to do each of them, so I'll look at them separately. 每种方法都有几种不同的方法,因此我将分别进行研究。

Managing two browsers 管理两个浏览器

The first part, that of opening two browsers, isn't too difficult; 第一部分,打开两个浏览器,并不难。 You can just create a separate WebDriver object for each one. 您可以为每个对象创建一个单独的WebDriver对象。 You may have to use two different browsers, however, to avoid any kind of session sharing issues: 但是,您可能必须使用两种不同的浏览器,以避免任何类型的会话共享问题:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;

WebDriver user_1 = new FirefoxDriver();
WebDriver user_2 = new ChromeDriver();

If you need to open two instances of the same browser, your best option is to use Selenium Grid to host your desired browsers, and then creating remote connections to them: 如果需要打开同一浏览器的两个实例,最好的选择是使用Selenium Grid托管所需的浏览器,然后为它们创建远程连接:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
URL server = new URL("http://your-server-location.com");
WebDriver driver = new RemoteWebDriver(server, capabilities);

If you don't want to set up your own Selenium Grid, you can use a service like Sauce Labs to manage it for you (and also give you an easy way to add extra test platform resources). 如果您不想设置自己的Selenium Grid,则可以使用Sauce Labs之类的服务来为您管理它(并且还为您提供了添加额外测试平台资源的简便方法)。

Managing two users simultaneously 同时管理两个用户

It sounds like you can just have the one test class performing each action in turn against the relevant Webdriver. 听起来您只需要让一个测试类针对相关的Webdriver依次执行每个操作即可。 Use something like the Page Object Model to provide service objects representing your pages, which you can pass an instance of WebDriver too. 使用诸如“ 页面对象模型”之类的东西来提供表示您的页面的服务对象,您也可以传递WebDriver的实例。 Then, you can express your test logic more fluently rather then dealing with different webdrivers and elements: 然后,您可以更流畅地表达测试逻辑,而不必处理不同的Webdriver和元素:

public class Chatsite{
  public WebDriver driver;
  private WebElement talkbox;
  private WebElement chatlog;
  private WebElement sendbutton;

  public Chatsite(WebDriver passed_in_driver){
      driver = passed_in_driver;
      talkbox = driver.find_element("name", "talkbox");
      sendbutton = driver.find_element("name", "send");
      chatlog = driver.find_element("name", "chatlog");
      driver.get("http://www.yoursite.com");
  }

  public void say(String string_to_type){
      talkbox.send_keys(string_to_type);
      sendbutton.click();
  }

  public void sees_in_the_chatlog(String expected_content){
      String current_chat_text =  chatlog.getText();
      assertTrue("Couldn't find content", current_chat_text.contains(expected_content));
  }
}

#Now, in your tests
#Name your users so it's easier to keep track of them
Chatsite david = Chatsite.new(user_1);
Chatsite susan = Chatsite.new(user_2);

david.say("Isn't it a fine day?");
susan.sees_in_the_chatlog("Isn't it a fine day?");

susan.say("If you're going to talk about the weather I'm failing this test case");
## And so on in that fashion

如果您不必在同一浏览器上对其进行测试,则请参考这篇文章,了解如何打开多个浏览器窗口进行测试- 如何在WebDriver中切换实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有Java的Selenium Webdriver自动化Salesforce应用程序? - How to automate Salesforce application using Selenium Webdriver with Java? 如何使用 selenium 和 appium 自动化移动聊天应用程序? - How to automate mobile chat app using selenium with appium? 如何使用 Selenium WebDriver Java 自动化拖放功能 - How to automate drag & drop functionality using Selenium WebDriver Java 如何使用 selenium webdriver 自动弹出对话框窗口 - how to automate popup dialog window using selenium webdriver 如何使用Selenium WebDriver自动执行salesforce登录OTP功能? - How to Automate the salesforce login OTP feature using selenium WebDriver? 是否可以使用Selenium Webdriver自动执行验证码? - Is it possible to automate captcha code using selenium webdriver? 使用 selenium webdriver 自动化引导日期选择器? - Automate bootstrap datepicker using selenium webdriver? 如何使用 selenium webdriver 和 Java 自动化模拟时钟? - How to automate an analog clock with selenium webdriver and Java? 如何使用Selenium Webdriver Java使用Sikuli自动执行Flash - How to automate flash with sikuli with selenium webdriver java 如何使用Selenium Java自动化PHP-Flash Web应用程序 - How to automate PHP-Flash web application using selenium java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM