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
# 1 楼答案
如果你不需要在同一个浏览器上测试它,那么请参阅这篇关于如何打开多个浏览器窗口进行测试的文章-How to switch instances in WebDriver
# 2 楼答案
你有两个问题要担心。第一个是确保每个用户有不同的浏览器,第二个是同时运行这两个用户。每种方法都有几种不同的实现方式,所以我将分别介绍它们
管理两个浏览器
第一部分,打开两个浏览器,并不太难;您只需为每个WebDriver对象创建一个单独的WebDriver对象。但是,为了避免任何类型的会话共享问题,您可能必须使用两种不同的浏览器:
如果需要打开同一浏览器的两个实例,最好的选择是使用Selenium Grid托管所需的浏览器,然后创建与它们的远程连接:
如果你不想建立自己的Selenium网格,你可以使用Sauce Labs这样的服务来管理它(同时也为你提供了一种添加额外测试平台资源的简单方法)
同时管理两个用户
听起来,您可以让一个测试类针对相关的Webdriver依次执行每个操作。使用类似Page Object Model的东西来提供表示页面的服务对象,您也可以传递WebDriver的实例。然后,您可以更流利地表达您的测试逻辑,而不是处理不同的Web驱动程序和元素: