简体   繁体   中英

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. 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. 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:

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).

Managing two users simultaneously

It sounds like you can just have the one test class performing each action in turn against the relevant Webdriver. Use something like the Page Object Model to provide service objects representing your pages, which you can pass an instance of WebDriver too. Then, you can express your test logic more fluently rather then dealing with different webdrivers and elements:

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中切换实例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM